1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage utils
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8
9 defined('FOF_INCLUDED') or die;
10
11 /**
12 * Temporary class for backwards compatibility. You should not be using this
13 * in your code. It is currently present to handle the validation error stack
14 * for FOFTable::check() and will be removed in an upcoming version.
15 *
16 * This class is based on JObject as found in Joomla! 3.2.1
17 *
18 * @deprecated 2.1
19 * @codeCoverageIgnore
20 */
21 class FOFUtilsObject
22 {
23 /**
24 * An array of error messages or Exception objects.
25 *
26 * @var array
27 */
28 protected $_errors = array();
29
30 /**
31 * Class constructor, overridden in descendant classes.
32 *
33 * @param mixed $properties Either and 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 * Magic method to convert the object to a string gracefully.
46 *
47 * @return string The classname.
48 */
49 public function __toString()
50 {
51 return get_class($this);
52 }
53
54 /**
55 * Sets a default value if not alreay assigned
56 *
57 * @param string $property The name of the property.
58 * @param mixed $default The default value.
59 *
60 * @return mixed
61 */
62 public function def($property, $default = null)
63 {
64 $value = $this->get($property, $default);
65 return $this->set($property, $value);
66 }
67
68 /**
69 * Returns a property of the object or the default value if the property is not set.
70 *
71 * @param string $property The name of the property.
72 * @param mixed $default The default value.
73 *
74 * @return mixed The value of the property.
75 */
76 public function get($property, $default = null)
77 {
78 if (isset($this->$property))
79 {
80 return $this->$property;
81 }
82 return $default;
83 }
84
85 /**
86 * Returns an associative array of object properties.
87 *
88 * @param boolean $public If true, returns only the public properties.
89 *
90 * @return array
91 */
92 public function getProperties($public = true)
93 {
94 $vars = get_object_vars($this);
95 if ($public)
96 {
97 foreach ($vars as $key => $value)
98 {
99 if ('_' == substr($key, 0, 1))
100 {
101 unset($vars[$key]);
102 }
103 }
104 }
105
106 return $vars;
107 }
108
109 /**
110 * Get the most recent error message.
111 *
112 * @param integer $i Option error index.
113 * @param boolean $toString Indicates if JError objects should return their error message.
114 *
115 * @return string Error message
116 */
117 public function getError($i = null, $toString = true)
118 {
119 // Find the error
120 if ($i === null)
121 {
122 // Default, return the last message
123 $error = end($this->_errors);
124 }
125 elseif (!array_key_exists($i, $this->_errors))
126 {
127 // If $i has been specified but does not exist, return false
128 return false;
129 }
130 else
131 {
132 $error = $this->_errors[$i];
133 }
134
135 // Check if only the string is requested
136 if ($error instanceof Exception && $toString)
137 {
138 return (string) $error;
139 }
140
141 return $error;
142 }
143
144 /**
145 * Return all errors, if any.
146 *
147 * @return array Array of error messages or JErrors.
148 */
149 public function getErrors()
150 {
151 return $this->_errors;
152 }
153
154 /**
155 * Modifies a property of the object, creating it if it does not already exist.
156 *
157 * @param string $property The name of the property.
158 * @param mixed $value The value of the property to set.
159 *
160 * @return mixed Previous value of the property.
161 */
162 public function set($property, $value = null)
163 {
164 $previous = isset($this->$property) ? $this->$property : null;
165 $this->$property = $value;
166 return $previous;
167 }
168
169 /**
170 * Set the object properties based on a named array/hash.
171 *
172 * @param mixed $properties Either an associative array or another object.
173 *
174 * @return boolean
175 */
176 public function setProperties($properties)
177 {
178 if (is_array($properties) || is_object($properties))
179 {
180 foreach ((array) $properties as $k => $v)
181 {
182 // Use the set function which might be overridden.
183 $this->set($k, $v);
184 }
185 return true;
186 }
187
188 return false;
189 }
190
191 /**
192 * Add an error message.
193 *
194 * @param string $error Error message.
195 *
196 * @return void
197 */
198 public function setError($error)
199 {
200 array_push($this->_errors, $error);
201 }
202 }
203