PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/pear/Calendar/Decorator.php

http://akelosframework.googlecode.com/
PHP | 558 lines | 212 code | 42 blank | 304 comment | 12 complexity | a05f64dc99b3229780358eca765a79e7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | PHP Version 4 |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2002 The PHP Group |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.02 of the PHP license, |
  10. // | that is bundled with this package in the file LICENSE, and is |
  11. // | available at through the world-wide-web at |
  12. // | http://www.php.net/license/3_0.txt. |
  13. // | If you did not receive a copy of the PHP license and are unable to |
  14. // | obtain it through the world-wide-web, please send a note to |
  15. // | license@php.net so we can mail you a copy immediately. |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Harry Fuecks <hfuecks@phppatterns.com> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Decorator.php,v 1.3 2005/10/22 10:29:46 quipo Exp $
  21. //
  22. /**
  23. * @package Calendar
  24. * @version $Id: Decorator.php,v 1.3 2005/10/22 10:29:46 quipo Exp $
  25. */
  26. /**
  27. * Decorates any calendar class.
  28. * Create a subclass of this class for your own "decoration".
  29. * Used for "selections"
  30. * <code>
  31. * class DayDecorator extends Calendar_Decorator
  32. * {
  33. * function thisDay($format = 'int')
  34. * {
  35. .* $day = parent::thisDay('timestamp');
  36. .* return date('D', $day);
  37. * }
  38. * }
  39. * $Day = & new Calendar_Day(2003, 10, 25);
  40. * $DayDecorator = & new DayDecorator($Day);
  41. * echo $DayDecorator->thisDay(); // Outputs "Sat"
  42. * </code>
  43. * @abstract
  44. * @package Calendar
  45. */
  46. class Calendar_Decorator
  47. {
  48. /**
  49. * Subclass of Calendar being decorated
  50. * @var object
  51. * @access private
  52. */
  53. var $calendar;
  54. /**
  55. * Constructs the Calendar_Decorator
  56. * @param object subclass to Calendar to decorate
  57. */
  58. function Calendar_Decorator(& $calendar)
  59. {
  60. $this->calendar = & $calendar;
  61. }
  62. /**
  63. * Defines the calendar by a Unix timestamp, replacing values
  64. * passed to the constructor
  65. * @param int Unix timestamp
  66. * @return void
  67. * @access public
  68. */
  69. function setTimestamp($ts)
  70. {
  71. $this->calendar->setTimestamp($ts);
  72. }
  73. /**
  74. * Returns a timestamp from the current date / time values. Format of
  75. * timestamp depends on Calendar_Engine implementation being used
  76. * @return int timestamp
  77. * @access public
  78. */
  79. function getTimestamp()
  80. {
  81. return $this->calendar->getTimeStamp();
  82. }
  83. /**
  84. * Defines calendar object as selected (e.g. for today)
  85. * @param boolean state whether Calendar subclass
  86. * @return void
  87. * @access public
  88. */
  89. function setSelected($state = true)
  90. {
  91. $this->calendar->setSelected($state = true);
  92. }
  93. /**
  94. * True if the calendar subclass object is selected (e.g. today)
  95. * @return boolean
  96. * @access public
  97. */
  98. function isSelected()
  99. {
  100. return $this->calendar->isSelected();
  101. }
  102. /**
  103. * Adjusts the date (helper method)
  104. * @return void
  105. * @access public
  106. */
  107. function adjust()
  108. {
  109. $this->calendar->adjust();
  110. }
  111. /**
  112. * Returns the date as an associative array (helper method)
  113. * @param mixed timestamp (leave empty for current timestamp)
  114. * @return array
  115. * @access public
  116. */
  117. function toArray($stamp=null)
  118. {
  119. return $this->calendar->toArray($stamp);
  120. }
  121. /**
  122. * Returns the value as an associative array (helper method)
  123. * @param string type of date object that return value represents
  124. * @param string $format ['int' | 'array' | 'timestamp' | 'object']
  125. * @param mixed timestamp (depending on Calendar engine being used)
  126. * @param int integer default value (i.e. give me the answer quick)
  127. * @return mixed
  128. * @access private
  129. */
  130. function returnValue($returnType, $format, $stamp, $default)
  131. {
  132. return $this->calendar->returnValue($returnType, $format, $stamp, $default);
  133. }
  134. /**
  135. * Defines Day object as first in a week
  136. * Only used by Calendar_Month_Weekdays::build()
  137. * @param boolean state
  138. * @return void
  139. * @access private
  140. */
  141. function setFirst ($state = true)
  142. {
  143. if ( method_exists($this->calendar,'setFirst') ) {
  144. $this->calendar->setFirst($state);
  145. }
  146. }
  147. /**
  148. * Defines Day object as last in a week
  149. * Used only following Calendar_Month_Weekdays::build()
  150. * @param boolean state
  151. * @return void
  152. * @access private
  153. */
  154. function setLast($state = true)
  155. {
  156. if ( method_exists($this->calendar,'setLast') ) {
  157. $this->calendar->setLast($state);
  158. }
  159. }
  160. /**
  161. * Returns true if Day object is first in a Week
  162. * Only relevant when Day is created by Calendar_Month_Weekdays::build()
  163. * @return boolean
  164. * @access public
  165. */
  166. function isFirst() {
  167. if ( method_exists($this->calendar,'isFirst') ) {
  168. return $this->calendar->isFirst();
  169. }
  170. }
  171. /**
  172. * Returns true if Day object is last in a Week
  173. * Only relevant when Day is created by Calendar_Month_Weekdays::build()
  174. * @return boolean
  175. * @access public
  176. */
  177. function isLast()
  178. {
  179. if ( method_exists($this->calendar,'isLast') ) {
  180. return $this->calendar->isLast();
  181. }
  182. }
  183. /**
  184. * Defines Day object as empty
  185. * Only used by Calendar_Month_Weekdays::build()
  186. * @param boolean state
  187. * @return void
  188. * @access private
  189. */
  190. function setEmpty ($state = true)
  191. {
  192. if ( method_exists($this->calendar,'setEmpty') ) {
  193. $this->calendar->setEmpty($state);
  194. }
  195. }
  196. /**
  197. * @return boolean
  198. * @access public
  199. */
  200. function isEmpty()
  201. {
  202. if ( method_exists($this->calendar,'isEmpty') ) {
  203. return $this->calendar->isEmpty();
  204. }
  205. }
  206. /**
  207. * Build the children
  208. * @param array containing Calendar objects to select (optional)
  209. * @return boolean
  210. * @access public
  211. * @abstract
  212. */
  213. function build($sDates = array())
  214. {
  215. $this->calendar->build($sDates);
  216. }
  217. /**
  218. * Iterator method for fetching child Calendar subclass objects
  219. * (e.g. a minute from an hour object). On reaching the end of
  220. * the collection, returns false and resets the collection for
  221. * further iteratations.
  222. * @return mixed either an object subclass of Calendar or false
  223. * @access public
  224. */
  225. function fetch()
  226. {
  227. return $this->calendar->fetch();
  228. }
  229. /**
  230. * Fetches all child from the current collection of children
  231. * @return array
  232. * @access public
  233. */
  234. function fetchAll()
  235. {
  236. return $this->calendar->fetchAll();
  237. }
  238. /**
  239. * Get the number Calendar subclass objects stored in the internal
  240. * collection.
  241. * @return int
  242. * @access public
  243. */
  244. function size()
  245. {
  246. return $this->calendar->size();
  247. }
  248. /**
  249. * Determine whether this date is valid, with the bounds determined by
  250. * the Calendar_Engine. The call is passed on to
  251. * Calendar_Validator::isValid
  252. * @return boolean
  253. * @access public
  254. */
  255. function isValid()
  256. {
  257. return $this->calendar->isValid();
  258. }
  259. /**
  260. * Returns an instance of Calendar_Validator
  261. * @return Calendar_Validator
  262. * @access public
  263. */
  264. function & getValidator()
  265. {
  266. $validator = $this->calendar->getValidator();
  267. return $validator;
  268. }
  269. /**
  270. * Returns a reference to the current Calendar_Engine being used. Useful
  271. * for Calendar_Table_Helper and Calendar_Validator
  272. * @return object implementing Calendar_Engine_Inteface
  273. * @access private
  274. */
  275. function & getEngine()
  276. {
  277. return $this->calendar->getEngine();
  278. }
  279. /**
  280. * Returns the value for the previous year
  281. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  282. * @return int e.g. 2002 or timestamp
  283. * @access public
  284. */
  285. function prevYear($format = 'int')
  286. {
  287. return $this->calendar->prevYear($format);
  288. }
  289. /**
  290. * Returns the value for this year
  291. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  292. * @return int e.g. 2003 or timestamp
  293. * @access public
  294. */
  295. function thisYear($format = 'int')
  296. {
  297. return $this->calendar->thisYear($format);
  298. }
  299. /**
  300. * Returns the value for next year
  301. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  302. * @return int e.g. 2004 or timestamp
  303. * @access public
  304. */
  305. function nextYear($format = 'int')
  306. {
  307. return $this->calendar->nextYear($format);
  308. }
  309. /**
  310. * Returns the value for the previous month
  311. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  312. * @return int e.g. 4 or Unix timestamp
  313. * @access public
  314. */
  315. function prevMonth($format = 'int')
  316. {
  317. return $this->calendar->prevMonth($format);
  318. }
  319. /**
  320. * Returns the value for this month
  321. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  322. * @return int e.g. 5 or timestamp
  323. * @access public
  324. */
  325. function thisMonth($format = 'int')
  326. {
  327. return $this->calendar->thisMonth($format);
  328. }
  329. /**
  330. * Returns the value for next month
  331. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  332. * @return int e.g. 6 or timestamp
  333. * @access public
  334. */
  335. function nextMonth($format = 'int')
  336. {
  337. return $this->calendar->nextMonth($format);
  338. }
  339. /**
  340. * Returns the value for the previous week
  341. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  342. * @return int e.g. 4 or Unix timestamp
  343. * @access public
  344. */
  345. function prevWeek($format = 'n_in_month')
  346. {
  347. if ( method_exists($this->calendar,'prevWeek') ) {
  348. return $this->calendar->prevWeek($format);
  349. } else {
  350. require_once 'PEAR.php';
  351. PEAR::raiseError(
  352. 'Cannot call prevWeek on Calendar object of type: '.
  353. get_class($this->calendar), 133, PEAR_ERROR_TRIGGER,
  354. E_USER_NOTICE, 'Calendar_Decorator::prevWeek()');
  355. return false;
  356. }
  357. }
  358. /**
  359. * Returns the value for this week
  360. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  361. * @return int e.g. 5 or timestamp
  362. * @access public
  363. */
  364. function thisWeek($format = 'n_in_month')
  365. {
  366. if ( method_exists($this->calendar,'thisWeek') ) {
  367. return $this->calendar->thisWeek($format);
  368. } else {
  369. require_once 'PEAR.php';
  370. PEAR::raiseError(
  371. 'Cannot call thisWeek on Calendar object of type: '.
  372. get_class($this->calendar), 133, PEAR_ERROR_TRIGGER,
  373. E_USER_NOTICE, 'Calendar_Decorator::thisWeek()');
  374. return false;
  375. }
  376. }
  377. /**
  378. * Returns the value for next week
  379. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  380. * @return int e.g. 6 or timestamp
  381. * @access public
  382. */
  383. function nextWeek($format = 'n_in_month')
  384. {
  385. if ( method_exists($this->calendar,'nextWeek') ) {
  386. return $this->calendar->nextWeek($format);
  387. } else {
  388. require_once 'PEAR.php';
  389. PEAR::raiseError(
  390. 'Cannot call thisWeek on Calendar object of type: '.
  391. get_class($this->calendar), 133, PEAR_ERROR_TRIGGER,
  392. E_USER_NOTICE, 'Calendar_Decorator::nextWeek()');
  393. return false;
  394. }
  395. }
  396. /**
  397. * Returns the value for the previous day
  398. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  399. * @return int e.g. 10 or timestamp
  400. * @access public
  401. */
  402. function prevDay($format = 'int') {
  403. return $this->calendar->prevDay($format);
  404. }
  405. /**
  406. * Returns the value for this day
  407. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  408. * @return int e.g. 11 or timestamp
  409. * @access public
  410. */
  411. function thisDay($format = 'int')
  412. {
  413. return $this->calendar->thisDay($format);
  414. }
  415. /**
  416. * Returns the value for the next day
  417. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  418. * @return int e.g. 12 or timestamp
  419. * @access public
  420. */
  421. function nextDay($format = 'int')
  422. {
  423. return $this->calendar->nextDay($format);
  424. }
  425. /**
  426. * Returns the value for the previous hour
  427. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  428. * @return int e.g. 13 or timestamp
  429. * @access public
  430. */
  431. function prevHour($format = 'int')
  432. {
  433. return $this->calendar->prevHour($format);
  434. }
  435. /**
  436. * Returns the value for this hour
  437. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  438. * @return int e.g. 14 or timestamp
  439. * @access public
  440. */
  441. function thisHour($format = 'int')
  442. {
  443. return $this->calendar->thisHour($format);
  444. }
  445. /**
  446. * Returns the value for the next hour
  447. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  448. * @return int e.g. 14 or timestamp
  449. * @access public
  450. */
  451. function nextHour($format = 'int')
  452. {
  453. return $this->calendar->nextHour($format);
  454. }
  455. /**
  456. * Returns the value for the previous minute
  457. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  458. * @return int e.g. 23 or timestamp
  459. * @access public
  460. */
  461. function prevMinute($format = 'int')
  462. {
  463. return $this->calendar->prevMinute($format);
  464. }
  465. /**
  466. * Returns the value for this minute
  467. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  468. * @return int e.g. 24 or timestamp
  469. * @access public
  470. */
  471. function thisMinute($format = 'int')
  472. {
  473. return $this->calendar->thisMinute($format);
  474. }
  475. /**
  476. * Returns the value for the next minute
  477. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  478. * @return int e.g. 25 or timestamp
  479. * @access public
  480. */
  481. function nextMinute($format = 'int')
  482. {
  483. return $this->calendar->nextMinute($format);
  484. }
  485. /**
  486. * Returns the value for the previous second
  487. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  488. * @return int e.g. 43 or timestamp
  489. * @access public
  490. */
  491. function prevSecond($format = 'int')
  492. {
  493. return $this->calendar->prevSecond($format);
  494. }
  495. /**
  496. * Returns the value for this second
  497. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  498. * @return int e.g. 44 or timestamp
  499. * @access public
  500. */
  501. function thisSecond($format = 'int')
  502. {
  503. return $this->calendar->thisSecond($format);
  504. }
  505. /**
  506. * Returns the value for the next second
  507. * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
  508. * @return int e.g. 45 or timestamp
  509. * @access public
  510. */
  511. function nextSecond($format = 'int')
  512. {
  513. return $this->calendar->nextSecond($format);
  514. }
  515. }
  516. ?>