[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Serializer/ -> JoomlaSerializer.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.txt
   8   */
   9  
  10  namespace Joomla\CMS\Serializer;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Object\CMSObject;
  14  use Tobscure\JsonApi\AbstractSerializer;
  15  use Tobscure\JsonApi\Relationship;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * This class does the messy job of sanitising all the classes Joomla has that contain data and converting them
  23   * into a standard array that can be consumed by the Tobscure library. It also throws appropriate plugin events
  24   * to allow 3rd party extensions to add custom data and relations into these properties before they are rendered
  25   *
  26   * @since  4.0.0
  27   */
  28  class JoomlaSerializer extends AbstractSerializer
  29  {
  30      /**
  31       * Constructor.
  32       *
  33       * @param   string  $type  The content type to be loaded
  34       *
  35       * @since 4.0.0
  36       */
  37      public function __construct(string $type)
  38      {
  39          $this->type = $type;
  40      }
  41  
  42      /**
  43       * Get the attributes array.
  44       *
  45       * @param   array|\stdClass|CMSObject  $post    The data container
  46       * @param   array|null                 $fields  The requested fields to be rendered
  47       *
  48       * @return  array
  49       *
  50       * @since   4.0.0
  51       */
  52      public function getAttributes($post, array $fields = null)
  53      {
  54          if (!($post instanceof \stdClass) && !(\is_array($post)) && !($post instanceof CMSObject)) {
  55              $message = sprintf(
  56                  'Invalid argument for %s. Expected array or %s. Got %s',
  57                  static::class,
  58                  CMSObject::class,
  59                  \gettype($post)
  60              );
  61  
  62              throw new \InvalidArgumentException($message);
  63          }
  64  
  65          // The response from a standard ListModel query
  66          if ($post instanceof \stdClass) {
  67              $post = (array) $post;
  68          }
  69  
  70          // The response from a standard AdminModel query also works for Table which extends CMSObject
  71          if ($post instanceof CMSObject) {
  72              $post = $post->getProperties();
  73          }
  74  
  75          $event = new Events\OnGetApiAttributes('onGetApiAttributes', ['attributes' => $post, 'context' => $this->type]);
  76  
  77          /** @var Events\OnGetApiAttributes $eventResult */
  78          $eventResult = Factory::getApplication()->getDispatcher()->dispatch('onGetApiAttributes', $event);
  79          $combinedData = array_merge($post, $eventResult->getAttributes());
  80  
  81          return \is_array($fields) ? array_intersect_key($combinedData, array_flip($fields)) : $combinedData;
  82      }
  83  
  84      /**
  85       * Get a relationship.
  86       *
  87       * @param   mixed   $model  The model of the entity being rendered
  88       * @param   string  $name   The name of the relationship to return
  89       *
  90       * @return \Tobscure\JsonApi\Relationship|void
  91       *
  92       * @since   4.0.0
  93       */
  94      public function getRelationship($model, $name)
  95      {
  96          $result = parent::getRelationship($model, $name);
  97  
  98          // If we found a result in the content type serializer return now. Else trigger plugins.
  99          if ($result instanceof Relationship) {
 100              return $result;
 101          }
 102  
 103          $eventData = ['model' => $model, 'field' => $name, 'context' => $this->type];
 104          $event     = new Events\OnGetApiRelation('onGetApiRelation', $eventData);
 105  
 106          /** @var Events\OnGetApiRelation $eventResult */
 107          $eventResult = Factory::getApplication()->getDispatcher()->dispatch('onGetApiRelation', $event);
 108  
 109          $relationship = $eventResult->getRelationship();
 110  
 111          if ($relationship instanceof Relationship) {
 112              return $relationship;
 113          }
 114      }
 115  }


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