Dataset   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 1
dl 0
loc 194
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addParam() 0 12 2
A addParams() 0 6 3
A set() 0 13 4
A setArray() 0 13 3
A cast() 0 13 4
A castArray() 0 13 4
A reset() 0 4 1
A get() 0 4 1
A getData() 0 4 1
A getDefaults() 0 4 1
A __set() 0 4 1
A __get() 0 4 1
A __isset() 0 4 1
1
<?php
2
3
/**
4
 * @package Cadmium\Framework\Dataset
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace {
11
12
	class Dataset {
13
14
		private $data = [], $defaults = [], $validators = [];
15
16
		/**
17
		 * Constructor
18
		 */
19
20
		public function __construct(array $data = []) {
21
22
			$this->addParams($data);
23
		}
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 {
36
37
			if ('' === $name) return $this;
38
39
			$this->data[$name] = $default; $this->defaults[$name] = $default;
40
41
			$this->validators[$name] = ($validator ?? Dataset\Validator::get($default));
42
43
			# ------------------------
44
45
			return $this;
46
		}
47
48
		/**
49
		 * Add multiple params
50
		 *
51
		 * @return Dataset : the current dataset object
52
		 */
53
54
		public function addParams(array $data) : Dataset {
55
56
			foreach ($data as $name => $value) if (is_scalar($name)) $this->addParam($name, $value);
57
58
			return $this;
59
		}
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) {
68
69
			if (!isset($this->validators[$name])) return null;
70
71
			try {
72
73
				if (null === ($value = $this->validators[$name]($value))) return false;
74
75
				else { $this->data[$name] = $value; return true; }
76
			}
77
78
			catch (TypeError $e) { return false; }
79
		}
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 {
88
89
			$setted = [];
90
91
			foreach ($data as $name => $value) {
92
93
				if (null !== ($value = $this->set($name, $value))) $setted[$name] = $value;
94
			}
95
96
			# ------------------------
97
98
			return $setted;
99
		}
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) {
108
109
			if (!isset($this->validators[$name])) return null;
110
111
			try {
112
113
				if (null !== ($value = $this->validators[$name]($value))) return $value;
114
115
				else return $this->data[$name];
116
			}
117
118
			catch (TypeError $e) { return $this->data[$name]; }
119
		}
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 {
128
129
			$casted = [];
130
131
			foreach ($data as $name => $value) {
132
133
				if (null !== ($value = $this->cast($name, $value))) $casted[$name] = $value;
134
			}
135
136
			# ------------------------
137
138
			return ($return_all ? array_replace($this->data, $casted) : $casted);
139
		}
140
141
		/**
142
		 * Reset all the values to their defaults
143
		 */
144
145
		public function reset() {
146
147
			$this->data = $this->defaults;
148
		}
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) {
157
158
			return ($this->data[$name] ?? null);
159
		}
160
161
		/**
162
		 * Get the array of params and their values
163
		 */
164
165
		public function getData() : array {
166
167
			return $this->data;
168
		}
169
170
		/**
171
		 * Get the array of params and their default values
172
		 */
173
174
		public function getDefaults() : array {
175
176
			return $this->defaults;
177
		}
178
179
		/**
180
		 * An alias for the set method
181
		 */
182
183
		public function __set(string $name, $value) {
184
185
			return $this->set($name, $value);
186
		}
187
188
		/**
189
		 * An alias for the get method
190
		 */
191
192
		public function __get(string $name) {
193
194
			return $this->get($name);
195
		}
196
197
		/**
198
		 * Check if a param exists
199
		 */
200
201
		 public function __isset(string $name) : bool {
202
203
 			return isset($this->data[$name]);
204
 		}
205
	}
206
}
207