|
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\Reflection\Definition\Behaviour; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Reflection\Contracts\Definition\Behaviour\ProvidesTypeDefinitions; |
|
13
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Trait HasDefinitions |
|
17
|
|
|
*/ |
|
18
|
|
|
trait HasDefinitions |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var array|string[] |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $types = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string|TypeDefinition $type |
|
27
|
|
|
* @return TypeDefinition |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract protected function fetch($type): TypeDefinition; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return iterable|TypeDefinition[] |
|
33
|
|
|
*/ |
|
34
|
3 |
|
public function getDefinitions(): iterable |
|
35
|
|
|
{ |
|
36
|
3 |
|
foreach ($this->types as $type) { |
|
37
|
2 |
|
yield $this->fetch($type); |
|
38
|
|
|
} |
|
39
|
3 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $name |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
5 |
|
public function hasDefinition(string $name): bool |
|
46
|
|
|
{ |
|
47
|
5 |
|
return \in_array($name, $this->types, true); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $name |
|
52
|
|
|
* @return null|TypeDefinition |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public function getDefinition(string $name): ?TypeDefinition |
|
55
|
|
|
{ |
|
56
|
3 |
|
if (! \in_array($name, $this->types, true)) { |
|
57
|
1 |
|
return null; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
return $this->fetch($name); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string|TypeDefinition ...$types |
|
65
|
|
|
* @return ProvidesTypeDefinitions|$this |
|
66
|
|
|
*/ |
|
67
|
17 |
|
public function withDefinition(...$types): ProvidesTypeDefinitions |
|
68
|
|
|
{ |
|
69
|
17 |
|
foreach ($types as $type) { |
|
70
|
17 |
|
$this->types[] = $this->nameOf($type); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
17 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string|TypeDefinition ...$types |
|
78
|
|
|
* @return ProvidesTypeDefinitions|$this |
|
79
|
|
|
*/ |
|
80
|
|
|
public function withoutDefinition(...$types): ProvidesTypeDefinitions |
|
81
|
|
|
{ |
|
82
|
|
|
foreach ($types as $type) { |
|
83
|
|
|
if ($this->hasDefinition($this->nameOf($type))) { |
|
|
|
|
|
|
84
|
|
|
array_splice($this->types, array_search($this->types, $this->nameOf($type)), 1); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.