Source for file Year.class.php

Documentation is available at Year.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: Year.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 Year.
  18. *
  19. * To create new Year instances, <b>use one of the static instance-creation
  20. * methods</b>, NOT 'new Year':
  21. * - {@link current Year::current()}
  22. * - {@link current Year::current()}
  23. * - {@link epoch Year::epoch()}
  24. * - {@link starting Year::starting($aDateAndTime)}
  25. * - {@link startingDuration Year::startingDuration($aDateAndTime, $aDuration)}
  26. * - {@link startingEnding Year::startingEnding($startDateAndTime, $endDateAndTime)}
  27. * - {@link withYear Year::withYear($anInteger)}
  28. *
  29. * @since 5/4/05
  30. * @package harmoni.primitives.chronology
  31. *
  32. * @copyright Copyright &copy; 2005, Middlebury College
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  34. *
  35. * @version $Id: Year.class.php,v 1.4 2007/09/04 20:25:25 adamfranco Exp $
  36. *
  37. * @link http://harmoni.sourceforge.net/
  38. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  39. */
  40. class Year
  41. extends Timespan
  42. {
  43.  
  44. /*******************************************************
  45. * Class Methods
  46. *********************************************************/
  47.  
  48.  
  49. /*******************************************************
  50. * Class Methods - Instance Creation
  51. *
  52. * All static instance creation methods have an optional
  53. * $class parameter which is used to get around the limitations
  54. * of not being able to find the class of the object that
  55. * recieved the initial method call rather than the one in
  56. * which it is implemented. These parameters SHOULD NOT BE
  57. * USED OUTSIDE OF THIS PACKAGE.
  58. *********************************************************/
  59.  
  60. /**
  61. * Answer a new object that represents now.
  62. *
  63. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  64. * This parameter is used to get around the limitations of not being
  65. * able to find the class of the object that recieved the initial
  66. * method call.
  67. * @return object Year
  68. * @access public
  69. * @since 5/5/05
  70. * @static
  71. */
  72. function current ( $class = 'Year' ) {
  73. $obj = parent::current($class);
  74. return $obj;
  75. }
  76. /**
  77. * Answer a Year starting on the Squeak epoch: 1 January 1901
  78. *
  79. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  80. * This parameter is used to get around the limitations of not being
  81. * able to find the class of the object that recieved the initial
  82. * method call.
  83. * @return object Year
  84. * @access public
  85. * @since 5/5/05
  86. * @static
  87. */
  88. function epoch ( $class = 'Year' ) {
  89. $obj = parent::epoch($class);
  90. return $obj;
  91. }
  92. /**
  93. * Create a new object starting now
  94. *
  95. * @param object DateAndTime $aDateAndTime
  96. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  97. * This parameter is used to get around the limitations of not being
  98. * able to find the class of the object that recieved the initial
  99. * method call.
  100. * @return object Year
  101. * @access public
  102. * @since 5/5/05
  103. * @static
  104. */
  105. function starting ( $aDateAndTime, $class = 'Year' ) {
  106. $obj = parent::starting($aDateAndTime, $class);
  107. return $obj;
  108. }
  109. /**
  110. * Create a new object with given start and end DateAndTimes
  111. *
  112. * @param object DateAndTime $startDateAndTime
  113. * @param object DateAndTime $endDateAndTime
  114. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  115. * This parameter is used to get around the limitations of not being
  116. * able to find the class of the object that recieved the initial
  117. * method call.
  118. * @return object Year
  119. * @access public
  120. * @since 5/11/05
  121. */
  122. function startingEnding ( $startDateAndTime, $endDateAndTime,
  123. $class = 'Year' )
  124. {
  125. $obj = parent::startingEnding ( $startDateAndTime, $endDateAndTime, $class);
  126. return $obj;
  127. }
  128. /**
  129. * Create a new object starting from midnight
  130. *
  131. * @param object DateAndTime $aDateAndTime
  132. * @param object Duration $aDuration
  133. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  134. * This parameter is used to get around the limitations of not being
  135. * able to find the class of the object that recieved the initial
  136. * method call.
  137. * @return object Year
  138. * @access public
  139. * @since 5/5/05
  140. * @static
  141. */
  142. function startingDuration ( $aDateAndTime, $aDuration, $class = 'Year' ) {
  143. // Validate our passed class name.
  144. if (!(strtolower($class) == strtolower('Year')
  145. || is_subclass_of(new $class, 'Year')))
  146. {
  147. die("Class, '$class', is not a subclass of 'Year'.");
  148. }
  149. $asDateAndTime =$aDateAndTime->asDateAndTime();
  150. $midnight =$asDateAndTime->atMidnight();
  151. $year = new $class;
  152. $year->setStart($midnight);
  153. $year->setDuration(Duration::withDays(Year::daysInYear($midnight->year())));
  154. return $year;
  155. }
  156.  
  157. /**
  158. * Create a new Year
  159. *
  160. * @param integer $anInteger
  161. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  162. * This parameter is used to get around the limitations of not being
  163. * able to find the class of the object that recieved the initial
  164. * method call.
  165. * @return object Year
  166. * @access public
  167. * @since 5/4/05
  168. * @static
  169. */
  170. function withYear ( $anInteger, $class = 'Year' ) {
  171. $start = DateAndTime::withYearMonthDay($anInteger, 1, 1);
  172. eval('$result = '.$class.'::startingDuration(
  173. $start,
  174. $null = NULL,
  175. $class
  176. );');
  177. return $result;
  178. }
  179.  
  180. /*******************************************************
  181. * Hybrid Class/Instance Methods
  182. *********************************************************/
  183.  
  184. /**
  185. * Return TRUE if the year passed is a leap year
  186. *
  187. * This method can be either called as a class method (with a parameter)
  188. * or as an instance method (without a parameter).
  189. *
  190. * @param optional integer $anInteger
  191. * @return boolean
  192. * @access public
  193. * @since 5/4/05
  194. * @static
  195. */
  196. function isLeapYear ( $anInteger = NULL ) {
  197. if (is_null($anInteger) && is_object ($this))
  198. return $this->isLeapYear($this->startYear());
  199. else {
  200. if($anInteger > 0)
  201. $adjustedYear = $anInteger;
  202. else
  203. $adjustedYear = 0 - ($anInteger + 1);
  204. if (($adjustedYear % 4 != 0)
  205. || (($adjustedYear % 100 == 0) && ($adjustedYear % 400 != 0)))
  206. {
  207. return FALSE;
  208. } else {
  209. return TRUE;
  210. }
  211. }
  212. }
  213. /**
  214. * Return the number of days in a year.
  215. *
  216. * This method can be either called as a class method (with a parameter)
  217. * or as an instance method (without a parameter).
  218. *
  219. * @param optional integer $anInteger
  220. * @return integer
  221. * @access public
  222. * @since 5/4/05
  223. */
  224. function daysInYear ( $anInteger = NULL ) {
  225. if (is_null($anInteger) && is_object ($this))
  226. return $this->duration->days();
  227. else {
  228. if (is_null($anInteger)) {
  229. $errorString = "Cannot execute daysInYear for NULL.";
  230. if (function_exists('throwError'))
  231. throwError(new Error($errorString));
  232. else
  233. die ($errorString);
  234. }
  235. if (Year::isLeapYear($anInteger))
  236. return 365 +1;
  237. else
  238. return 365;
  239. }
  240. }
  241.  
  242. /*******************************************************
  243. * Instance Methods - Accessing
  244. *********************************************************/
  245.  
  246. /**
  247. * Answer a printable string
  248. *
  249. * @return string
  250. * @access public
  251. * @since 5/23/05
  252. */
  253. function printableString () {
  254. return $this->startYear();
  255. }
  256.  
  257. /*******************************************************
  258. * Instance Methods - Converting
  259. *********************************************************/
  260.  
  261. /**
  262. * Answer the receiver as a Year
  263. *
  264. * @return object Year
  265. * @access public
  266. * @since 5/23/05
  267. */
  268. function asYear () {
  269. return $this;
  270. }
  271. }
  272.  
  273. ?>

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