| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 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.5 |
| 28 |
*/ |
| 29 |
abstract class VBOConfigRegistry |
| 30 |
{ |
| 31 |
/** |
| 32 |
* The map containing all the settings retrieved. |
| 33 |
* |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
private $pool = []; |
| 37 |
|
| 38 |
/** |
| 39 |
* An array of options. |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
protected $options; |
| 44 |
|
| 45 |
/** |
| 46 |
* Class constructor. |
| 47 |
* |
| 48 |
* @param array $options An array of options. |
| 49 |
*/ |
| 50 |
public function __construct(array $options = []) |
| 51 |
{ |
| 52 |
if (!isset($options['debug'])) |
| 53 |
{ |
| 54 |
$options['debug'] = false; |
| 55 |
} |
| 56 |
|
| 57 |
$this->options = $options; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the value of the specified setting. |
| 62 |
* |
| 63 |
* @param string $key Name of the setting. |
| 64 |
* @param mixed $default Default value in case the setting is empty. |
| 65 |
* @param string $filter Filter to apply to the value (string by default). |
| 66 |
* |
| 67 |
* @return mixed The filtered value of the setting. |
| 68 |
*/ |
| 69 |
final public function get($key, $default = null, $filter = 'string') |
| 70 |
{ |
| 71 |
// if the setting is alread loaded |
| 72 |
if (!array_key_exists($key, $this->pool)) |
| 73 |
{ |
| 74 |
// otherwise read it from the apposite handler |
| 75 |
$value = $this->retrieve($key); |
| 76 |
|
| 77 |
// if the returned value is false |
| 78 |
if ($value === false) |
| 79 |
{ |
| 80 |
// raise an exception if the setting doesn't exist (only in DEBUGGING mode) |
| 81 |
if ($this->options['debug']) |
| 82 |
{ |
| 83 |
throw new Exception(sprintf('The setting [%s] does not exist.', $k), 404); |
| 84 |
} |
| 85 |
|
| 86 |
// return the default specified value |
| 87 |
return $default; |
| 88 |
} |
| 89 |
|
| 90 |
// register the value into the pool |
| 91 |
$this->pool[$key] = $value; |
| 92 |
} |
| 93 |
|
| 94 |
// always filter the value. |
| 95 |
return $this->clean($this->pool[$key], $filter); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Magic method to get filtered input data. |
| 100 |
* |
| 101 |
* @param string $name The name of the function. The string next to "get" word will be used as filter. |
| 102 |
* For example, getInt will use a "int" filter. |
| 103 |
* @param array $arguments Array containing arguments to retrieve the setting. |
| 104 |
* Contains name of the key and the default value. |
| 105 |
* |
| 106 |
* @return mixed The filtered value of the setting. |
| 107 |
*/ |
| 108 |
public function __call($name, $arguments) |
| 109 |
{ |
| 110 |
// check if the method is prefixed with 'get' word |
| 111 |
if (substr($name, 0, 3) == 'get') |
| 112 |
{ |
| 113 |
$key = ''; |
| 114 |
$default = null; |
| 115 |
$filter = substr($name, 3); |
| 116 |
|
| 117 |
// check if setting key is set |
| 118 |
if (isset($arguments[0])) |
| 119 |
{ |
| 120 |
$key = $arguments[0]; |
| 121 |
} |
| 122 |
|
| 123 |
// check if default value is set |
| 124 |
if (isset($arguments[1])) |
| 125 |
{ |
| 126 |
$default = $arguments[1]; |
| 127 |
} |
| 128 |
|
| 129 |
return $this->get($key, $default, $filter); |
| 130 |
} |
| 131 |
|
| 132 |
throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()'); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Custom filter implementation. |
| 137 |
* |
| 138 |
* @param mixed $value The value to clean. |
| 139 |
* @param string $filter The type of the value. |
| 140 |
* |
| 141 |
* @return mixed The filtered value. |
| 142 |
*/ |
| 143 |
protected function clean($value, $filter) |
| 144 |
{ |
| 145 |
switch (strtolower($filter)) |
| 146 |
{ |
| 147 |
case 'int': |
| 148 |
$value = intval($value); |
| 149 |
break; |
| 150 |
|
| 151 |
case 'uint': |
| 152 |
$value = abs(intval($value)); |
| 153 |
break; |
| 154 |
|
| 155 |
case 'float': |
| 156 |
case 'double': |
| 157 |
$value = floatval($value); |
| 158 |
break; |
| 159 |
|
| 160 |
case 'bool': |
| 161 |
$value = (empty($value) === false); |
| 162 |
break; |
| 163 |
|
| 164 |
case 'array': |
| 165 |
$value = (is_array($value) ? $value : (is_string($value) && strlen($value) ? (array) json_decode($value, true) : array())); |
| 166 |
break; |
| 167 |
|
| 168 |
case 'json': |
| 169 |
case 'object': |
| 170 |
$value = (is_object($value) ? $value : (is_string($value) && strlen($value) ? json_decode($value) : new stdClass)); |
| 171 |
break; |
| 172 |
|
| 173 |
default: |
| 174 |
$value = (string) $value; |
| 175 |
} |
| 176 |
|
| 177 |
return $value; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Store the value of the specified setting. |
| 182 |
* |
| 183 |
* @param string $key The name of the setting. |
| 184 |
* @param mixed $val The value of the setting. |
| 185 |
* |
| 186 |
* @return self This object to support chaining. |
| 187 |
*/ |
| 188 |
final public function set($key, $val) |
| 189 |
{ |
| 190 |
// if the registration of the setting went fine |
| 191 |
if ($this->register($key, $val)) |
| 192 |
{ |
| 193 |
// overwrite/push the value of the setting |
| 194 |
$this->pool[$key] = $val; |
| 195 |
} |
| 196 |
|
| 197 |
return $this; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Checks if the specified property exists. |
| 202 |
* |
| 203 |
* @param string $key The name of the setting. |
| 204 |
* |
| 205 |
* @return boolean True if exists, otherwise false. |
| 206 |
*/ |
| 207 |
final public function has($key) |
| 208 |
{ |
| 209 |
return array_key_exists($key, $this->pool) || $this->retrieve($key) !== false; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Removes the specified property. |
| 214 |
* |
| 215 |
* @param string $key The name of the setting. |
| 216 |
* |
| 217 |
* @return self This object to support chaining. |
| 218 |
* |
| 219 |
* @since 1.16.0 (J) - 1.6.0 (WP) |
| 220 |
*/ |
| 221 |
final public function remove($key) |
| 222 |
{ |
| 223 |
// if the deletion of the setting went fine |
| 224 |
if ($this->delete($key)) |
| 225 |
{ |
| 226 |
// unset the value of the setting |
| 227 |
unset($this->pool[$key]); |
| 228 |
} |
| 229 |
|
| 230 |
return $this; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Retrieve the value of the setting from the instance in which it is stored. |
| 235 |
* |
| 236 |
* @param string $key The name of the setting. |
| 237 |
* |
| 238 |
* @return mixed The value of the setting if exists, otherwise false. |
| 239 |
*/ |
| 240 |
abstract protected function retrieve($key); |
| 241 |
|
| 242 |
/** |
| 243 |
* Register the value of the setting into the instance in which should be stored. |
| 244 |
* |
| 245 |
* @param string $key The name of the setting. |
| 246 |
* @param mixed $val The value of the setting. |
| 247 |
* |
| 248 |
* @return boolean True in case of success, otherwise false. |
| 249 |
*/ |
| 250 |
abstract protected function register($key, $val); |
| 251 |
|
| 252 |
/** |
| 253 |
* Delete the setting from the instance where it's stored. |
| 254 |
* |
| 255 |
* @param string $key The name of the setting. |
| 256 |
* |
| 257 |
* @return boolean True in case of success, otherwise false. |
| 258 |
* |
| 259 |
* @since 1.16.0 (J) - 1.6.0 (WP) |
| 260 |
*/ |
| 261 |
abstract protected function delete($key); |
| 262 |
} |
| 263 |
|