1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spinen\Halo\Support\Relations; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
6
|
|
|
use Illuminate\Support\Traits\Macroable; |
7
|
|
|
use Spinen\Halo\Exceptions\InvalidRelationshipException; |
8
|
|
|
use Spinen\Halo\Support\Builder; |
9
|
|
|
use Spinen\Halo\Support\Model; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Relation |
13
|
|
|
*/ |
14
|
|
|
abstract class Relation |
15
|
|
|
{ |
16
|
|
|
use ForwardsCalls, Macroable { |
|
|
|
|
17
|
|
|
__call as macroCall; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The related model instance. |
22
|
|
|
*/ |
23
|
|
|
protected Model $related; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new relation instance. |
27
|
|
|
* |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
* |
31
|
|
|
* @throws InvalidRelationshipException |
32
|
|
|
*/ |
33
|
25 |
|
public function __construct(protected Builder $builder, protected Model $parent) |
34
|
|
|
{ |
35
|
25 |
|
$this->related = $builder->getModel(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Handle dynamic method calls to the relationship. |
40
|
|
|
*/ |
41
|
3 |
|
public function __call(string $method, array $parameters) |
42
|
|
|
{ |
43
|
3 |
|
if (static::hasMacro($method)) { |
44
|
1 |
|
return $this->macroCall($method, $parameters); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
$result = $this->forwardCallTo($this->getBuilder(), $method, $parameters); |
48
|
|
|
|
49
|
2 |
|
if ($result === $this->getBuilder()) { |
50
|
1 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the Builder instance |
58
|
|
|
*/ |
59
|
11 |
|
public function getBuilder(): Builder |
60
|
|
|
{ |
61
|
11 |
|
return $this->builder; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the parent Model instance |
66
|
|
|
*/ |
67
|
4 |
|
public function getParent(): Model |
68
|
|
|
{ |
69
|
4 |
|
return $this->parent; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the related Model instance |
74
|
|
|
*/ |
75
|
1 |
|
public function getRelated(): Model |
76
|
|
|
{ |
77
|
1 |
|
return $this->related; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the results of the relationship. |
82
|
|
|
*/ |
83
|
|
|
abstract public function getResults(); |
84
|
|
|
} |
85
|
|
|
|