[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/MVC/Model/ -> StateBehaviorTrait.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  namespace Joomla\CMS\MVC\Model;
  11  
  12  use Joomla\CMS\Object\CMSObject;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Trait which supports state behavior
  20   *
  21   * @since  4.0.0
  22   */
  23  trait StateBehaviorTrait
  24  {
  25      /**
  26       * Indicates if the internal state has been set
  27       *
  28       * @var    boolean
  29       * @since  4.0.0
  30       */
  31      protected $__state_set = null;
  32  
  33      /**
  34       * A state object
  35       *
  36       * @var    CMSObject
  37       * @since  4.0.0
  38       */
  39      protected $state = null;
  40  
  41      /**
  42       * Method to get state variables.
  43       *
  44       * @param   string  $property  Optional parameter name
  45       * @param   mixed   $default   Optional default value
  46       *
  47       * @return  mixed  The property where specified, the state object where omitted
  48       *
  49       * @since   4.0.0
  50       */
  51      public function getState($property = null, $default = null)
  52      {
  53          if ($this->state === null) {
  54              $this->state = new CMSObject();
  55          }
  56  
  57          if (!$this->__state_set) {
  58              // Protected method to auto-populate the state
  59              $this->populateState();
  60  
  61              // Set the state set flag to true.
  62              $this->__state_set = true;
  63          }
  64  
  65          return $property === null ? $this->state : $this->state->get($property, $default);
  66      }
  67  
  68      /**
  69       * Method to set state variables.
  70       *
  71       * @param   string  $property  The name of the property
  72       * @param   mixed   $value     The value of the property to set or null
  73       *
  74       * @return  mixed  The previous value of the property or null if not set
  75       *
  76       * @since   4.0.0
  77       */
  78      public function setState($property, $value = null)
  79      {
  80          if ($this->state === null) {
  81              $this->state = new CMSObject();
  82          }
  83  
  84          return $this->state->set($property, $value);
  85      }
  86  
  87      /**
  88       * Method to auto-populate the state.
  89       *
  90       * This method should only be called once per instantiation and is designed
  91       * to be called on the first call to the getState() method unless the
  92       * configuration flag to ignore the request is set.
  93       *
  94       * @return  void
  95       *
  96       * @note    Calling getState in this method will result in recursion.
  97       * @since   4.0.0
  98       */
  99      protected function populateState()
 100      {
 101      }
 102  }


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