1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bakery\Fields; |
4
|
|
|
|
5
|
|
|
use Bakery\Utils\Utils; |
6
|
|
|
use Bakery\Eloquent\ModelSchema; |
7
|
|
|
use Bakery\Support\TypeRegistry; |
8
|
|
|
use Bakery\Types\Definitions\RootType; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
11
|
|
|
|
12
|
|
|
class EloquentField extends Field |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $modelSchemaClass; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $inverseRelationName; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var callable|null |
26
|
|
|
*/ |
27
|
|
|
protected $relationResolver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var callable|null |
31
|
|
|
*/ |
32
|
|
|
protected $collectionResolver; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* EloquentField constructor. |
36
|
|
|
* |
37
|
|
|
* @param \Bakery\Support\TypeRegistry $registry |
38
|
|
|
* @param string $class |
39
|
|
|
*/ |
40
|
|
|
public function __construct(TypeRegistry $registry, string $class) |
41
|
|
|
{ |
42
|
|
|
$this->modelSchemaClass = $class; |
43
|
|
|
|
44
|
|
|
parent::__construct($registry); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set the name of the inverse relationship. |
49
|
|
|
* |
50
|
|
|
* @param string $relationName |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
|
|
public function inverse(string $relationName): self |
54
|
|
|
{ |
55
|
|
|
$this->inverseRelationName = $relationName; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the name of the inverse relationship. |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getInverse(): ?string |
66
|
|
|
{ |
67
|
|
|
return $this->inverseRelationName; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the type of the Eloquent field. |
72
|
|
|
* |
73
|
|
|
* @return \Bakery\Types\Definitions\RootType |
74
|
|
|
*/ |
75
|
|
|
protected function type(): RootType |
76
|
|
|
{ |
77
|
|
|
return $this->registry->type($this->getName()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return \Bakery\Eloquent\ModelSchema |
82
|
|
|
*/ |
83
|
|
|
protected function getModelClass(): ModelSchema |
84
|
|
|
{ |
85
|
|
|
return $this->registry->getModelSchema($this->modelSchemaClass); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function name(): string |
92
|
|
|
{ |
93
|
|
|
return $this->getModelClass()->getTypename(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set a custom relation resolver. |
98
|
|
|
*/ |
99
|
|
|
public function relation(callable $resolver): self |
100
|
|
|
{ |
101
|
|
|
$this->relationResolver = $resolver; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return the Eloquent relation. |
108
|
|
|
*/ |
109
|
|
|
public function getRelation(Model $model): Relation |
110
|
|
|
{ |
111
|
|
|
if ($resolver = $this->relationResolver) { |
112
|
|
|
return $resolver($model); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$accessor = $this->getAccessor(); |
116
|
|
|
|
117
|
|
|
Utils::invariant( |
118
|
|
|
method_exists($model, $accessor), |
119
|
|
|
'Relation "'.$accessor.'" is not defined on "'.get_class($model).'".' |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
return $model->{$accessor}(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Determine if the field has a relation resolver. |
127
|
|
|
*/ |
128
|
|
|
public function hasRelationResolver(): bool |
129
|
|
|
{ |
130
|
|
|
return isset($this->relationResolver); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the result of the field. |
135
|
|
|
*/ |
136
|
|
|
public function getResult(Model $model) |
137
|
|
|
{ |
138
|
|
|
if ($resolver = $this->relationResolver) { |
139
|
|
|
return $resolver($model)->get(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $model->{$this->getAccessor()}; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|