[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_languages/src/View/Strings/ -> JsonapiView.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.API
   5   * @subpackage  com_languages
   6   *
   7   * @copyright   (C) 2019 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\Languages\Api\View\Strings;
  12  
  13  use Joomla\CMS\Language\Text;
  14  use Joomla\CMS\MVC\View\GenericDataException;
  15  use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
  16  use Joomla\CMS\Serializer\JoomlaSerializer;
  17  use Tobscure\JsonApi\Collection;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * The strings view
  25   *
  26   * @since  4.0.0
  27   */
  28  class JsonapiView extends BaseApiView
  29  {
  30      /**
  31       * The fields to render items in the documents
  32       *
  33       * @var  array
  34       * @since  4.0.0
  35       */
  36      protected $fieldsToRenderList = [
  37          'id',
  38          'string',
  39          'file',
  40      ];
  41  
  42      /**
  43       * Execute and display a template script.
  44       *
  45       * @param   array|null  $items  Array of items
  46       *
  47       * @return  string
  48       *
  49       * @since   4.0.0
  50       */
  51      public function displayList(array $items = null)
  52      {
  53          /** @var \Joomla\Component\Languages\Administrator\Model\StringsModel $model */
  54          $model  = $this->getModel();
  55          $result = $model->search();
  56  
  57          if ($result instanceof \Exception) {
  58              throw $result;
  59          }
  60  
  61          $items = [];
  62  
  63          foreach ($result['results'] as $item) {
  64              $items[] = $this->prepareItem($item);
  65          }
  66  
  67          // Check for errors.
  68          if (\count($errors = $this->get('Errors'))) {
  69              throw new GenericDataException(implode("\n", $errors), 500);
  70          }
  71  
  72          if ($this->type === null) {
  73              throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_CONTENT_TYPE_MISSING'), 400);
  74          }
  75  
  76          $collection = (new Collection($items, new JoomlaSerializer($this->type)))
  77              ->fields([$this->type => $this->fieldsToRenderList]);
  78  
  79          // Set the data into the document and render it
  80          $this->document->setData($collection);
  81  
  82          return $this->document->render();
  83      }
  84  
  85      /**
  86       * Prepare item before render.
  87       *
  88       * @param   object  $item  The model item
  89       *
  90       * @return  object
  91       *
  92       * @since   4.0.0
  93       */
  94      protected function prepareItem($item)
  95      {
  96          $item->id = $item->constant;
  97          unset($item->constant);
  98  
  99          return parent::prepareItem($item);
 100      }
 101  }


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