|
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) { |
|
|
|
|
|
|
78
|
|
|
$array['meta'] = $this->meta; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
12 |
|
if ($this->links) { |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
94
|
|
|
*/ |
|
95
|
|
|
private function buildIdentifier(ResourceInterface $resource) |
|
96
|
|
|
{ |
|
97
|
|
|
return [ |
|
98
|
|
|
'type' => $resource->getType(), |
|
99
|
|
|
'id' => $resource->getId() |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
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.