1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Object
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Joomla Platform Object Class
14 *
15 * This class allows for simple but smart objects with get and set methods
16 * and an internal error handler.
17 *
18 * @since 11.1
19 * @deprecated 4.0
20 */
21 class JObject
22 {
23 /**
24 * An array of error messages or Exception objects.
25 *
26 * @var array
27 * @since 11.1
28 * @see JError
29 * @deprecated 12.3 JError has been deprecated
30 */
31 protected $_errors = array();
32
33 /**
34 * Class constructor, overridden in descendant classes.
35 *
36 * @param mixed $properties Either and associative array or another
37 * object to set the initial properties of the object.
38 *
39 * @since 11.1
40 */
41 public function __construct($properties = null)
42 {
43 if ($properties !== null)
44 {
45 $this->setProperties($properties);
46 }
47 }
48
49 /**
50 * Magic method to convert the object to a string gracefully.
51 *
52 * @return string The classname.
53 *
54 * @since 11.1
55 * @deprecated 12.3 Classes should provide their own __toString() implementation.
56 */
57 public function __toString()
58 {
59 return get_class($this);
60 }
61
62 /**
63 * Sets a default value if not already assigned
64 *
65 * @param string $property The name of the property.
66 * @param mixed $default The default value.
67 *
68 * @return mixed
69 *
70 * @since 11.1
71 */
72 public function def($property, $default = null)
73 {
74 $value = $this->get($property, $default);
75
76 return $this->set($property, $value);
77 }
78
79 /**
80 * Returns a property of the object or the default value if the property is not set.
81 *
82 * @param string $property The name of the property.
83 * @param mixed $default The default value.
84 *
85 * @return mixed The value of the property.
86 *
87 * @since 11.1
88 *
89 * @see JObject::getProperties()
90 */
91 public function get($property, $default = null)
92 {
93 if (isset($this->$property))
94 {
95 return $this->$property;
96 }
97
98 return $default;
99 }
100
101 /**
102 * Returns an associative array of object properties.
103 *
104 * @param boolean $public If true, returns only the public properties.
105 *
106 * @return array
107 *
108 * @since 11.1
109 *
110 * @see JObject::get()
111 */
112 public function getProperties($public = true)
113 {
114 $vars = get_object_vars($this);
115
116 if ($public)
117 {
118 foreach ($vars as $key => $value)
119 {
120 if ('_' == substr($key, 0, 1))
121 {
122 unset($vars[$key]);
123 }
124 }
125 }
126
127 return $vars;
128 }
129
130 /**
131 * Get the most recent error message.
132 *
133 * @param integer $i Option error index.
134 * @param boolean $toString Indicates if JError objects should return their error message.
135 *
136 * @return string Error message
137 *
138 * @since 11.1
139 * @see JError
140 * @deprecated 12.3 JError has been deprecated
141 */
142 public function getError($i = null, $toString = true)
143 {
144 // Find the error
145 if ($i === null)
146 {
147 // Default, return the last message
148 $error = end($this->_errors);
149 }
150 elseif (!array_key_exists($i, $this->_errors))
151 {
152 // If $i has been specified but does not exist, return false
153 return false;
154 }
155 else
156 {
157 $error = $this->_errors[$i];
158 }
159
160 // Check if only the string is requested
161 if ($error instanceof Exception && $toString)
162 {
163 return $error->getMessage();
164 }
165
166 return $error;
167 }
168
169 /**
170 * Return all errors, if any.
171 *
172 * @return array Array of error messages or JErrors.
173 *
174 * @since 11.1
175 * @see JError
176 * @deprecated 12.3 JError has been deprecated
177 */
178 public function getErrors()
179 {
180 return $this->_errors;
181 }
182
183 /**
184 * Modifies a property of the object, creating it if it does not already exist.
185 *
186 * @param string $property The name of the property.
187 * @param mixed $value The value of the property to set.
188 *
189 * @return mixed Previous value of the property.
190 *
191 * @since 11.1
192 */
193 public function set($property, $value = null)
194 {
195 $previous = isset($this->$property) ? $this->$property : null;
196 $this->$property = $value;
197
198 return $previous;
199 }
200
201 /**
202 * Set the object properties based on a named array/hash.
203 *
204 * @param mixed $properties Either an associative array or another object.
205 *
206 * @return boolean
207 *
208 * @since 11.1
209 *
210 * @see JObject::set()
211 */
212 public function setProperties($properties)
213 {
214 if (is_array($properties) || is_object($properties))
215 {
216 foreach ((array) $properties as $k => $v)
217 {
218 // Use the set function which might be overridden.
219 $this->set($k, $v);
220 }
221
222 return true;
223 }
224
225 return false;
226 }
227
228 /**
229 * Add an error message.
230 *
231 * @param string $error Error message.
232 *
233 * @return void
234 *
235 * @since 11.1
236 * @see JError
237 * @deprecated 12.3 JError has been deprecated
238 */
239 public function setError($error)
240 {
241 $this->_errors[] = $error;
242 }
243 }
244