Source for file Week.class.php

Documentation is available at Week.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: Week.class.php,v 1.5 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 Week.
  18. *
  19. * To create new Week instances, <b>use one of the static instance-creation
  20. * methods</b>, NOT 'new Week':
  21. * - {@link current Week::current()}
  22. * - {@link current Week::current()}
  23. * - {@link epoch Week::epoch()}
  24. * - {@link starting Week::starting($aDateAndTime)}
  25. * - {@link startingDuration Week::startingDuration($aDateAndTime, $aDuration)}
  26. * - {@link startingEnding Week::startingEnding($startDateAndTime, $endDateAndTime)}
  27. *
  28. * @since 5/4/05
  29. * @package harmoni.primitives.chronology
  30. *
  31. * @copyright Copyright &copy; 2005, Middlebury College
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  33. *
  34. * @version $Id: Week.class.php,v 1.5 2007/09/04 20:25:25 adamfranco Exp $
  35. *
  36. * @link http://harmoni.sourceforge.net/
  37. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  38. */
  39. class Week
  40. extends Timespan
  41. {
  42.  
  43. /*******************************************************
  44. * Class Methods
  45. *********************************************************/
  46.  
  47. /**
  48. * Return the index of a string Day.
  49. *
  50. * @param string $aNameString
  51. * @return integer
  52. * @access public
  53. * @since 5/4/05
  54. */
  55. function indexOfDay ( $aNameString ) {
  56. foreach (ChronologyConstants::DayNames() as $i => $name) {
  57. if (preg_match("/$aNameString.*/i", $name))
  58. return $i;
  59. }
  60. $errorString = $aNameString ." is not a recognized day name.";
  61. if (function_exists('throwError'))
  62. throwError(new Error($errorString));
  63. else
  64. die ($errorString);
  65. }
  66. /**
  67. * Return the name of the day at index.
  68. *
  69. * @param integer $anInteger
  70. * @return string
  71. * @access public
  72. * @since 5/4/05
  73. */
  74. function nameOfDay ( $anInteger ) {
  75. $names = ChronologyConstants::DayNames();
  76. if ($names[$anInteger])
  77. return $names[$anInteger];
  78. $errorString = $anInteger ." is not a valid day index.";
  79. if (function_exists('throwError'))
  80. throwError(new Error($errorString));
  81. else
  82. die ($errorString);
  83. }
  84. /**
  85. * Answer the day at the start of the week
  86. *
  87. * @return string
  88. * @access public
  89. * @since 5/20/05
  90. */
  91. function startDay () {
  92. $dayNames = ChronologyConstants::DayNames();
  93. return $dayNames[1];
  94. }
  95. /*******************************************************
  96. * Class Methods - Instance Creation
  97. *
  98. * All static instance creation methods have an optional
  99. * $class parameter which is used to get around the limitations
  100. * of not being able to find the class of the object that
  101. * recieved the initial method call rather than the one in
  102. * which it is implemented. These parameters SHOULD NOT BE
  103. * USED OUTSIDE OF THIS PACKAGE.
  104. *********************************************************/
  105.  
  106. /**
  107. * Answer a new object that represents now.
  108. *
  109. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  110. * This parameter is used to get around the limitations of not being
  111. * able to find the class of the object that recieved the initial
  112. * method call.
  113. * @return object Week
  114. * @access public
  115. * @since 5/5/05
  116. * @static
  117. */
  118. function current ( $class = 'Week' ) {
  119. $obj = parent::current($class);
  120. return $obj;
  121. }
  122. /**
  123. * Answer a Month starting on the Squeak epoch: 1 January 1901
  124. *
  125. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  126. * This parameter is used to get around the limitations of not being
  127. * able to find the class of the object that recieved the initial
  128. * method call.
  129. * @return object Week
  130. * @access public
  131. * @since 5/5/05
  132. * @static
  133. */
  134. function epoch ( $class = 'Week' ) {
  135. $obj = parent::epoch($class);
  136. return $obj;
  137. }
  138. /**
  139. * Create a new object starting now, with zero duration
  140. *
  141. * @param object DateAndTime $aDateAndTime
  142. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  143. * This parameter is used to get around the limitations of not being
  144. * able to find the class of the object that recieved the initial
  145. * method call.
  146. * @return object Week
  147. * @access public
  148. * @since 5/5/05
  149. * @static
  150. */
  151. function starting ( $aDateAndTime, $class = 'Week' ) {
  152. $obj = parent::starting($aDateAndTime, $class);
  153. return $obj;
  154. }
  155. /**
  156. * Create a new object with given start and end DateAndTimes
  157. *
  158. * @param object DateAndTime $startDateAndTime
  159. * @param object DateAndTime $endDateAndTime
  160. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  161. * This parameter is used to get around the limitations of not being
  162. * able to find the class of the object that recieved the initial
  163. * method call.
  164. * @return object Week
  165. * @access public
  166. * @since 5/11/05
  167. */
  168. function startingEnding ( $startDateAndTime, $endDateAndTime,
  169. $class = 'Week' )
  170. {
  171. $obj = parent::startingEnding ( $startDateAndTime, $endDateAndTime, $class);
  172. return $obj;
  173. }
  174. /**
  175. * Create a new object starting now, with a given duration.
  176. * Override - as each Week has a defined duration
  177. *
  178. * @param object DateAndTime $aDateAndTime
  179. * @param object Duration $aDuration
  180. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  181. * This parameter is used to get around the limitations of not being
  182. * able to find the class of the object that recieved the initial
  183. * method call.
  184. * @return object Week
  185. * @access public
  186. * @since 5/5/05
  187. * @static
  188. */
  189. function startingDuration ( $aDateAndTime, $aDuration, $class = 'Week' ) {
  190. // Validate our passed class name.
  191. if (!(strtolower($class) == strtolower('Week')
  192. || is_subclass_of(new $class, 'Week')))
  193. {
  194. die("Class, '$class', is not a subclass of 'Week'.");
  195. }
  196. $asDateAndTime =$aDateAndTime->asDateAndTime();
  197. $midnight =$asDateAndTime->atMidnight();
  198. $dayNames = ChronologyConstants::DayNames();
  199. $temp = $midnight->dayOfWeek() + 7 - array_search(Week::startDay(), $dayNames);
  200. $delta = abs($temp - (intval($temp/7) * 7));
  201. $adjusted =$midnight->minus(Duration::withDays($delta));
  202. $obj = parent::startingDuration($adjusted, Duration::withWeeks(1), $class);
  203. return $obj;
  204. }
  205.  
  206. /*******************************************************
  207. * Instance Methods - Converting
  208. *********************************************************/
  209.  
  210. /**
  211. * Answer the receiver as a Week
  212. *
  213. * @return object Week
  214. * @access public
  215. * @since 5/23/05
  216. */
  217. function asWeek () {
  218. return $this;
  219. }
  220. }
  221.  
  222. ?>

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