Completed
Pull Request — master (#119)
by Toby
61:54
created

Relationship::buildIdentifier()   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 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 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
class Relationship
15
{
16
    use LinksTrait;
17
    use MetaTrait;
18
19
    /**
20
     * The data object.
21
     *
22
     * @var \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null
23
     */
24
    protected $data;
25
26
    /**
27
     * Create a new relationship.
28
     *
29
     * @param \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null $data
30
     */
31 21
    public function __construct($data = null)
32
    {
33 21
        $this->data = $data;
34 21
    }
35
36
    /**
37
     * Get the data object.
38
     *
39
     * @return \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null
40
     */
41 12
    public function getData()
42
    {
43 12
        return $this->data;
44
    }
45
46
    /**
47
     * Set the data object.
48
     *
49
     * @param \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null $data
50
     *
51
     * @return $this
52
     */
53
    public function setData($data)
54
    {
55
        $this->data = $data;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Build the relationship as an array.
62
     *
63
     * @return array
64
     */
65 12
    public function toArray()
66
    {
67 12
        $array = [];
68
69 12
        if ($this->data) {
70 12
            if (is_array($this->data)) {
71 12
                $array['data'] = array_map([$this, 'buildIdentifier'], $this->data);
72
            } else {
73 12
                $array['data'] = $this->buildIdentifier($this->data);
74
            }
75
        }
76
77 12
        if ($this->meta) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->meta of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
78
            $array['meta'] = $this->meta;
79
        }
80
81 12
        if ($this->links) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->links of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
82
            $array['links'] = $this->links;
83
        }
84
85
        return $array;
86
    }
87
88
    /**
89
     * Build an idenitfier array for the given resource.
90
     *
91
     * @param ResourceInterface $resource
92
     *
93
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
94
     */
95
    private function buildIdentifier(ResourceInterface $resource)
96
    {
97
        return [
98
            'type' => $resource->getType(),
99
            'id' => $resource->getId()
100
        ];
101
    }
102
}
103