1 | <?php |
||
12 | class Dataset { |
||
13 | |||
14 | private $data = [], $defaults = [], $validators = []; |
||
15 | |||
16 | /** |
||
17 | * Constructor |
||
18 | */ |
||
19 | |||
20 | public function __construct(array $data = []) { |
||
24 | |||
25 | /** |
||
26 | * Create a param with a given name, a default value, and a validator function. |
||
27 | * Every time you update the param, a given value is being passed to the validator as an argument. |
||
28 | * The returned value is being appended to the dataset. If the validator returns null, the dataset is not being affected. |
||
29 | * If a validator was not given, a default validator will be used to convert a given variable type to a default's type. |
||
30 | * In a case the conversion fails an existing value will not be changed |
||
31 | * |
||
32 | * @return Dataset : the current dataset object |
||
33 | */ |
||
34 | |||
35 | public function addParam(string $name, $default, callable $validator = null) : Dataset { |
||
47 | |||
48 | /** |
||
49 | * Add multiple params |
||
50 | * |
||
51 | * @return Dataset : the current dataset object |
||
52 | */ |
||
53 | |||
54 | public function addParams(array $data) : Dataset { |
||
60 | |||
61 | /** |
||
62 | * Set a param value |
||
63 | * |
||
64 | * @return bool|null : true on success, false on error, or null if the param does not exist |
||
65 | */ |
||
66 | |||
67 | public function set(string $name, $value) { |
||
80 | |||
81 | /** |
||
82 | * Set multiple values |
||
83 | * |
||
84 | * @return array : the array of set results for every param (true on success or false on error) |
||
85 | */ |
||
86 | |||
87 | public function setArray(array $data) : array { |
||
100 | |||
101 | /** |
||
102 | * Validate and return a value without affecting the dataset |
||
103 | * |
||
104 | * @return mixed|null : the validated value or null if the param does not exist |
||
105 | */ |
||
106 | |||
107 | public function cast(string $name, $value) { |
||
120 | |||
121 | /** |
||
122 | * Validate and return multiple values without affecting the dataset |
||
123 | * |
||
124 | * @return array : the array of validated values or the array of all values if $return_all is true |
||
125 | */ |
||
126 | |||
127 | public function castArray(array $data, bool $return_all = false) : array { |
||
140 | |||
141 | /** |
||
142 | * Reset all the values to their defaults |
||
143 | */ |
||
144 | |||
145 | public function reset() { |
||
149 | |||
150 | /** |
||
151 | * Get a param value |
||
152 | * |
||
153 | * @return mixed|null : the value or null if the param does not exist |
||
154 | */ |
||
155 | |||
156 | public function get(string $name) { |
||
160 | |||
161 | /** |
||
162 | * Get the array of params and their values |
||
163 | */ |
||
164 | |||
165 | public function getData() : array { |
||
169 | |||
170 | /** |
||
171 | * Get the array of params and their default values |
||
172 | */ |
||
173 | |||
174 | public function getDefaults() : array { |
||
178 | |||
179 | /** |
||
180 | * An alias for the set method |
||
181 | */ |
||
182 | |||
183 | public function __set(string $name, $value) { |
||
187 | |||
188 | /** |
||
189 | * An alias for the get method |
||
190 | */ |
||
191 | |||
192 | public function __get(string $name) { |
||
196 | |||
197 | /** |
||
198 | * Check if a param exists |
||
199 | */ |
||
200 | |||
201 | public function __isset(string $name) : bool { |
||
205 | } |
||
206 | } |
||
207 |