|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Swis\JsonApi\Client\Relations; |
|
6
|
|
|
|
|
7
|
|
|
use Swis\JsonApi\Client\Collection; |
|
8
|
|
|
use Swis\JsonApi\Client\Interfaces\ManyRelationInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @property \Swis\JsonApi\Client\Collection|false|null $data |
|
12
|
|
|
* @property \Swis\JsonApi\Client\Collection|false|null $included |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class AbstractManyRelation extends AbstractRelation implements ManyRelationInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param \Swis\JsonApi\Client\Collection|null $data |
|
18
|
|
|
* |
|
19
|
|
|
* @return $this |
|
20
|
|
|
*/ |
|
21
|
116 |
|
public function setData(?Collection $data) |
|
22
|
|
|
{ |
|
23
|
116 |
|
$this->data = $data; |
|
24
|
|
|
|
|
25
|
116 |
|
return $this; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return \Swis\JsonApi\Client\Collection|null |
|
30
|
|
|
*/ |
|
31
|
8 |
|
public function getData(): ?Collection |
|
32
|
|
|
{ |
|
33
|
8 |
|
return $this->data ?: null; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param \Swis\JsonApi\Client\Collection $included |
|
38
|
|
|
* |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
116 |
|
public function setIncluded(Collection $included) |
|
42
|
|
|
{ |
|
43
|
116 |
|
$this->included = $included; |
|
44
|
|
|
|
|
45
|
116 |
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return \Swis\JsonApi\Client\Collection |
|
50
|
|
|
*/ |
|
51
|
88 |
|
public function getIncluded(): Collection |
|
52
|
|
|
{ |
|
53
|
88 |
|
return $this->included ?: new Collection(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param \Swis\JsonApi\Client\Collection $included |
|
58
|
|
|
* |
|
59
|
|
|
* @return $this |
|
60
|
|
|
*/ |
|
61
|
116 |
|
public function associate(Collection $included) |
|
62
|
|
|
{ |
|
63
|
116 |
|
return $this->setData($included) |
|
64
|
116 |
|
->setIncluded($included); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return \Swis\JsonApi\Client\Collection |
|
69
|
|
|
*/ |
|
70
|
44 |
|
public function getAssociated(): Collection |
|
71
|
|
|
{ |
|
72
|
44 |
|
if ($this->hasIncluded()) { |
|
73
|
44 |
|
return $this->getIncluded(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($this->hasData()) { |
|
77
|
|
|
return $this->getData(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return new Collection(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Sort the included collection by the given key. |
|
85
|
|
|
* You can also pass your own callback to determine how to sort the collection values. |
|
86
|
|
|
* |
|
87
|
|
|
* @param callable $callback |
|
88
|
|
|
* @param int $options |
|
89
|
|
|
* @param bool $descending |
|
90
|
|
|
* |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
4 |
|
public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
|
94
|
|
|
{ |
|
95
|
|
|
// Included may be empty when defining the relation (on the item), |
|
96
|
|
|
// but will be filled when using the relation to fetch the data. |
|
97
|
|
|
// Checking if we have included items and applying the order is |
|
98
|
|
|
// simpler then keeping track of the sorts and applying them later. |
|
99
|
4 |
|
if ($this->hasIncluded()) { |
|
100
|
4 |
|
$this->included = $this->getIncluded()->sortBy($callback, $options, $descending); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
4 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|