[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Object/ -> CMSObject.php (source)

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer