|
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\Dictionary; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Reflection\Common\Serializable; |
|
13
|
|
|
use Railt\Reflection\Contracts\Definition; |
|
14
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
|
15
|
|
|
use Railt\Reflection\Contracts\Dictionary; |
|
16
|
|
|
use Railt\Reflection\Contracts\Type; |
|
17
|
|
|
use Railt\Reflection\Exception\TypeNotFoundException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class SimpleDictionary |
|
21
|
|
|
*/ |
|
22
|
|
|
class SimpleDictionary implements Dictionary |
|
23
|
|
|
{ |
|
24
|
|
|
use Serializable; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array|TypeDefinition[] |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $types = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Type|null $of |
|
33
|
|
|
* @return iterable|TypeDefinition[] |
|
34
|
|
|
*/ |
|
35
|
3 |
|
public function all(Type $of = null): iterable |
|
36
|
|
|
{ |
|
37
|
3 |
|
foreach ($this->types as $definition) { |
|
38
|
1 |
|
if ($of === null || $definition::typeOf($of)) { |
|
39
|
1 |
|
yield $definition; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
3 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $name |
|
46
|
|
|
* @param Definition|null $from |
|
47
|
|
|
* @return TypeDefinition |
|
48
|
|
|
* @throws TypeNotFoundException |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function get(string $name, Definition $from = null): TypeDefinition |
|
51
|
|
|
{ |
|
52
|
3 |
|
if ($result = $this->find($name)) { |
|
53
|
3 |
|
return $result; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
throw $this->typeNotFound($name, $from); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $name |
|
61
|
|
|
* @return null|TypeDefinition |
|
62
|
|
|
*/ |
|
63
|
6 |
|
public function find(string $name): ?TypeDefinition |
|
64
|
|
|
{ |
|
65
|
6 |
|
return $this->types[$name] ?? null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $name |
|
70
|
|
|
* @param Definition|null $from |
|
71
|
|
|
* @return TypeNotFoundException |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function typeNotFound(string $name, Definition $from = null): TypeNotFoundException |
|
74
|
|
|
{ |
|
75
|
|
|
$error = \sprintf('Type %s not found or could not be loaded', $name); |
|
76
|
|
|
|
|
77
|
|
|
$exception = new TypeNotFoundException($error); |
|
78
|
|
|
|
|
79
|
|
|
if ($from !== null) { |
|
80
|
|
|
$exception->throwsIn($from->getFile(), $from->getLine(), $from->getColumn()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $exception; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param TypeDefinition $type |
|
88
|
|
|
* @return Dictionary |
|
89
|
|
|
*/ |
|
90
|
5 |
|
public function add(TypeDefinition $type): Dictionary |
|
91
|
|
|
{ |
|
92
|
5 |
|
$this->types[$type->getName()] = $type; |
|
93
|
|
|
|
|
94
|
5 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $name |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function has(string $name): bool |
|
102
|
|
|
{ |
|
103
|
3 |
|
return isset($this->types[$name]); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|