PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.7
VikBooking Hotel Booking Engine & PMS v1.6.7
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / libraries / adapter / application / object.php

object.php in VikBooking Hotel Booking Engine & PMS 1.6.7, at libraries/adapter/application/object.php

219 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 return $vars;
109 }
110
111 /**
112 * Modifies a property of the object, creating it if it does not already exist.
113 *
114 * @param string $property The name of the property.
115 * @param mixed $value The value of the property to set.
116 *
117 * @return mixed Previous value of the property.
118 *
119 * @uses get()
120 */
121 public function set($property, $value = null)
122 {
123 $previous = $this->get($property, null);
124 $this->$property = $value;
125
126 return $previous;
127 }
128
129 /**
130 * Set the object properties based on a named array/hash.
131 *
132 * @param mixed $properties Either an associative array or another object.
133 *
134 * @return boolean True on success, otherwise false.
135 *
136 * @uses set()
137 */
138 public function setProperties($properties)
139 {
140 if (is_array($properties) || is_object($properties))
141 {
142 foreach ((array) $properties as $k => $v)
143 {
144 // Use the set function which might be overridden.
145 $this->set($k, $v);
146 }
147
148 return true;
149 }
150
151 return false;
152 }
153
154 /**
155 * Get the most recent error message.
156 *
157 * @param integer $i Option error index.
158 * @param boolean $toString Indicates if the exception should return the error message.
159 *
160 * @return mixed The error message or the exception object.
161 */
162 public function getError($i = null, $toString = true)
163 {
164 // find the error
165 if ($i === null)
166 {
167 // default, return the last message
168 $error = end($this->_errors);
169 }
170 else if (!array_key_exists($i, $this->_errors))
171 {
172 // if $i has been specified but does not exist, return false
173 return false;
174 }
175 else
176 {
177 $error = $this->_errors[$i];
178 }
179
180 // check if only the string is requested
181 if ($error instanceof Exception && $toString)
182 {
183 return $error->getMessage();
184 }
185
186 return $error;
187 }
188
189 /**
190 * Return all errors, if any.
191 *
192 * @return array Array of error messages or exceptions.
193 */
194 public function getErrors()
195 {
196 return $this->_errors;
197 }
198
199 /**
200 * Pushes an error message.
201 *
202 * @param mixed $error The error string or an exception.
203 * If $error is an instance of WP_Error, it will
204 * be adapted to a generic exception.
205 *
206 * @return void
207 */
208 public function setError($error)
209 {
210 if ($error instanceof WP_Error)
211 {
212 // adapt WP_Error object
213 $error = new Exception($error->get_error_message(), (int) $error->get_error_code());
214 }
215
216 $this->_errors[] = $error;
217 }
218 }
219