|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\SDL\Reflection\Coercion; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\SDL\Base\Dependent\BaseArgument; |
|
13
|
|
|
use Railt\SDL\Contracts\Definitions\TypeDefinition; |
|
14
|
|
|
use Railt\SDL\Contracts\Dependent\ArgumentDefinition; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class ArgumentCoercion |
|
18
|
|
|
*/ |
|
19
|
|
|
class ArgumentCoercion extends BaseTypeCoercion |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param TypeDefinition $type |
|
23
|
|
|
* @return bool |
|
24
|
|
|
*/ |
|
25
|
6571 |
|
public function match(TypeDefinition $type): bool |
|
26
|
|
|
{ |
|
27
|
6571 |
|
return $type instanceof ArgumentDefinition; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param TypeDefinition|ArgumentDefinition $type |
|
32
|
|
|
*/ |
|
33
|
5545 |
|
public function apply(TypeDefinition $type): void |
|
34
|
|
|
{ |
|
35
|
5545 |
|
if ($type->hasDefaultValue()) { |
|
|
|
|
|
|
36
|
5320 |
|
$this->normalizeDefinedValue($type); |
|
|
|
|
|
|
37
|
5320 |
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5535 |
|
if (! $type->hasDefaultValue()) { |
|
|
|
|
|
|
41
|
5535 |
|
$this->inferenceValue($type); |
|
|
|
|
|
|
42
|
5535 |
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param ArgumentDefinition|BaseArgument $argument |
|
48
|
|
|
*/ |
|
49
|
5320 |
|
private function normalizeDefinedValue(ArgumentDefinition $argument): void |
|
50
|
|
|
{ |
|
51
|
5320 |
|
$value = $argument->getDefaultValue(); |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The value can be automatically adjusted without loss of meaning: |
|
55
|
|
|
* |
|
56
|
|
|
* <code> |
|
57
|
|
|
* # Will transforms to List: |
|
58
|
|
|
* field(arg: [Int] = 23) → field(arg: [Int] = [23]) |
|
59
|
|
|
* field(arg: [String] = "s") → field(arg: [String] = ["s"]) |
|
60
|
|
|
* field(arg: [ID] = "id") → field(arg: [ID] = ["id"]) |
|
61
|
|
|
* field(arg: [Float] = 4.2) → field(arg: [Float] = [4.2]) |
|
62
|
|
|
* field(arg: [Input] = {a: 23}) → field(arg: [Input] = [{a: 23}]) |
|
63
|
|
|
* # etc... |
|
64
|
|
|
* |
|
65
|
|
|
* # Excluding "NULL": |
|
66
|
|
|
* field(arg: [Float] = null) → field(arg: [Float] = null) |
|
67
|
|
|
* </code> |
|
68
|
|
|
*/ |
|
69
|
5320 |
|
$isListDefinedByNonList = ($value !== null && ! \is_array($value)); |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* The allowable conversion method for NULL: |
|
73
|
|
|
* <code> |
|
74
|
|
|
* # As it is while Nullable type initialized by NULL: |
|
75
|
|
|
* field(arg: [ID] = null) → field(arg: [ID] = null) |
|
76
|
|
|
* |
|
77
|
|
|
* # But apply coercion while NonNull type initialized by NULL: |
|
78
|
|
|
* field(arg: [ID]! = null) → field(arg: [ID]! = [null]) |
|
79
|
|
|
* </code> |
|
80
|
|
|
*/ |
|
81
|
5320 |
|
$isNonNullListDefinedByNull = ($argument->isNonNull() && $value === null); |
|
82
|
|
|
|
|
83
|
5320 |
|
if (($isListDefinedByNonList || $isNonNullListDefinedByNull) && $argument->isList()) { |
|
84
|
|
|
/** |
|
85
|
|
|
* Warn: Do not change to `(array)$value` since leads |
|
86
|
|
|
* to the destructuring of some iterators (like instance of InputInvocation::class). |
|
87
|
|
|
*/ |
|
88
|
307 |
|
$this->set($argument, [$value]); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
5320 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param ArgumentDefinition|BaseArgument $argument |
|
94
|
|
|
*/ |
|
95
|
5535 |
|
private function inferenceValue(ArgumentDefinition $argument): void |
|
96
|
|
|
{ |
|
97
|
|
|
/** |
|
98
|
|
|
* Any code initialization like: |
|
99
|
|
|
* <code> |
|
100
|
|
|
* field(argument: [Type]): Type |
|
101
|
|
|
* </code> |
|
102
|
|
|
* |
|
103
|
|
|
* Will transform to: |
|
104
|
|
|
* <code> |
|
105
|
|
|
* field(argument: [Type] = []): Type |
|
106
|
|
|
* </code> |
|
107
|
|
|
*/ |
|
108
|
5535 |
|
if ($argument->isList()) { |
|
109
|
18 |
|
$this->set($argument, []); |
|
|
|
|
|
|
110
|
18 |
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Any code initialization like: |
|
115
|
|
|
* <code> |
|
116
|
|
|
* field(argument: Type): Type |
|
117
|
|
|
* </code> |
|
118
|
|
|
* |
|
119
|
|
|
* Will transform to: |
|
120
|
|
|
* <code> |
|
121
|
|
|
* field(argument: Type = NULL): Type |
|
122
|
|
|
* </code> |
|
123
|
|
|
*/ |
|
124
|
5517 |
|
if (! $argument->isNonNull()) { |
|
125
|
5509 |
|
$this->set($argument, null); |
|
|
|
|
|
|
126
|
5509 |
|
return; |
|
127
|
|
|
} |
|
128
|
40 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param BaseArgument $argument |
|
132
|
|
|
* @param $value |
|
133
|
|
|
*/ |
|
134
|
|
|
private function set(BaseArgument $argument, $value): void |
|
135
|
|
|
{ |
|
136
|
5528 |
|
$invocation = function ($value): void { |
|
137
|
|
|
/** @var BaseArgument $this */ |
|
138
|
5528 |
|
$this->defaultValue = $value; |
|
|
|
|
|
|
139
|
5528 |
|
$this->hasDefaultValue = true; |
|
|
|
|
|
|
140
|
5528 |
|
}; |
|
141
|
|
|
|
|
142
|
5528 |
|
$invocation->call($argument, $value); |
|
143
|
5528 |
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: