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