|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swis\JsonApi\Client\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Swis\JsonApi\Client\Collection; |
|
7
|
|
|
use Swis\JsonApi\Client\Interfaces\DataInterface; |
|
8
|
|
|
use Swis\JsonApi\Client\Interfaces\ManyRelationInterface; |
|
9
|
|
|
use Swis\JsonApi\Client\Interfaces\OneRelationInterface; |
|
10
|
|
|
use Swis\JsonApi\Client\Links; |
|
11
|
|
|
use Swis\JsonApi\Client\Meta; |
|
12
|
|
|
use Swis\JsonApi\Client\Relations\HasManyRelation; |
|
13
|
|
|
use Swis\JsonApi\Client\Relations\HasOneRelation; |
|
14
|
|
|
use Swis\JsonApi\Client\Relations\MorphToManyRelation; |
|
15
|
|
|
use Swis\JsonApi\Client\Relations\MorphToRelation; |
|
16
|
|
|
|
|
17
|
|
|
trait HasRelations |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface[]|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface[] |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $relations = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create a singular relation to another item. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $itemClass |
|
28
|
|
|
* @param string|null $name |
|
29
|
|
|
* |
|
30
|
|
|
* @return \Swis\JsonApi\Client\Relations\HasOneRelation |
|
31
|
|
|
*/ |
|
32
|
55 |
|
public function hasOne(string $itemClass, string $name = null): OneRelationInterface |
|
33
|
|
|
{ |
|
34
|
55 |
|
$name = $name ?: Str::snake(debug_backtrace()[1]['function']); |
|
35
|
|
|
|
|
36
|
55 |
|
if (!array_key_exists($name, $this->relations)) { |
|
37
|
55 |
|
$this->relations[$name] = new HasOneRelation((new $itemClass())->getType()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
55 |
|
return $this->relations[$name]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create a plural relation to another item. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $itemClass |
|
47
|
|
|
* @param string|null $name |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Swis\JsonApi\Client\Relations\HasManyRelation |
|
50
|
|
|
*/ |
|
51
|
30 |
|
public function hasMany(string $itemClass, string $name = null): ManyRelationInterface |
|
52
|
|
|
{ |
|
53
|
30 |
|
$name = $name ?: Str::snake(debug_backtrace()[1]['function']); |
|
54
|
|
|
|
|
55
|
30 |
|
if (!array_key_exists($name, $this->relations)) { |
|
56
|
30 |
|
$this->relations[$name] = new HasManyRelation((new $itemClass())->getType()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
30 |
|
return $this->relations[$name]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Create a singular relation. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string|null $name |
|
66
|
|
|
* |
|
67
|
|
|
* @return \Swis\JsonApi\Client\Relations\MorphToRelation |
|
68
|
|
|
*/ |
|
69
|
125 |
|
public function morphTo(string $name = null): OneRelationInterface |
|
70
|
|
|
{ |
|
71
|
125 |
|
$name = $name ?: Str::snake(debug_backtrace()[1]['function']); |
|
72
|
|
|
|
|
73
|
125 |
|
if (!array_key_exists($name, $this->relations)) { |
|
74
|
125 |
|
$this->relations[$name] = new MorphToRelation(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
125 |
|
return $this->relations[$name]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Create a plural relation. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string|null $name |
|
84
|
|
|
* |
|
85
|
|
|
* @return \Swis\JsonApi\Client\Relations\MorphToManyRelation |
|
86
|
|
|
*/ |
|
87
|
70 |
|
public function morphToMany(string $name = null): ManyRelationInterface |
|
88
|
|
|
{ |
|
89
|
70 |
|
$name = $name ?: Str::snake(debug_backtrace()[1]['function']); |
|
90
|
|
|
|
|
91
|
70 |
|
if (!array_key_exists($name, $this->relations)) { |
|
92
|
70 |
|
$this->relations[$name] = new MorphToManyRelation(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
70 |
|
return $this->relations[$name]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
175 |
|
public function getRelations(): array |
|
102
|
|
|
{ |
|
103
|
175 |
|
return $this->relations; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $name |
|
108
|
|
|
* |
|
109
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface|null |
|
110
|
|
|
*/ |
|
111
|
155 |
|
public function getRelation(string $name) |
|
112
|
|
|
{ |
|
113
|
155 |
|
return $this->relations[$name] ?? null; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get the relationship data (included). |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $name |
|
120
|
|
|
* |
|
121
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DataInterface|null |
|
122
|
|
|
*/ |
|
123
|
40 |
|
public function getRelationValue(string $name): ?DataInterface |
|
124
|
|
|
{ |
|
125
|
|
|
// If the "attribute" exists as a method on the model, we will just assume |
|
126
|
|
|
// it is a relationship and will load and return the included items in the relationship |
|
127
|
40 |
|
$method = Str::camel($name); |
|
128
|
40 |
|
if (method_exists($this, $method)) { |
|
129
|
15 |
|
return $this->$method()->getIncluded(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// If the "attribute" exists as a relationship on the model, we will return |
|
133
|
|
|
// the included items in the relationship |
|
134
|
25 |
|
if ($this->hasRelation($name)) { |
|
135
|
15 |
|
return $this->getRelation($name)->getIncluded(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
10 |
|
return null; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Set the specific relationship on the model. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $name |
|
145
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\DataInterface $data |
|
146
|
|
|
* @param \Swis\JsonApi\Client\Links|null $links |
|
147
|
|
|
* @param \Swis\JsonApi\Client\Meta|null $meta |
|
148
|
|
|
* |
|
149
|
|
|
* @return static |
|
150
|
|
|
*/ |
|
151
|
100 |
|
public function setRelation(string $name, DataInterface $data, Links $links = null, Meta $meta = null) |
|
152
|
|
|
{ |
|
153
|
100 |
|
$method = Str::camel($name); |
|
154
|
100 |
|
if (method_exists($this, $method)) { |
|
155
|
|
|
/** @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationObject */ |
|
156
|
15 |
|
$relationObject = $this->$method(); |
|
157
|
90 |
|
} elseif ($data instanceof Collection) { |
|
158
|
15 |
|
$relationObject = $this->morphToMany($name); |
|
159
|
|
|
} else { |
|
160
|
80 |
|
$relationObject = $this->morphTo($name); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
100 |
|
$relationObject->associate($data); |
|
|
|
|
|
|
164
|
100 |
|
$relationObject->setLinks($links); |
|
165
|
100 |
|
$relationObject->setMeta($meta); |
|
166
|
|
|
|
|
167
|
100 |
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param string $name |
|
172
|
|
|
* |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
40 |
|
public function hasRelation(string $name): bool |
|
176
|
|
|
{ |
|
177
|
40 |
|
return array_key_exists($name, $this->relations); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param $name |
|
182
|
|
|
* |
|
183
|
|
|
* @return static |
|
184
|
|
|
*/ |
|
185
|
5 |
|
public function unsetRelation(string $name) |
|
186
|
|
|
{ |
|
187
|
5 |
|
unset($this->relations[$name]); |
|
188
|
|
|
|
|
189
|
5 |
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|