for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?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;
}
public function getId($model)
return $model->id;
public function getAttributes($model, array $fields = null)
return [];
public function getLinks($model)
public function getMeta($model)
* @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');
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.
return $relationship;
* Get the serializer method name for the given relationship.
* kebab-case is converted into camelCase.
* @param string $name
* @return string
private function getRelationshipMethodName($name)
if (stripos($name, '-')) {
$name = lcfirst(implode('', array_map('ucfirst', explode('-', $name))));
return $name;
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.