Completed
Push — master ( 5b5380...2e1d78 )
by Toby
63:49
created

AbstractSerializer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 17
Bugs 3 Features 2
Metric Value
wmc 10
c 17
b 3
f 2
lcom 1
cbo 0
dl 0
loc 78
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 4 1
A getId() 0 4 1
A getAttributes() 0 4 1
A getLinks() 0 4 1
A getRelationship() 0 14 4
A getRelationshipMethodName() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of JSON-API.
5
 *
6
 * (c) Toby Zerner <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tobscure\JsonApi;
13
14
use LogicException;
15
16
abstract class AbstractSerializer implements SerializerInterface
17
{
18
    /**
19
     * The type.
20
     *
21
     * @var string
22
     */
23
    protected $type;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getType($model)
29
    {
30
        return $this->type;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getId($model)
37
    {
38
        return $model->id;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getAttributes($model, array $fields = null)
45
    {
46
        return [];
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getLinks($model)
53
    {
54
        return [];
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     *
60
     * @throws LogicException
61
     */
62
    public function getRelationship($model, $name)
63
    {
64
        $method = $this->getRelationshipMethodName($name);
65
66
        if (method_exists($this, $method)) {
67
            $relationship = $this->$method($model);
68
69
            if ($relationship !== null && ! ($relationship instanceof Relationship)) {
70
                throw new LogicException('Relationship method must return null or an instance of Tobscure\JsonApi\Relationship');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

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.

Loading history...
71
            }
72
73
            return $relationship;
74
        }
75
    }
76
77
    /**
78
     * Get the serializer method name for the given relationship.
79
     *
80
     * kebab-case is converted into camelCase.
81
     *
82
     * @param string $name
83
     * @return string
84
     */
85
    private function getRelationshipMethodName($name)
86
    {
87
    	if (stripos($name, '-')) {
88
        	$name = lcfirst(implode('', array_map('ucfirst', explode('-', $name))));
89
    	}
90
91
    	return $name;
92
    }
93
}
94