|
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\Builder\Invocations; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\NodeInterface; |
|
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
14
|
|
|
use Railt\SDL\Base\Invocations\BaseInputInvocation; |
|
15
|
|
|
use Railt\SDL\Contracts\Definitions\InputDefinition; |
|
16
|
|
|
use Railt\SDL\Contracts\Definitions\TypeDefinition; |
|
17
|
|
|
use Railt\SDL\Contracts\Dependent\ArgumentDefinition; |
|
18
|
|
|
use Railt\SDL\Reflection\Builder\DocumentBuilder; |
|
19
|
|
|
use Railt\SDL\Reflection\Builder\Process\Compilable; |
|
20
|
|
|
use Railt\SDL\Reflection\Builder\Process\Compiler; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class InputInvocationBuilder |
|
24
|
|
|
* @property ArgumentDefinition $parent |
|
25
|
|
|
*/ |
|
26
|
|
|
class InputInvocationBuilder extends BaseInputInvocation implements Compilable |
|
27
|
|
|
{ |
|
28
|
|
|
use Compiler; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $path = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $parentType; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* InputInvocationBuilder constructor. |
|
42
|
|
|
* @param NodeInterface $ast |
|
43
|
|
|
* @param DocumentBuilder $document |
|
44
|
|
|
* @param string $parentType |
|
45
|
|
|
* @param array $path |
|
46
|
|
|
*/ |
|
47
|
798 |
|
public function __construct(NodeInterface $ast, DocumentBuilder $document, string $parentType, array $path) |
|
48
|
|
|
{ |
|
49
|
798 |
|
$this->path = $path; |
|
50
|
798 |
|
$this->parentType = $parentType; |
|
51
|
798 |
|
$this->boot($ast, $document); |
|
52
|
798 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
36 |
|
public function getName(): string |
|
58
|
|
|
{ |
|
59
|
36 |
|
return $this->parentType; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param NodeInterface|RuleInterface $ast |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
222 |
|
protected function onCompile(NodeInterface $ast): bool |
|
67
|
|
|
{ |
|
68
|
222 |
|
$key = (string)$ast->getChild(0)->getChild(0)->getValue(); |
|
|
|
|
|
|
69
|
222 |
|
$value = $ast->getChild(1)->getChild(0); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
222 |
|
$this->arguments[$key] = $this->parseValue($value, $this->parentType, \array_merge($this->path, [$key])); |
|
72
|
|
|
|
|
73
|
222 |
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return TypeDefinition |
|
78
|
|
|
*/ |
|
79
|
282 |
|
public function getParent(): TypeDefinition |
|
80
|
|
|
{ |
|
81
|
282 |
|
return $this->load($this->parentType); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
68 |
|
public function __sleep(): array |
|
88
|
|
|
{ |
|
89
|
68 |
|
return \array_merge(parent::__sleep(), [ |
|
90
|
68 |
|
'path', |
|
91
|
|
|
'parentType', |
|
92
|
|
|
]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return null|TypeDefinition |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getTypeDefinition(): ?TypeDefinition |
|
99
|
|
|
{ |
|
100
|
282 |
|
$reduce = function (?InputDefinition $carry, $item): ?TypeDefinition { |
|
101
|
|
|
/** @var ArgumentDefinition|null $argument */ |
|
102
|
|
|
$argument = $carry->getArgument($item); |
|
103
|
|
|
// TODO $argument can be null. Add validation? |
|
104
|
|
|
|
|
105
|
|
|
return $argument ? $argument->getTypeDefinition() : null; |
|
106
|
282 |
|
}; |
|
107
|
|
|
|
|
108
|
282 |
|
return \array_reduce($this->path, $reduce, $this->getParent()); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
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: