PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / config / abstract.php
vikappointments / site / helpers / libraries / config Last commit date
classes 3 days ago abstract.php 3 days ago index.html 3 days ago
abstract.php
236 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Utility class working with an abstract configuration.
16 *
17 * @method integer getInt() getInt($name, $default = null) Get a signed integer.
18 * @method integer getUint() getUint($name, $default = null) Get an unsigned integer.
19 * @method float getFloat() getFloat($name, $default = null) Get a floating-point number.
20 * @method float getDouble() getDouble($name, $default = null) Get a floating-point number.
21 * @method boolean getBool() getBool($name, $default = null) Get a boolean.
22 * @method string getString() getString($name, $default = null) Get a string.
23 * @method array getArray() getArray($name, $default = null) Decode a JSON string and get an array.
24 * @method mixed getObject() getObject($name, $default = null) Decode a JSON string and get an object.
25 * @method mixed getJson() getJson($name, $default = null) Decode a JSON string and get an object.
26 *
27 * @since 1.6
28 * @since 1.7 Renamed from UIConfig
29 */
30 abstract class VAPConfig
31 {
32 /**
33 * The map containing all the settings retrieved.
34 *
35 * @var array
36 */
37 private $pool = array();
38
39 /**
40 * An array of options.
41 *
42 * @var array
43 */
44 protected $options = array();
45
46 /**
47 * Class constructor.
48 *
49 * @param array $options An array of options.
50 */
51 public function __construct(array $options = array())
52 {
53 if (!isset($options['debug']))
54 {
55 $options['debug'] = false;
56 }
57
58 $this->options = $options;
59 }
60
61 /**
62 * Returns the value of the specified setting.
63 *
64 * @param string $key Name of the setting.
65 * @param mixed $default Default value in case the setting is empty.
66 * @param string $filter Filter to apply to the value (string by default).
67 *
68 * @return mixed The filtered value of the setting.
69 */
70 public function get($key, $default = null, $filter = 'string')
71 {
72 // if the setting is alread loaded
73 if (!array_key_exists($key, $this->pool))
74 {
75 // otherwise read it from the apposite handler
76 $value = $this->retrieve($key);
77
78 // if the returned value is false
79 if ($value === false)
80 {
81 // raise an exception if the setting doesn't exist (only in DEBUGGING mode)
82 if ($this->options['debug'])
83 {
84 throw new Exception(sprintf('The setting [%s] does not exist.', $k), 404);
85 }
86
87 // return the default specified value
88 return $default;
89 }
90
91 // register the value into the pool
92 $this->pool[$key] = $value;
93 }
94
95 /**
96 * Always filter the value.
97 *
98 * @since 1.7
99 */
100 return $this->_clean($this->pool[$key], $filter);
101 }
102
103 /**
104 * Magic method to get filtered input data.
105 *
106 * @param string $name The name of the function. The string next to "get" word will be used as filter.
107 * For example, getInt will use a "int" filter.
108 * @param array $arguments Array containing arguments to retrieve the setting.
109 * Contains name of the key and the default value.
110 *
111 * @return mixed The filtered value of the setting.
112 */
113 public function __call($name, $arguments)
114 {
115 // check if the method is prefixed with 'get' word
116 if (substr($name, 0, 3) == 'get')
117 {
118 $key = '';
119 $default = null;
120 $filter = substr($name, 3);
121
122 // check if setting key is set
123 if (isset($arguments[0]))
124 {
125 $key = $arguments[0];
126 }
127
128 // check if default value is set
129 if (isset($arguments[1]))
130 {
131 $default = $arguments[1];
132 }
133
134 return $this->get($key, $default, $filter);
135 }
136
137 throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
138 }
139
140 /**
141 * Custom filter implementation.
142 *
143 * @param mixed $value The value to clean.
144 * @param string $filter The type of the value.
145 *
146 * @return mixed The filtered value.
147 */
148 protected function _clean($value, $filter)
149 {
150 switch (strtolower($filter))
151 {
152 case 'int':
153 $value = intval($value);
154 break;
155
156 case 'uint':
157 $value = abs(intval($value));
158 break;
159
160 case 'float':
161 case 'double':
162 $value = floatval($value);
163 break;
164
165 case 'bool':
166 $value = (empty($value) === false);
167 break;
168
169 case 'array':
170 $value = (is_array($value) ? $value : (is_string($value) && strlen($value) ? (array) json_decode($value, true) : array()));
171 break;
172
173 case 'json':
174 case 'object':
175 $value = (is_object($value) ? $value : (is_string($value) && strlen($value) ? json_decode($value) : new stdClass));
176 break;
177
178 default:
179 $value = (string) $value;
180 }
181
182 return $value;
183 }
184
185 /**
186 * Retrieve the value of the setting from the instance in which it is stored.
187 *
188 * @param string $key The name of the setting.
189 *
190 * @return mixed The value of the setting if exists, otherwise false.
191 */
192 protected abstract function retrieve($key);
193
194 /**
195 * Store the value of the specified setting.
196 *
197 * @param string $key The name of the setting.
198 * @param mixed $val The value of the setting.
199 *
200 * @return self This object to support chaining.
201 */
202 public function set($key, $val)
203 {
204 // if the registration of the setting went fine
205 if ($this->register($key, $val))
206 {
207 // overwrite/push the value of the setting
208 $this->pool[$key] = $val;
209 }
210
211 return $this;
212 }
213
214 /**
215 * Checks if the specified property exists.
216 *
217 * @param string $key The name of the setting.
218 *
219 * @return boolean True if exists, otherwise false.
220 */
221 public function has($key)
222 {
223 return array_key_exists($key, $this->pool) || $this->retrieve($key) !== false;
224 }
225
226 /**
227 * Register the value of the setting into the instance in which should be stored.
228 *
229 * @param string $key The name of the setting.
230 * @param mixed $val The value of the setting.
231 *
232 * @return bool True in case of success, otherwise false.
233 */
234 protected abstract function register($key, $val);
235 }
236