| @@ -1,0 +1,244 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.application | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 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 | + * Object adapter class. | |
| 16 | + * This class handles simple and smart functions for objects. | |
| 17 | + * | |
| 18 | + * @since 10.0 | |
| 19 | + */ | |
| 20 | +#[\AllowDynamicProperties] | |
| 21 | +class JObject | |
| 22 | +{ | |
| 23 | + /** | |
| 24 | + * An array of error messages or Exception objects. | |
| 25 | + * | |
| 26 | + * @var array | |
| 27 | + */ | |
| 28 | + protected $_errors = []; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Class constructor, overridden in descendant classes. | |
| 32 | + * | |
| 33 | + * @param mixed $properties Either an associative array or another | |
| 34 | + * object to set the initial properties of the object. | |
| 35 | + */ | |
| 36 | + public function __construct($properties = null) | |
| 37 | + { | |
| 38 | + if ($properties !== null) | |
| 39 | + { | |
| 40 | + $this->setProperties($properties); | |
| 41 | + } | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Sets a default value if not already assigned | |
| 46 | + * | |
| 47 | + * @param string $property The name of the property. | |
| 48 | + * @param mixed $default The default value. | |
| 49 | + * | |
| 50 | + * @return mixed Previous value of the property. | |
| 51 | + * | |
| 52 | + * @uses get() | |
| 53 | + * @uses set() | |
| 54 | + */ | |
| 55 | + public function def($property, $default = null) | |
| 56 | + { | |
| 57 | + $value = $this->get($property, $default); | |
| 58 | + | |
| 59 | + return $this->set($property, $value); | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * Returns a property of the object or the default value if the property is not set. | |
| 64 | + * | |
| 65 | + * @param string $property The name of the property. | |
| 66 | + * @param mixed $default The default value. | |
| 67 | + * | |
| 68 | + * @return mixed The value of the property. | |
| 69 | + */ | |
| 70 | + public function get($property, $default = null) | |
| 71 | + { | |
| 72 | + /** | |
| 73 | + * Return default value also in case the property is set | |
| 74 | + * and owns a NULL value or an empty string. | |
| 75 | + * | |
| 76 | + * @since 10.1.27 | |
| 77 | + */ | |
| 78 | + if (isset($this->$property) && !is_null($this->$property) && $this->$property !== '') | |
| 79 | + { | |
| 80 | + return $this->$property; | |
| 81 | + } | |
| 82 | + | |
| 83 | + return $default; | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Returns an associative array of object properties. | |
| 88 | + * | |
| 89 | + * @param boolean $public If true, returns only the public properties. | |
| 90 | + * | |
| 91 | + * @return array The internal properties. | |
| 92 | + */ | |
| 93 | + public function getProperties($public = true) | |
| 94 | + { | |
| 95 | + $vars = get_object_vars($this); | |
| 96 | + | |
| 97 | + if ($public) | |
| 98 | + { | |
| 99 | + foreach ($vars as $key => $value) | |
| 100 | + { | |
| 101 | + if (substr($key, 0, 1) === '_') | |
| 102 | + { | |
| 103 | + unset($vars[$key]); | |
| 104 | + } | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Collect all none public properties of the current class and it's parents. | |
| 109 | + * This way, protected and private properties won't be returned. | |
| 110 | + * | |
| 111 | + * @since 10.1.68 | |
| 112 | + */ | |
| 113 | + $nonePublicProperties = []; | |
| 114 | + $reflection = new ReflectionObject($this); | |
| 115 | + | |
| 116 | + do { | |
| 117 | + $nonePublicProperties = array_merge( | |
| 118 | + $reflection->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED), | |
| 119 | + $nonePublicProperties | |
| 120 | + ); | |
| 121 | + } while ($reflection = $reflection->getParentClass()); | |
| 122 | + | |
| 123 | + // unset all none public properties, this is needed as get_object_vars returns now all vars | |
| 124 | + // from the current object and not only the CMSObject and the public ones from the inheriting classes | |
| 125 | + foreach ($nonePublicProperties as $prop) | |
| 126 | + { | |
| 127 | + if (array_key_exists($prop->getName(), $vars)) | |
| 128 | + { | |
| 129 | + unset($vars[$prop->getName()]); | |
| 130 | + } | |
| 131 | + } | |
| 132 | + } | |
| 133 | + | |
| 134 | + return $vars; | |
| 135 | + } | |
| 136 | + | |
| 137 | + /** | |
| 138 | + * Modifies a property of the object, creating it if it does not already exist. | |
| 139 | + * | |
| 140 | + * @param string $property The name of the property. | |
| 141 | + * @param mixed $value The value of the property to set. | |
| 142 | + * | |
| 143 | + * @return mixed Previous value of the property. | |
| 144 | + * | |
| 145 | + * @uses get() | |
| 146 | + */ | |
| 147 | + public function set($property, $value = null) | |
| 148 | + { | |
| 149 | + $previous = $this->get($property, null); | |
| 150 | + $this->$property = $value; | |
| 151 | + | |
| 152 | + return $previous; | |
| 153 | + } | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * Set the object properties based on a named array/hash. | |
| 157 | + * | |
| 158 | + * @param mixed $properties Either an associative array or another object. | |
| 159 | + * | |
| 160 | + * @return boolean True on success, otherwise false. | |
| 161 | + * | |
| 162 | + * @uses set() | |
| 163 | + */ | |
| 164 | + public function setProperties($properties) | |
| 165 | + { | |
| 166 | + if (is_array($properties) || is_object($properties)) | |
| 167 | + { | |
| 168 | + foreach ((array) $properties as $k => $v) | |
| 169 | + { | |
| 170 | + // Use the set function which might be overridden. | |
| 171 | + $this->set($k, $v); | |
| 172 | + } | |
| 173 | + | |
| 174 | + return true; | |
| 175 | + } | |
| 176 | + | |
| 177 | + return false; | |
| 178 | + } | |
| 179 | + | |
| 180 | + /** | |
| 181 | + * Get the most recent error message. | |
| 182 | + * | |
| 183 | + * @param integer $i Option error index. | |
| 184 | + * @param boolean $toString Indicates if the exception should return the error message. | |
| 185 | + * | |
| 186 | + * @return mixed The error message or the exception object. | |
| 187 | + */ | |
| 188 | + public function getError($i = null, $toString = true) | |
| 189 | + { | |
| 190 | + // find the error | |
| 191 | + if ($i === null) | |
| 192 | + { | |
| 193 | + // default, return the last message | |
| 194 | + $error = end($this->_errors); | |
| 195 | + } | |
| 196 | + else if (!array_key_exists($i, $this->_errors)) | |
| 197 | + { | |
| 198 | + // if $i has been specified but does not exist, return false | |
| 199 | + return false; | |
| 200 | + } | |
| 201 | + else | |
| 202 | + { | |
| 203 | + $error = $this->_errors[$i]; | |
| 204 | + } | |
| 205 | + | |
| 206 | + // check if only the string is requested | |
| 207 | + if ($error instanceof Exception && $toString) | |
| 208 | + { | |
| 209 | + return $error->getMessage(); | |
| 210 | + } | |
| 211 | + | |
| 212 | + return $error; | |
| 213 | + } | |
| 214 | + | |
| 215 | + /** | |
| 216 | + * Return all errors, if any. | |
| 217 | + * | |
| 218 | + * @return array Array of error messages or exceptions. | |
| 219 | + */ | |
| 220 | + public function getErrors() | |
| 221 | + { | |
| 222 | + return $this->_errors; | |
| 223 | + } | |
| 224 | + | |
| 225 | + /** | |
| 226 | + * Pushes an error message. | |
| 227 | + * | |
| 228 | + * @param mixed $error The error string or an exception. | |
| 229 | + * If $error is an instance of WP_Error, it will | |
| 230 | + * be adapted to a generic exception. | |
| 231 | + * | |
| 232 | + * @return void | |
| 233 | + */ | |
| 234 | + public function setError($error) | |
| 235 | + { | |
| 236 | + if ($error instanceof WP_Error) | |
| 237 | + { | |
| 238 | + // adapt WP_Error object | |
| 239 | + $error = new Exception($error->get_error_message(), (int) $error->get_error_code()); | |
| 240 | + } | |
| 241 | + | |
| 242 | + $this->_errors[] = $error; | |
| 243 | + } | |
| 244 | +} | |