CoreFiles by mySites.guru
AbstractSerializer.php106 lines, 2,133 bytes, from Joomla_5.4.11-Stable-Full_Package.zip
Raw History Compare with 5.4.10
md5
74e8bacce801ce3b44714e1223d1fd97
sha1
c6dc4e9ecffe08254efa0c8dd27d374ab03329cf
sha256
92bde84b0654d29178233ce71bfb57197a8ff7761ab1286b2dadb9139b8f2b45
<?php

/*
 * This file is part of JSON-API.
 *
 * (c) Toby Zerner <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tobscure\JsonApi;

use LogicException;

abstract class AbstractSerializer implements SerializerInterface
{
    /**
     * The type.
     *
     * @var string
     */
    protected $type;

    /**
     * {@inheritdoc}
     */
    public function getType($model)
    {
        return $this->type;
    }

    /**
     * {@inheritdoc}
     */
    public function getId($model)
    {
        return $model->id;
    }

    /**
     * {@inheritdoc}
     */
    public function getAttributes($model, ?array $fields = null)
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getLinks($model)
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getMeta($model)
    {
        return [];
    }

    /**
     * {@inheritdoc}
     *
     * @throws \LogicException
     */
    public function getRelationship($model, $name)
    {
        $method = $this->getRelationshipMethodName($name);

        if (method_exists($this, $method)) {
            $relationship = $this->$method($model);

            if ($relationship !== null && ! ($relationship instanceof Relationship)) {
                throw new LogicException('Relationship method must return null or an instance of Tobscure\JsonApi\Relationship');
            }

            return $relationship;
        }
    }

    /**
     * Get the serializer method name for the given relationship.
     *
     * snake_case and kebab-case are converted into camelCase.
     *
     * @param string $name
     *
     * @return string
     */
    private function getRelationshipMethodName($name)
    {
        if (stripos($name, '-')) {
            $name = lcfirst(implode('', array_map('ucfirst', explode('-', $name))));
        }

        if (stripos($name, '_')) {
            $name = lcfirst(implode('', array_map('ucfirst', explode('_', $name))));
        }

        return $name;
    }
}

The same content is in 94 releases, Joomla 5.3.0-rc1 to 6.3.0-alpha1. See them all, or every version of this file.