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
|
|
|
* Basic event collection. |
19
|
|
|
* Juste stores event as an array, and iterate over the array for retrieving. |
20
|
|
|
* |
21
|
|
|
* @author Yohan Giarelli <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Basic implements CollectionInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The events. |
27
|
|
|
* |
28
|
|
|
* @var EventInterface[] |
29
|
|
|
*/ |
30
|
|
|
protected $events; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param EventInterface[] $events |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $events = array()) |
36
|
|
|
{ |
37
|
|
|
$this->events = $events; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Adds an event to the collection. |
42
|
|
|
* |
43
|
|
|
* @param EventInterface $event |
44
|
|
|
*/ |
45
|
|
|
public function add(EventInterface $event) |
46
|
|
|
{ |
47
|
|
|
$this->events[] = $event; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Removes an event from the collection. |
52
|
|
|
* |
53
|
|
|
* @param EventInterface $event |
54
|
|
|
*/ |
55
|
|
|
public function remove(EventInterface $event) |
56
|
|
|
{ |
57
|
|
|
foreach ($this->events as $key => $internalEvent) { |
58
|
|
|
if ($event->getUid() === $internalEvent->getUid()) { |
59
|
|
|
unset($this->events[$key]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return all events;. |
66
|
|
|
* |
67
|
|
|
* @return EventInterface[] |
68
|
|
|
*/ |
69
|
|
|
public function all() |
70
|
|
|
{ |
71
|
|
|
return $this->events; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns if there is events corresponding to $index period. |
76
|
|
|
* |
77
|
|
|
* @param mixed $index |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function has($index) |
82
|
|
|
{ |
83
|
|
|
return count($this->find($index)) > 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Find events in the collection. |
88
|
|
|
* |
89
|
|
|
* @param mixed $index |
90
|
|
|
* |
91
|
|
|
* @return EventInterface[] |
92
|
|
|
*/ |
93
|
|
|
public function find($index) |
94
|
|
|
{ |
95
|
|
|
$result = array(); |
96
|
|
|
foreach ($this->events as $event) { |
97
|
|
|
if ($index instanceof PeriodInterface && $index->containsEvent($event)) { |
98
|
|
|
$result[] = $event; |
99
|
|
|
} elseif ($index instanceof \DateTime && $event->contains($index)) { |
100
|
|
|
$result[] = $event; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $result; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
|
|
public function count() |
111
|
|
|
{ |
112
|
|
|
return count($this->events); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|