1 | <?php |
||
21 | abstract class PeriodAbstract implements PeriodInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var \DateTime |
||
25 | */ |
||
26 | protected $begin; |
||
27 | |||
28 | /** |
||
29 | * @var \DateTime |
||
30 | */ |
||
31 | protected $end; |
||
32 | |||
33 | /** |
||
34 | * @var FactoryInterface |
||
35 | */ |
||
36 | protected $factory; |
||
37 | |||
38 | /** |
||
39 | * @param \DateTime $begin |
||
40 | * @param FactoryInterface $factory |
||
41 | * |
||
42 | * @throws \CalendR\Exception |
||
43 | */ |
||
44 | public function __construct(\DateTime $begin, FactoryInterface $factory) |
||
45 | { |
||
46 | $this->factory = $factory; |
||
47 | if (!static::isValid($begin)) { |
||
48 | throw $this->createInvalidException(); |
||
49 | } |
||
50 | |||
51 | $this->begin = clone $begin; |
||
52 | $this->end = clone $begin; |
||
53 | $this->end->add($this->getDateInterval()); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Checks if the given period is contained in the current period. |
||
58 | * |
||
59 | * @param \DateTime $date |
||
60 | * |
||
61 | * @return bool true if the period contains this date |
||
62 | */ |
||
63 | public function contains(\DateTime $date) |
||
67 | |||
68 | /** |
||
69 | * Checks if a period is equals to an other. |
||
70 | * |
||
71 | * @param PeriodInterface $period |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | public function equals(PeriodInterface $period) |
||
82 | |||
83 | /** |
||
84 | * Returns true if the period include the other period |
||
85 | * given as argument. |
||
86 | * |
||
87 | * @param PeriodInterface $period |
||
88 | * @param bool $strict |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | public function includes(PeriodInterface $period, $strict = true) |
||
105 | |||
106 | /** |
||
107 | * Returns if $event is during this period. |
||
108 | * Non strict. Must return true if : |
||
109 | * * Event is during period |
||
110 | * * Period is during event |
||
111 | * * Event begin is during Period |
||
112 | * * Event end is during Period. |
||
113 | * |
||
114 | * @param EventInterface $event |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function containsEvent(EventInterface $event) |
||
127 | |||
128 | /** |
||
129 | * Format the period to a string. |
||
130 | * |
||
131 | * @param string $format |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | public function format($format) |
||
139 | |||
140 | /** |
||
141 | * Returns if the current period is the current one. |
||
142 | * |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function isCurrent() |
||
149 | |||
150 | /** |
||
151 | * Gets the next period of the same type. |
||
152 | * |
||
153 | * @return PeriodInterface |
||
154 | */ |
||
155 | public function getNext() |
||
159 | |||
160 | /** |
||
161 | * Gets the previous period of the same type. |
||
162 | * |
||
163 | * @return PeriodInterface |
||
164 | */ |
||
165 | public function getPrevious() |
||
172 | |||
173 | /** |
||
174 | * @return \DateTime |
||
175 | */ |
||
176 | public function getBegin() |
||
180 | |||
181 | /** |
||
182 | * @return \DateTime |
||
183 | */ |
||
184 | public function getEnd() |
||
188 | |||
189 | /** |
||
190 | * @return FactoryInterface |
||
191 | */ |
||
192 | public function getFactory() |
||
200 | |||
201 | /** |
||
202 | * @return \CalendR\Exception |
||
203 | */ |
||
204 | protected function createInvalidException() |
||
210 | } |
||
211 |