Source for file Month.class.php

Documentation is available at Month.class.php

  1. <?php
  2. /**
  3. * @since 5/4/05
  4. * @package harmoni.primitives.chronology
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: Month.class.php,v 1.4 2007/09/04 20:25:25 adamfranco Exp $
  10. *
  11. * @link http://harmoni.sourceforge.net/
  12. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  13. */
  14. require_once(dirname(__FILE__)."/Timespan.class.php");
  15.  
  16. /**
  17. * I am a timespan that represents a month.
  18. *
  19. * To create new Month instances, <b>use one of the static instance-creation
  20. * methods</b>, NOT 'new Month':
  21. * - {@link current Month::current()}
  22. * - {@link current Month::current()}
  23. * - {@link epoch Month::epoch()}
  24. * - {@link fromString Month::fromString($aString)}
  25. * - {@link starting Month::starting($aDateAndTime)}
  26. * - {@link startingDuration Month::startingDuration($aDateAndTime, $aDuration)}
  27. * - {@link startingEnding Month::startingEnding($startDateAndTime, $endDateAndTime)}
  28. * - {@link withMonthYear Month::withMonthYear($anIntegerOrStringMonth, $anIntYear)}
  29. *
  30. * @since 5/4/05
  31. * @package harmoni.primitives.chronology
  32. *
  33. * @copyright Copyright &copy; 2005, Middlebury College
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  35. *
  36. * @version $Id: Month.class.php,v 1.4 2007/09/04 20:25:25 adamfranco Exp $
  37. *
  38. * @link http://harmoni.sourceforge.net/
  39. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  40. */
  41. class Month
  42. extends Timespan
  43. {
  44.  
  45. /*******************************************************
  46. * Class Methods
  47. *********************************************************/
  48.  
  49. /**
  50. * Return the index of a string Month.
  51. *
  52. * @param string $aNameString
  53. * @return integer
  54. * @access public
  55. * @since 5/4/05
  56. * @static
  57. */
  58. function indexOfMonth ( $aNameString ) {
  59. foreach (ChronologyConstants::MonthNames() as $i => $name) {
  60. if (preg_match("/$aNameString.*/i", $name))
  61. return $i;
  62. }
  63. $errorString = $aNameString ." is not a recognized month name.";
  64. if (function_exists('throwError'))
  65. throwError(new Error($errorString));
  66. else
  67. die ($errorString);
  68. }
  69. /**
  70. * Return the name of the month at index.
  71. *
  72. * @param integer $anInteger
  73. * @return string
  74. * @access public
  75. * @since 5/4/05
  76. * @static
  77. */
  78. function nameOfMonth ( $anInteger ) {
  79. $names = ChronologyConstants::MonthNames();
  80. if ($names[$anInteger])
  81. return $names[$anInteger];
  82. $errorString = $anInteger ." is not a valid month index.";
  83. if (function_exists('throwError'))
  84. throwError(new Error($errorString));
  85. else
  86. die ($errorString);
  87. }
  88. /**
  89. * Answer the days in this month on a given year.
  90. *
  91. * @param string $indexOrNameString
  92. * @param ingteger $yearInteger
  93. * @return integer
  94. * @access public
  95. * @since 5/5/05
  96. * @static
  97. */
  98. function daysInMonthForYear ( $indexOrNameString, $yearInteger ) {
  99. if (is_numeric($indexOrNameString))
  100. $index = $indexOrNameString;
  101. else
  102. $index = Month::indexOfMonth($indexOrNameString);
  103. if ($index < 1 | $index > 12) {
  104. $errorString = $index ." is not a valid month index.";
  105. if (function_exists('throwError'))
  106. throwError(new Error($errorString));
  107. else
  108. die ($errorString);
  109. }
  110. $monthDays = ChronologyConstants::DaysInMonth();
  111. $days = $monthDays[$index];
  112. if ($index == 2 && Year::isLeapYear($yearInteger))
  113. return $days + 1;
  114. else
  115. return $days;
  116. }
  117. /*******************************************************
  118. * Class Methods - Instance Creation
  119. *
  120. * All static instance creation methods have an optional
  121. * $class parameter which is used to get around the limitations
  122. * of not being able to find the class of the object that
  123. * recieved the initial method call rather than the one in
  124. * which it is implemented. These parameters SHOULD NOT BE
  125. * USED OUTSIDE OF THIS PACKAGE.
  126. *********************************************************/
  127.  
  128. /**
  129. * Answer a new object that represents now.
  130. *
  131. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  132. * This parameter is used to get around the limitations of not being
  133. * able to find the class of the object that recieved the initial
  134. * method call.
  135. * @return object Month
  136. * @access public
  137. * @since 5/5/05
  138. * @static
  139. */
  140. function current ( $class = 'Month' ) {
  141. $obj = parent::current($class);
  142. return $obj;
  143. }
  144. /**
  145. * Answer a Month starting on the Squeak epoch: 1 January 1901
  146. *
  147. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  148. * This parameter is used to get around the limitations of not being
  149. * able to find the class of the object that recieved the initial
  150. * method call.
  151. * @return object Month
  152. * @access public
  153. * @since 5/5/05
  154. * @static
  155. */
  156. function epoch ( $class = 'Month' ) {
  157. $obj = parent::epoch($class);
  158. return $obj;
  159. }
  160. /**
  161. * Read a month from the stream in any of the forms:
  162. *
  163. * - July 1998
  164. *
  165. * @param string $aString
  166. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  167. * This parameter is used to get around the limitations of not being
  168. * able to find the class of the object that recieved the initial
  169. * method call.
  170. * @return object Month
  171. * @access public
  172. * @since 5/10/05
  173. */
  174. function fromString ( $aString, $class = 'Month' ) {
  175. $parser = StringParser::getParserFor($aString);
  176. if (!is_string($aString) || !preg_match('/[^\W]/', $aString) || !$parser) {
  177. $null = null;
  178. return $null;
  179. // die("'".$aString."' is not in a valid format.");
  180. }
  181. eval('$result = '.$class.'::withMonthYear($parser->month(),
  182. $parser->year(), $class);');
  183. return $result;
  184. }
  185. /**
  186. * Create a new object starting now, with zero duration
  187. *
  188. * @param object DateAndTime $aDateAndTime
  189. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  190. * This parameter is used to get around the limitations of not being
  191. * able to find the class of the object that recieved the initial
  192. * method call.
  193. * @return object Month
  194. * @access public
  195. * @since 5/5/05
  196. * @static
  197. */
  198. function starting ( $aDateAndTime, $class = 'Month' ) {
  199. $obj = parent::starting($aDateAndTime, $class);
  200. return $obj;
  201. }
  202. /**
  203. * Create a new object with given start and end DateAndTimes
  204. *
  205. * @param object DateAndTime $startDateAndTime
  206. * @param object DateAndTime $endDateAndTime
  207. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  208. * This parameter is used to get around the limitations of not being
  209. * able to find the class of the object that recieved the initial
  210. * method call.
  211. * @return object Month
  212. * @access public
  213. * @since 5/11/05
  214. */
  215. function startingEnding ( $startDateAndTime, $endDateAndTime,
  216. $class = 'Month' )
  217. {
  218. $obj = parent::startingEnding ( $startDateAndTime, $endDateAndTime, $class);
  219. return $obj;
  220. }
  221. /**
  222. * Create a new object starting now, with a given duration.
  223. * Override - as each month has a defined duration
  224. *
  225. * @param object DateAndTime $aDateAndTime
  226. * @param object Duration $aDuration
  227. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  228. * This parameter is used to get around the limitations of not being
  229. * able to find the class of the object that recieved the initial
  230. * method call.
  231. * @return object Month
  232. * @access public
  233. * @since 5/5/05
  234. * @static
  235. */
  236. function startingDuration ( $aDateAndTime, $aDuration, $class = 'Month' ) {
  237. // Validate our passed class name.
  238. if (!(strtolower($class) == strtolower('Month')
  239. || is_subclass_of(new $class, 'Month')))
  240. {
  241. die("Class, '$class', is not a subclass of 'Month'.");
  242. }
  243. $start =$aDateAndTime->asDateAndTime();
  244. $adjusted = DateAndTime::withYearMonthDay($start->year(), $start->month(), 1);
  245. $days = Month::daysInMonthForYear($adjusted->month(), $adjusted->year());
  246. $month = new $class;
  247. $month->setStart($adjusted);
  248. $month->setDuration(Duration::withDays($days));
  249. return $month;
  250. }
  251. /**
  252. * Create a Month for the given <year> and <month>.
  253. * <month> may be a number or a String with the
  254. * name of the month. <year> should be with 4 digits.
  255. *
  256. * @param string $anIntegerOrStringMonth
  257. * @param integer $anIntegerYear Four-digit year.
  258. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  259. * This parameter is used to get around the limitations of not being
  260. * able to find the class of the object that recieved the initial
  261. * method call.
  262. * @return object Month
  263. * @access public
  264. * @since 5/11/05
  265. */
  266. function withMonthYear ( $anIntegerOrStringMonth, $anIntegerYear,
  267. $class = 'Month' )
  268. {
  269. eval('$result = '.$class.'::starting(DateAndTime::withYearMonthDay(
  270. $anIntegerYear, $anIntegerOrStringMonth, 1), $class);');
  271. return $result;
  272. }
  273.  
  274. /*******************************************************
  275. * Instance methods - Accessing
  276. *********************************************************/
  277.  
  278. /**
  279. * Answer the number of days
  280. *
  281. * @return integer
  282. * @access public
  283. * @since 5/5/05
  284. */
  285. function daysInMonth () {
  286. return $this->duration->days();
  287. }
  288. /**
  289. * Answer the index of this object
  290. *
  291. * @return integer
  292. * @access public
  293. * @since 5/23/05
  294. */
  295. function index () {
  296. return $this->startMonthIndex();
  297. }
  298. /**
  299. * Answer the name of this object
  300. *
  301. * @return string
  302. * @access public
  303. * @since 5/23/05
  304. */
  305. function name () {
  306. return $this->startMonthName();
  307. }
  308. /**
  309. * Answer a printable string
  310. *
  311. * @return string
  312. * @access public
  313. * @since 5/23/05
  314. */
  315. function printableString () {
  316. return $this->name().' '.$this->startYear();
  317. }
  318.  
  319. /*******************************************************
  320. * Instance methods - Operations
  321. *********************************************************/
  322.  
  323.  
  324. /**
  325. * Answer the previous object of our duration.
  326. *
  327. * @return object Timespan
  328. * @access public
  329. * @since 5/10/05
  330. */
  331. function previous () {
  332. eval('$result = '.get_class($this).'::startingDuration(
  333. $this->start->minus(Duration::withDays(1)),
  334. $this->duration,
  335. "'.get_class($this).'");');
  336. return $result;
  337. }
  338. /*******************************************************
  339. * Instance Methods - Converting
  340. *********************************************************/
  341.  
  342. /**
  343. * Answer the receiver as a Month
  344. *
  345. * @return object Month
  346. * @access public
  347. * @since 5/23/05
  348. */
  349. function asMonth () {
  350. return $this;
  351. }
  352.  
  353. }
  354.  
  355. ?>

Documentation generated on Wed, 19 Sep 2007 10:25:07 -0400 by phpDocumentor 1.3.0RC3