-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutable.hh
191 lines (167 loc) · 3.59 KB
/
Mutable.hh
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Common;
use Titon\Utility\Col;
/**
* The Mutable trait allows for a set of data to be read and written to.
* It also provides methods for iteration and counting.
* This trait is useful when an object should represent a set of data or an entity.
*
* @package Titon\Common
*/
trait Mutable<Tk, Tv> {
/**
* Mapped data.
*
* @var Map<Tk, Tv>
*/
protected Map<Tk, Tv> $data = Map {};
/**
* Magic method for get().
*
* @param Tk $key
* @return Tv
*/
public function __get(Tk $key): ?Tv {
return $this->get($key);
}
/**
* Magic method for set().
*
* @param Tk $key
* @param Tv $value
*/
public function __set(Tk $key, Tv $value): void {
$this->set($key, $value);
}
/**
* Magic method for has().
*
* @param Tk $key
* @return bool
*/
public function __isset(Tk $key): bool {
return $this->has($key);
}
/**
* Magic method for remove().
*
* @param Tk $key
*/
public function __unset(Tk $key): void {
$this->remove($key);
}
/**
* Add multiple parameters.
*
* @param Map<Tk, Tv> $params
* @return $this
*/
public function add(Map<Tk, Tv> $params): this {
foreach ($params as $key => $value) {
$this->set($key, $value);
}
return $this;
}
/**
* Return all parameters.
*
* @return Map<Tk, Tv>
*/
public function all(): Map<Tk, Tv> {
return $this->data;
}
/**
* Reset all data.
*
* @return $this
*/
public function flush(): this {
$this->all()->clear();
return $this;
}
/**
* Return a parameter by key.
*
* @uses Titon\Utility\Col
*
* @param Tk $key
* @param Tv $default
* @return Tv
*/
public function get(Tk $key, ?Tv $default = null): ?Tv {
return Col::get($this->all(), $key, $default);
}
/**
* Return an iterator.
*
* @return Iterator<Tv>
*/
public function getIterator(): Iterator<Tv> {
return $this->all()->getIterator();
}
/**
* Check if a parameter exists.
*
* @uses Titon\Utility\Col
*
* @param Tk $key
* @return bool
*/
public function has(Tk $key): bool {
return Col::has($this->all(), $key);
}
/**
* Return all the parameter keys.
*
* @return Vector<Tk>
*/
public function keys(): Vector<Tk> {
return $this->all()->keys();
}
/**
* Remove a parameter by key.
*
* @uses Titon\Utility\Col
*
* @param Tk $key
* @return $this
*/
public function remove(Tk $key): this {
Col::remove($this->all(), $key);
return $this;
}
/**
* Set a parameter value by key.
*
* @uses Titon\Utility\Col
*
* @param Tk $key
* @param Tv $value
* @return $this
*/
public function set(Tk $key, Tv $value): this {
Col::set($this->all(), $key, $value);
return $this;
}
/**
* Return all the parameter values.
*
* @return Vector<Tv>
*/
public function values(): Vector<Tv> {
return $this->all()->values();
}
/**
* Return the count of the collection.
*
* @return int
*/
public function count(): int {
return $this->all()->count();
}
}