Passed
Push — master ( 4b8f5b...7fa4c0 )
by Filippo
05:53 queued 03:53
created

MetaCollection::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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