[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Serializer/Events/ -> OnGetApiAttributes.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2020 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\Serializer\Events;
  11  
  12  use Joomla\CMS\Event\AbstractImmutableEvent;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('_JEXEC') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Event for getting extra data attributes for an API Entity
  20   *
  21   * @since  4.0.0
  22   */
  23  final class OnGetApiAttributes extends AbstractImmutableEvent
  24  {
  25      /**
  26       * The attributes
  27       *
  28       * @var     array
  29       * @since   4.0.0
  30       */
  31      private $attributes = [];
  32  
  33      /**
  34       * Constructor.
  35       *
  36       * Mandatory arguments:
  37       * attributes   array           The main data for the object.
  38       * context      string          The content type of the api resource.
  39       *
  40       * @param   string  $name       The event name.
  41       * @param   array   $arguments  The event arguments.
  42       *
  43       * @since   4.0.0
  44       * @throws  \BadMethodCallException
  45       */
  46      public function __construct($name, array $arguments = array())
  47      {
  48          if (
  49              !\array_key_exists('attributes', $arguments)
  50              || \array_key_exists('attributes', $arguments) && !is_array($arguments['attributes'])
  51          ) {
  52              throw new \BadMethodCallException("Argument 'attributes' as an array is required for event $name");
  53          }
  54  
  55          if (!\array_key_exists('context', $arguments)) {
  56              throw new \BadMethodCallException("Argument 'context' is required for event $name");
  57          }
  58  
  59          parent::__construct($name, $arguments);
  60      }
  61  
  62      /**
  63       * The properties to be rendered.
  64       *
  65       * @return  array
  66       *
  67       * @since   4.0.0
  68       */
  69      public function getAttributes(): array
  70      {
  71          return $this->attributes;
  72      }
  73  
  74      /**
  75       * Set a named attribute to be rendered in the API.
  76       *
  77       * @param   string  $name   The name of the property to be rendered in the api
  78       * @param   mixed   $value  The value of the named property to be rendered in the api.
  79       *
  80       * @return  void
  81       * @since   4.0.0
  82       */
  83      public function addAttribute($name, $value): void
  84      {
  85          $this->attributes[$name] = $value;
  86      }
  87  
  88      /**
  89       * Set attributes to be rendered in the API.
  90       *
  91       * @param   array  $value  An array of key/value pairs for properties to be added to the api.
  92       *
  93       * @return  void
  94       * @since   4.0.0
  95       */
  96      public function addAttributes(array $value): void
  97      {
  98          $this->attributes = array_merge($this->attributes, $value);
  99      }
 100  }


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