root/trunk/src/main/php/net/stubbles/util/datespan/stubDateSpanCustom.php

Revision 1541, 4.8 kB (checked in by mikey, 3 months ago)

remove reflection dependency

Line 
1 <?php
2 /**
3  * Datespan with a custom start and end date.
4  *
5  * @author      Ingo Sobolewski <ingo.sobolewski@1und1.de>
6  * @author      Frank Kleine <frank.kleine@1und1.de>
7  * @package     stubbles
8  * @subpackage  util_datespan
9  */
10 stubClassLoader::load('net::stubbles::util::datespan::stubDateSpan');
11 /**
12  * Datespan with a custom start and end date.
13  *
14  * @package     stubbles
15  * @subpackage  util_datespan
16  */
17 class stubDateSpanCustom extends stubSerializableObject implements stubDateSpan
18 {
19     /**
20      * start date of the span
21      *
22      * @var  DateTime
23      * @see  http://php.net/manual/en/function.date-create.php
24      */
25     protected $from;
26     /**
27      * end date of the span
28      *
29      * @var  DateTime
30      * @see  http://php.net/manual/en/function.date-create.php
31      */
32     protected $to;
33     /**
34      * The interval of the span (e.g. day, week, month)
35      *
36      * @var  string
37      * @see  DateSpan::INTERVAL_*
38      */
39     protected $interval;
40
41     /**
42      * constructor
43      *
44      * @param  string|DateTime  $from      start date of the span
45      * @param  string|DateTime  $to        end date of the span
46      * @param  string           $interval  optional  interval of the span
47      */
48     public function __construct($from, $to, $interval = stubDateSpan::INTERVAL_DAY)
49     {
50         if (($from instanceof DateTime) == false) {
51             $from = new DateTime($from);
52         }
53         
54         if (($to instanceof DateTime) == false) {
55             $to = new DateTime($to);
56         }
57         
58         $this->from     = $from;
59         $this->to       = $to;
60         $this->interval = $interval;
61     }
62
63     /**
64      * returns the start date
65      *
66      * @return  DateTime
67      * @see     http://php.net/manual/en/function.date-create.php
68      */
69     public function getStartDate()
70     {
71         return $this->from;
72     }
73
74     /**
75      * returns the end date
76      *
77      * @return  DateTime
78      * @see     http://php.net/manual/en/function.date-create.php
79      */
80     public function getEndDate()
81     {
82         return $this->to;
83     }
84
85     /**
86      * returns the spans between the start date and the end date
87      *
88      * @return  array<stubDateSpan>
89      */
90     public function getDateSpans()
91     {
92         $spans = array();
93         switch ($this->interval) {
94             case stubDateSpan::INTERVAL_DAY:
95                 stubClassLoader::load('net::stubbles::util::datespan::stubDateSpanDay');
96                 $day   = clone $this->from;
97                 $end   = $this->to->format('U');
98                 while ($day->format('U') <= $end) {
99                     $spans[] = new stubDateSpanDay(clone $day);
100                     $day->modify('+1 day');
101                 }
102                 break;
103         
104             case stubDateSpan::INTERVAL_WEEK:
105                 stubClassLoader::load('net::stubbles::util::datespan::stubDateSpanWeek');
106                 $day   = clone $this->from;
107                 $end   = $this->to->format('U');
108                 while ($day->format('U') <= $end) {
109                     $spans[] = new stubDateSpanWeek(clone $day);
110                     $day->modify('+7 days');
111                 }
112                 break;
113                 
114             default:
115                 // intentionally empty
116         }
117         
118         return $spans;
119     }
120
121     /**
122      * returns a string representation of the date object
123      *
124      * @return  string
125      */
126     public function toString()
127     {
128         return $this->from->format('d.m.Y') . ' bis ' . $this->to->format('d.m.Y');
129     }
130
131     /**
132      * checks whether the DateSpan is in the future
133      *
134      * @return  bool
135      */
136     public function isFuture()
137     {
138         $today = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
139         if ($this->from->format('U') > $today) {
140             return true;
141         }
142         
143         return false;
144     }
145
146     /**
147      * takes care of serializing the value
148      *
149      * @param  array   &$propertiesToSerialize  list of properties to serialize
150      * @param  string  $name                    name of the property to serialize
151      * @param  mixed   $value                   value to serialize
152      */
153     protected function __doSerialize(&$propertiesToSerialize, $name, $value)
154     {
155         if ('from' == $name || 'to' == $name) {
156             $this->_serializedProperties[$name] = $value->format('c');
157             return;
158         }
159         
160         parent::__doSerialize($propertiesToSerialize, $name, $value);
161     }
162
163     /**
164      * takes care of unserializing the value
165      *
166      * @param  string  $name             name of the property
167      * @param  mixed   $serializedValue  value of the property
168      */
169     protected function __doUnserialize($name, $serializedValue)
170     {
171         if ('from' == $name || 'to' == $name) {
172             $this->$name = new DateTime($serializedValue);
173             return;
174         }
175         
176         parent::__doUnserialize($name, $serializedValue);
177     }
178 }
179 ?>
Note: See TracBrowser for help on using the browser.