Completed
Pull Request — master (#119)
by Toby
04:37 queued 01:24
created

Relationship::fromSelfLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 1
b 1
f 0
cc 1
eloc 4
nc 1
nop 1
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 JsonSerializable;
15
16
class Relationship implements JsonSerializable
17
{
18
    use LinksTrait, SelfLinkTrait, RelatedLinkTrait, PaginationLinksTrait, MetaTrait;
19
20
    private $data;
21
22 6
    private function __construct()
23
    {
24 6
    }
25
26
    public static function fromMeta($meta)
27
    {
28
        $r = new self;
29
        $r->replaceMeta($meta);
0 ignored issues
show
Bug introduced by
The method replaceMeta() does not seem to exist on object<Tobscure\JsonApi\Relationship>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
31
        return $r;
32
    }
33
34
    public static function fromSelfLink($link)
35
    {
36
        $r = new self;
37
        $r->setSelfLink($link);
38
39
        return $r;
40
    }
41
42
    public static function fromRelatedLink($link)
43
    {
44
        $r = new self;
45
        $r->setRelatedLink($link);
46
47
        return $r;
48
    }
49
50 6
    public static function fromData($data)
51
    {
52 6
        $r = new self;
53 6
        $r->data = $data;
54
55 6
        return $r;
56
    }
57
58
    public function getData()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
59
    {
60
        return $this->data;
61
    }
62
63
    public function setData($data)
64
    {
65
        $this->data = $data;
66
    }
67
68 3
    public function jsonSerialize()
69
    {
70 3
        $relationship = [];
71
72 3
        if ($this->data) {
73 3
            $relationship['data'] = is_array($this->data)
74 3
                ? array_map([$this, 'buildIdentifier'], $this->data)
75 3
                : $this->buildIdentifier($this->data);
76 3
        }
77
78 3
        return array_filter($relationship + [
79 3
            'meta' => $this->meta,
80 3
            'links' => $this->links
81 3
        ]);
82
    }
83
84 3
    private function buildIdentifier(ResourceInterface $resource)
85
    {
86 3
        $id = new ResourceIdentifier($resource->getType(), $resource->getId());
87 3
        $id->setMeta($resource->getMeta());
0 ignored issues
show
Bug introduced by
It seems like $resource->getMeta() targeting Tobscure\JsonApi\ResourceInterface::getMeta() can also be of type null; however, Tobscure\JsonApi\MetaTrait::setMeta() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
88
89 3
        return $id;
90
    }
91
}
92