1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of CalendR, a Fréquence web project. |
5
|
|
|
* |
6
|
|
|
* (c) 2012 Fréquence web |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CalendR\Event\Collection; |
13
|
|
|
|
14
|
|
|
use CalendR\Event\EventInterface; |
15
|
|
|
use CalendR\Period\PeriodInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This class store event by indexing them with a given index pattern. |
19
|
|
|
* Index pattern is generated by an index function. |
20
|
|
|
* |
21
|
|
|
* @author Yohan Giarelli <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Indexed implements CollectionInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var EventInterface[][] |
27
|
|
|
*/ |
28
|
|
|
protected $events; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Event count. |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $count = 0; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The function used to index events. |
39
|
|
|
* Takes a \DateTime in parameter and must return an array index for this value. |
40
|
|
|
* |
41
|
|
|
* By default : |
42
|
|
|
* ```php |
43
|
|
|
* function(\DateTime $dateTime) { |
44
|
|
|
* return $dateTime->format('Y-m-d'); |
45
|
|
|
* } |
46
|
|
|
* ``` |
47
|
|
|
* |
48
|
|
|
* @var callable |
49
|
|
|
*/ |
50
|
|
|
protected $indexFunction; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array<EventInterface> $events |
54
|
|
|
* @param callable|null $callable |
55
|
|
|
*/ |
56
|
|
|
public function __construct(array $events = array(), $callable = null) |
57
|
|
|
{ |
58
|
|
|
if (is_callable($callable)) { |
59
|
|
|
$this->indexFunction = $callable; |
60
|
|
|
} else { |
61
|
|
|
$this->indexFunction = function (\DateTime $dateTime) { |
62
|
|
|
return $dateTime->format('Y-m-d'); |
63
|
|
|
}; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
foreach ($events as $event) { |
67
|
|
|
$this->add($event); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Adds an event to the collection. |
73
|
|
|
* |
74
|
|
|
* @param EventInterface $event |
75
|
|
|
*/ |
76
|
|
|
public function add(EventInterface $event) |
77
|
|
|
{ |
78
|
|
|
$index = $this->computeIndex($event); |
79
|
|
|
if (isset($this->events[$index])) { |
80
|
|
|
$this->events[$index][] = $event; |
81
|
|
|
} else { |
82
|
|
|
$this->events[$index] = array($event); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
++$this->count; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Removes an event from the collection. |
90
|
|
|
* |
91
|
|
|
* @param EventInterface $event |
92
|
|
|
*/ |
93
|
|
|
public function remove(EventInterface $event) |
94
|
|
|
{ |
95
|
|
|
$index = $this->computeIndex($event); |
96
|
|
|
if (isset($this->events[$index])) { |
97
|
|
|
foreach ($this->events[$index] as $key => $internalEvent) { |
98
|
|
|
if ($event->getUid() == $internalEvent->getUid()) { |
99
|
|
|
unset($this->events[$index][$key]); |
100
|
|
|
--$this->count; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns if we have events for the given index. |
108
|
|
|
* |
109
|
|
|
* @param mixed $index |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function has($index) |
114
|
|
|
{ |
115
|
|
|
return 0 < count($this->find($index)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* returns events. |
120
|
|
|
* |
121
|
|
|
* @param mixed $index |
122
|
|
|
* |
123
|
|
|
* @return EventInterface[] |
124
|
|
|
*/ |
125
|
|
|
public function find($index) |
126
|
|
|
{ |
127
|
|
|
if ($index instanceof PeriodInterface) { |
128
|
|
|
$index = $index->getBegin(); |
129
|
|
|
} |
130
|
|
|
if ($index instanceof \DateTime) { |
131
|
|
|
$index = $this->computeIndex($index); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return isset($this->events[$index]) ? $this->events[$index] : array(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Returns a flattened array of all events. |
139
|
|
|
* |
140
|
|
|
* @return EventInterface[] |
141
|
|
|
*/ |
142
|
|
|
public function all() |
143
|
|
|
{ |
144
|
|
|
$results = array(); |
145
|
|
|
|
146
|
|
|
foreach ($this->events as $events) { |
147
|
|
|
$results = array_merge($results, $events); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $results; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Computes event index. |
155
|
|
|
* |
156
|
|
|
* @param EventInterface|\DateTime $toCompute |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
protected function computeIndex($toCompute) |
161
|
|
|
{ |
162
|
|
|
if ($toCompute instanceof EventInterface) { |
163
|
|
|
$toCompute = $toCompute->getBegin(); |
164
|
|
|
} |
165
|
|
|
$function = $this->indexFunction; |
166
|
|
|
|
167
|
|
|
return $function($toCompute); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function count() |
174
|
|
|
{ |
175
|
|
|
return $this->count; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|