CoreFiles by mySites.guru
JoomlaSerializer.php80 lines, 1,809 bytes, from Joomla_4.0.0-alpha8-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha7
md5
4bd9fd7db3865ec8053254b621b91523
sha1
07a498e5d3863c2b3cdcd6477eb6730e0dbd0770
sha256
79138c2ef9e020eb7e4cf6bbcc78c5954c4f7ad447e01122a468d30ae481fa1e
<?php
/**
 * Joomla! Content Management System
 *
 * @copyright  Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\CMS\Serializer;

defined('_JEXEC') or die;

use Joomla\CMS\Object\CMSObject;
use Joomla\CMS\Table\Table;
use Tobscure\JsonApi\AbstractSerializer;

/**
 * Temporary serializer
 *
 * @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   Table|array|\stdClass|CMSobject  $post    The model
	 * @param   array                            $fields  The fields can be array or null
	 *
	 * @return  array
	 *
	 * @since   4.0.0
	 */
	public function getAttributes($post, array $fields = null)
	{
		if (!($post instanceof Table) && !($post instanceof \stdClass) && !(is_array($post))
			&& !($post instanceof CMSObject))
		{
			$message = sprintf(
				'Invalid argument for TableSerializer. Expected array or %s. Got %s',
				Table::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
		if ($post instanceof CMSObject)
		{
			$post = $post->getProperties();
		}

		// TODO: Find a way to make this an instance of TableInterface instead of the concrete class
		if ($post instanceof Table)
		{
			$post = $post->getProperties();
		}

		return is_array($fields) ? array_intersect_key($post, array_flip($fields)) : $post;
	}
}

The same content is in 5 releases, Joomla 4.0.0-alpha7 to 4.0.0-alpha11. See them all, or every version of this file.