-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathObjectArrayStorage.php
More file actions
107 lines (96 loc) · 2.36 KB
/
ObjectArrayStorage.php
File metadata and controls
107 lines (96 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
declare(strict_types=1);
namespace Brick\Std;
/**
* Associates an array of values to an object.
*/
class ObjectArrayStorage implements \Countable, \IteratorAggregate
{
/**
* The underlying storage.
*
* @var ObjectStorage
*/
private $storage;
/**
* Class constructor.
*/
public function __construct()
{
$this->storage = new ObjectStorage();
}
/**
* Returns whether this storage contains the given object.
*
* @param object $object The object to test.
*
* @return bool True if this storage contains the object, false otherwise.
*/
public function has($object) : bool
{
return $this->storage->has($object);
}
/**
* Returns the values associated to the given object.
*
* If the object is not present in the storage, an empty array is returned.
*
* @param object $object The object.
*
* @return array The values associated with the object.
*/
public function get($object) : array
{
$values = $this->storage->get($object);
return isset($values) ? $values : [];
}
/**
* Adds data associated with the given object.
*
* @param object $object The object.
* @param mixed $value The value to add.
*
* @return void
*/
public function add($object, $value) : void
{
$values = $this->get($object);
$values[] = $value;
$this->storage->set($object, $values);
}
/**
* Removes all values associated with the given object from the storage.
*
* If this storage does not any value for the given object, this method does nothing.
*
* @param object $object The object to remove.
*
* @return void
*/
public function remove($object) : void
{
$this->storage->remove($object);
}
/**
* Returns the number of objects in this storage.
*
* This method is part of the Countable interface.
*
* @return int
*/
public function count() : int
{
return $this->storage->count();
}
/**
* Returns an iterator for this storage.
*
* This method is part of the IteratorAggregate interface.
*
* @return \Traversable
*/
public function getIterator() : \Traversable
{
return $this->storage->getIterator();
}
}