Completed
Push — master ( 2e1d78...d4a74e )
by Toby
66:55 queued 03:07
created

AbstractSerializer::getMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 30
    public function getType($model)
29
    {
30 30
        return $this->type;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 30
    public function getId($model)
37
    {
38 30
        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 12
    public function getLinks($model)
53
    {
54 12
        return [];
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 12
    public function getMeta($model)
61
    {
62 12
        return [];
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     *
68
     * @throws LogicException
69
     */
70 9
    public function getRelationship($model, $name)
71
    {
72 9
        $method = $this->getRelationshipMethodName($name);
73
74 9
        if (method_exists($this, $method)) {
75 9
            $relationship = $this->$method($model);
76
77 9
            if ($relationship !== null && ! ($relationship instanceof Relationship)) {
78 3
                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...
79
            }
80
81 6
            return $relationship;
82
        }
83
    }
84
85
    /**
86
     * Get the serializer method name for the given relationship.
87
     *
88
     * kebab-case is converted into camelCase.
89
     *
90
     * @param string $name
91
     * @return string
92
     */
93 9
    private function getRelationshipMethodName($name)
94
    {
95 9
    	if (stripos($name, '-')) {
96
        	$name = lcfirst(implode('', array_map('ucfirst', explode('-', $name))));
97
    	}
98
99 9
    	return $name;
100
    }
101
}
102