|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bakery\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use Bakery\Utils\Utils; |
|
6
|
|
|
use Bakery\Support\TypeRegistry; |
|
7
|
|
|
use Bakery\Types\Definitions\RootType; |
|
8
|
|
|
|
|
9
|
|
|
class PolymorphicField extends Field |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The model schemas of a polymorphic type. |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $modelSchemas; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The type resolver. |
|
20
|
|
|
* |
|
21
|
|
|
* @var callable |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $typeResolver; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* PolymorphicType constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param \Bakery\Support\TypeRegistry $registry |
|
29
|
|
|
* @param array $modelSchemas |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(TypeRegistry $registry, array $modelSchemas = []) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct($registry); |
|
34
|
|
|
|
|
35
|
|
|
$this->modelSchemas = $modelSchemas; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get the model schemas of a polymorphic type. |
|
40
|
|
|
* |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getModelSchemas(): array |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->modelSchemas; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the model schema by key. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $key |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getModelSchemaByKey(string $key) |
|
55
|
|
|
{ |
|
56
|
|
|
return collect($this->modelSchemas)->first(function ($definition) use ($key) { |
|
57
|
|
|
return Utils::single(resolve($definition)->getModel()) === $key; |
|
|
|
|
|
|
58
|
|
|
}); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Define the type resolver. |
|
63
|
|
|
* |
|
64
|
|
|
* @param callable $resolver |
|
65
|
|
|
* @return $this |
|
66
|
|
|
*/ |
|
67
|
|
|
public function typeResolver(callable $resolver) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->typeResolver = $resolver; |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the type resolver. |
|
76
|
|
|
* |
|
77
|
|
|
* @return callable |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getTypeResolver() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->typeResolver; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the underlying (wrapped) type. |
|
86
|
|
|
* |
|
87
|
|
|
* @return \Bakery\Types\Definitions\RootType |
|
88
|
|
|
*/ |
|
89
|
|
|
public function type(): RootType |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->registry->type($this->name); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.