1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file MetaCollection.php |
5
|
|
|
* @brief This file contains the MetaCollection class. |
6
|
|
|
* @details |
7
|
|
|
* @author Filippo F. Fadda |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Meta; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use Meta\Extension\TProperty; |
15
|
|
|
|
16
|
|
|
use ArrayIterator; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @brief This class is used to represent a generic collection. |
21
|
|
|
* @details This class implements `IteratorAggregate`, `Countable`, and `ArrayAccess`. |
22
|
|
|
* @nosubgrouping |
23
|
|
|
*/ |
24
|
|
|
abstract class MetaCollection implements \IteratorAggregate, \Countable, \ArrayAccess { |
25
|
|
|
use TProperty; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array $meta |
29
|
|
|
*/ |
30
|
|
|
protected $meta; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string $name |
35
|
|
|
*/ |
36
|
|
|
protected $name; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @brief Creates a new collection of items. |
41
|
|
|
* @param string $name Collection's name. |
42
|
|
|
* @param array &$meta Array of metadata. |
43
|
|
|
*/ |
44
|
|
|
public function __construct($name, array &$meta) { |
45
|
|
|
$this->name = $name; |
46
|
|
|
$this->meta = &$meta; |
47
|
|
|
$this->meta[$name] = []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @brief Removes all items from the collection. |
53
|
|
|
*/ |
54
|
|
|
public function reset() { |
55
|
|
|
unset($this->meta[$this->name]); |
56
|
|
|
$this->meta[$this->name] = []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @brief Returns the collection as a real array. |
62
|
|
|
* @return array An associative array using as keys the e-mail addresses, and as values if the address are verified or |
63
|
|
|
* not. |
64
|
|
|
*/ |
65
|
|
|
public function asArray() { |
66
|
|
|
return $this->meta[$this->name]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @brief Returns an external iterator. |
72
|
|
|
* @return ArrayIterator |
73
|
|
|
*/ |
74
|
|
|
public function getIterator() { |
75
|
|
|
return new ArrayIterator($this->meta[$this->name]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @brief Returns the number of documents found. |
81
|
|
|
* @return integer Number of documents. |
82
|
|
|
*/ |
83
|
|
|
public function count() { |
84
|
|
|
return count($this->meta[$this->name]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @brief Returns `true` in case there aren't items inside the collection, `false` otherwise. |
90
|
|
|
* @details Since the PHP core developers are noobs, `empty()` cannot be used on any class that implements ArrayAccess. |
91
|
|
|
* @attention This method must be used in place of `empty()`. |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function isEmpty() { |
95
|
|
|
return empty($this->meta[$this->name]) ? TRUE : FALSE; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @brief Whether or not an offset exists. |
101
|
|
|
* @details This method is executed when using `isset()` or `empty()` on objects implementing ArrayAccess. |
102
|
|
|
* @param integer $offset An offset to check for. |
103
|
|
|
* @return bool Returns `true` on success or `false` on failure. |
104
|
|
|
*/ |
105
|
|
|
public function offsetExists($offset) { |
106
|
|
|
return isset($this->meta[$this->name][$offset]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @brief Returns the value at specified offset. |
112
|
|
|
* @details This method is executed when checking if offset is `empty()`. |
113
|
|
|
* @param integer $offset The offset to retrieve. |
114
|
|
|
* @return mixed Can return all value types. |
115
|
|
|
*/ |
116
|
|
|
public function offsetGet($offset) { |
117
|
|
|
return $this->meta[$this->name][$offset]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
//! @cond HIDDEN_SYMBOLS |
122
|
|
|
|
123
|
|
|
public function offsetSet($offset, $value) { |
124
|
|
|
throw new \BadMethodCallException("Collection is immutable and cannot be changed."); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
public function offsetUnset($offset) { |
129
|
|
|
throw new \BadMethodCallException("Collection is immutable and cannot be changed."); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
//! @endcond |
133
|
|
|
|
134
|
|
|
} |