Completed
Pull Request — master (#119)
by Toby
02:47
created

AbstractResource::getLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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 AbstractResource implements ResourceInterface
17
{
18
    use LinksTrait, SelfLinkTrait, MetaTrait;
19
20
    /**
21
     * The resource type.
22
     *
23
     * @var string
24
     */
25
    protected $type;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 3
    public function getType()
31
    {
32 3
        return $this->type;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 3
    public function getAttributes(array $fields = null)
39
    {
40 3
        return [];
41
    }
42
43
    /**
44
     * Get the links.
45
     *
46
     * @return array
47
     */
48
    public function getLinks()
49
    {
50
        return $this->links;
51
    }
52
53
    /**
54
     * Get the meta data.
55
     *
56
     * @return array
57
     */
58 3
    public function getMeta()
59
    {
60 3
        return $this->meta;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     *
66
     * @throws LogicException
67
     */
68 6
    public function getRelationship($name)
69
    {
70 6
        $method = $this->getRelationshipMethodName($name);
71
72 6
        $relationship = $this->$method();
73
74 6
        if ($relationship !== null && ! ($relationship instanceof Relationship)) {
75 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 125 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...
76
        }
77
78 3
        return $relationship;
79
    }
80
81
    /**
82
     * Get the method name for the given relationship.
83
     *
84
     * snake_case and kebab-case are converted into camelCase.
85
     *
86
     * @param string $name
87
     *
88
     * @return string
89
     */
90 6
    private function getRelationshipMethodName($name)
91
    {
92 6
        return 'get'.implode(array_map('ucfirst', preg_split('/[-_]/', $name))).'Relationship';
93
    }
94
}
95