[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_users/src/DataShape/ -> DataShapeObject.php (source)

   1  <?php
   2  
   3  /**
   4   * @package    Joomla.Administrator
   5   * @subpackage com_users
   6   *
   7   * @copyright  (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Users\Administrator\DataShape;
  12  
  13  use InvalidArgumentException;
  14  
  15  /**
  16   * Generic helper for handling data shapes in com_users
  17   *
  18   * @since 4.2.0
  19   */
  20  abstract class DataShapeObject implements \ArrayAccess
  21  {
  22      /**
  23       * Public constructor
  24       *
  25       * @param   array  $array  The data to initialise this object with
  26       *
  27       * @since 4.2.0
  28       */
  29      public function __construct(array $array = [])
  30      {
  31          if (!is_array($array) && !($array instanceof self)) {
  32              throw new InvalidArgumentException(sprintf('%s needs an array or a %s object', __METHOD__, __CLASS__));
  33          }
  34  
  35          foreach (($array instanceof self) ? $array->asArray() : $array as $k => $v) {
  36              $this[$k] = $v;
  37          }
  38      }
  39  
  40      /**
  41       * Get the data shape as a key-value array
  42       *
  43       * @return array
  44       *
  45       * @since 4.2.0
  46       */
  47      public function asArray(): array
  48      {
  49          return get_object_vars($this);
  50      }
  51  
  52      /**
  53       * Merge another data shape object or key-value array into this object.
  54       *
  55       * @param   array|self  $newValues  The object or array to merge into self.
  56       *
  57       * @return  $this
  58       *
  59       * @since 4.2.0
  60       */
  61      public function merge($newValues): self
  62      {
  63          if (!is_array($newValues) && !($newValues instanceof self)) {
  64              throw new InvalidArgumentException(sprintf('%s needs an array or a %s object', __METHOD__, __CLASS__));
  65          }
  66  
  67          foreach (($newValues instanceof self) ? $newValues->asArray() : $newValues as $k => $v) {
  68              if (!isset($this->{$k})) {
  69                  continue;
  70              }
  71  
  72              $this[$k] = $v;
  73          }
  74  
  75          return $this;
  76      }
  77  
  78      /**
  79       * Magic getter
  80       *
  81       * @param   string  $name  The name of the property to retrieve
  82       *
  83       * @return  mixed
  84       *
  85       * @since 4.2.0
  86       */
  87      public function __get($name)
  88      {
  89          $methodName = 'get' . ucfirst($name);
  90  
  91          if (method_exists($this, $methodName)) {
  92              return $this->{$methodName};
  93          }
  94  
  95          if (property_exists($this, $name)) {
  96              return $this->{$name};
  97          }
  98  
  99          throw new InvalidArgumentException(sprintf('Property %s not found in %s', $name, __CLASS__));
 100      }
 101  
 102      /**
 103       * Magic Setter
 104       *
 105       * @param   string  $name   The property to set the value for
 106       * @param   mixed   $value  The property value to set it to
 107       *
 108       * @return mixed
 109       * @since 4.2.0
 110       */
 111      public function __set($name, $value)
 112      {
 113          $methodName = 'set' . ucfirst($name);
 114  
 115          if (method_exists($this, $methodName)) {
 116              return $this->{$methodName}($value);
 117          }
 118  
 119          if (property_exists($this, $name)) {
 120              $this->{$name} = $value;
 121          }
 122  
 123          throw new InvalidArgumentException(sprintf('Property %s not found in %s', $name, __CLASS__));
 124      }
 125  
 126      /**
 127       * Is a property set?
 128       *
 129       * @param   string  $name  Property name
 130       *
 131       * @return  boolean  Does it exist in the object?
 132       * @since 4.2.0
 133       */
 134      #[\ReturnTypeWillChange]
 135      public function __isset($name)
 136      {
 137          $methodName = 'get' . ucfirst($name);
 138  
 139          return method_exists($this, $methodName) || property_exists($this, $name);
 140      }
 141  
 142      /**
 143       * Does the property exist (array access)?
 144       *
 145       * @param   string  $offset  Property name
 146       *
 147       * @return  boolean
 148       * @since 4.2.0
 149       */
 150      #[\ReturnTypeWillChange]
 151      public function offsetExists($offset)
 152      {
 153          return isset($this->{$offset});
 154      }
 155  
 156      /**
 157       * Get the value of a property (array access).
 158       *
 159       * @param   string  $offset  Property name
 160       *
 161       * @return  mixed
 162       * @since 4.2.0
 163       */
 164      #[\ReturnTypeWillChange]
 165      public function offsetGet($offset)
 166      {
 167          return $this->{$offset};
 168      }
 169  
 170      /**
 171       * Set the value of a property (array access).
 172       *
 173       * @param   string  $offset  Property name
 174       * @param   mixed   $value   Property value
 175       *
 176       * @return void
 177       * @since 4.2.0
 178       */
 179      #[\ReturnTypeWillChange]
 180      public function offsetSet($offset, $value)
 181      {
 182          $this->{$offset} = $value;
 183      }
 184  
 185      /**
 186       * Unset a property (array access).
 187       *
 188       * @param   string  $offset  Property name
 189       *
 190       * @return  mixed
 191       * @since 4.2.0
 192       */
 193      #[\ReturnTypeWillChange]
 194      public function offsetUnset($offset)
 195      {
 196          throw new \LogicException(sprintf('You cannot unset members of %s', __CLASS__));
 197      }
 198  }


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