File manager - Edit - /home/opticamezl/www/newok/Serializer.tar
Back
Events/OnGetApiRelation.php 0000644 00000004524 15172742130 0011674 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\Serializer\Events; use Joomla\CMS\Event\AbstractImmutableEvent; use Tobscure\JsonApi\Relationship; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * Event for getting information on an API Relationship * * @since 4.0.0 */ final class OnGetApiRelation extends AbstractImmutableEvent { /** * The relationship * * @var Relationship * @since 4.0.0 */ private $relationship; /** * Constructor. * * Mandatory arguments: * model mixed The model being used to render the resource. * field string The field name we wish to obtain a relationship for. * context string The content type of the api resource. * * @param string $name The event name. * @param array $arguments The event arguments. * * @since 4.0.0 * @throws \BadMethodCallException */ public function __construct($name, array $arguments = []) { if (!\array_key_exists('model', $arguments)) { throw new \BadMethodCallException("Argument 'model' is required for event $name"); } if (!\array_key_exists('field', $arguments)) { throw new \BadMethodCallException("Argument 'field' is required for event $name"); } if (!\array_key_exists('context', $arguments)) { throw new \BadMethodCallException("Argument 'context' is required for event $name"); } parent::__construct($name, $arguments); } /** * Get properties to render. * * @return Relationship * * @since 4.0.0 */ public function getRelationship(): ?Relationship { return $this->relationship; } /** * Set relationship object that should be rendered. * * @param Relationship $relationship The relationship object that should be rendered. * * @return void * @since 4.0.0 */ public function setRelationship(Relationship $relationship): void { $this->relationship = $relationship; } } Events/OnGetApiAttributes.php 0000644 00000005112 15172742130 0012237 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\Serializer\Events; use Joomla\CMS\Event\AbstractImmutableEvent; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * Event for getting extra data attributes for an API Entity * * @since 4.0.0 */ final class OnGetApiAttributes extends AbstractImmutableEvent { /** * The attributes * * @var array * @since 4.0.0 */ private $attributes = []; /** * Constructor. * * Mandatory arguments: * attributes array The main data for the object. * context string The content type of the api resource. * * @param string $name The event name. * @param array $arguments The event arguments. * * @since 4.0.0 * @throws \BadMethodCallException */ public function __construct($name, array $arguments = []) { if ( !\array_key_exists('attributes', $arguments) || \array_key_exists('attributes', $arguments) && !is_array($arguments['attributes']) ) { throw new \BadMethodCallException("Argument 'attributes' as an array is required for event $name"); } if (!\array_key_exists('context', $arguments)) { throw new \BadMethodCallException("Argument 'context' is required for event $name"); } parent::__construct($name, $arguments); } /** * The properties to be rendered. * * @return array * * @since 4.0.0 */ public function getAttributes(): array { return $this->attributes; } /** * Set a named attribute to be rendered in the API. * * @param string $name The name of the property to be rendered in the api * @param mixed $value The value of the named property to be rendered in the api. * * @return void * @since 4.0.0 */ public function addAttribute($name, $value): void { $this->attributes[$name] = $value; } /** * Set attributes to be rendered in the API. * * @param array $value An array of key/value pairs for properties to be added to the api. * * @return void * @since 4.0.0 */ public function addAttributes(array $value): void { $this->attributes = array_merge($this->attributes, $value); } } JoomlaSerializer.php 0000644 00000007272 15172742130 0010542 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\Serializer; use Joomla\CMS\Factory; use Joomla\CMS\Object\CMSObject; use Tobscure\JsonApi\AbstractSerializer; use Tobscure\JsonApi\Relationship; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * This class does the messy job of sanitising all the classes Joomla has that contain data and converting them * into a standard array that can be consumed by the Tobscure library. It also throws appropriate plugin events * to allow 3rd party extensions to add custom data and relations into these properties before they are rendered * * @since 4.0.0 */ class JoomlaSerializer extends AbstractSerializer { /** * Constructor. * * @param string $type The content type to be loaded * * @since 4.0.0 */ public function __construct(string $type) { $this->type = $type; } /** * Get the attributes array. * * @param array|\stdClass|CMSObject $post The data container * @param array|null $fields The requested fields to be rendered * * @return array * * @since 4.0.0 */ public function getAttributes($post, array $fields = null) { if (!($post instanceof \stdClass) && !(\is_array($post)) && !($post instanceof CMSObject)) { $message = sprintf( 'Invalid argument for %s. Expected array or %s. Got %s', static::class, CMSObject::class, \gettype($post) ); throw new \InvalidArgumentException($message); } // The response from a standard ListModel query if ($post instanceof \stdClass) { $post = (array) $post; } // The response from a standard AdminModel query also works for Table which extends CMSObject if ($post instanceof CMSObject) { $post = $post->getProperties(); } $event = new Events\OnGetApiAttributes('onGetApiAttributes', ['attributes' => $post, 'context' => $this->type]); /** @var Events\OnGetApiAttributes $eventResult */ $eventResult = Factory::getApplication()->getDispatcher()->dispatch('onGetApiAttributes', $event); $combinedData = array_merge($post, $eventResult->getAttributes()); return \is_array($fields) ? array_intersect_key($combinedData, array_flip($fields)) : $combinedData; } /** * Get a relationship. * * @param mixed $model The model of the entity being rendered * @param string $name The name of the relationship to return * * @return \Tobscure\JsonApi\Relationship|null * * @since 4.0.0 */ public function getRelationship($model, $name) { $result = parent::getRelationship($model, $name); // If we found a result in the content type serializer return now. Else trigger plugins. if ($result instanceof Relationship) { return $result; } $eventData = ['model' => $model, 'field' => $name, 'context' => $this->type]; $event = new Events\OnGetApiRelation('onGetApiRelation', $eventData); /** @var Events\OnGetApiRelation $eventResult */ $eventResult = Factory::getApplication()->getDispatcher()->dispatch('onGetApiRelation', $event); $relationship = $eventResult->getRelationship(); if ($relationship instanceof Relationship) { return $relationship; } return null; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.23 | Generation time: 0 |
proxy
|
phpinfo
|
Settings