PageRenderTime 24ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/base/object.php

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