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\Frontend\Type; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class TypeName |
14
|
|
|
*/ |
15
|
|
|
class TypeName implements TypeNameInterface, \JsonSerializable |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
public const NAMESPACE_SEPARATOR = '/'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array|string[] |
24
|
|
|
*/ |
25
|
|
|
private $chunks; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string|null |
29
|
|
|
*/ |
30
|
|
|
private $fqn; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* TypeName constructor. |
34
|
|
|
* @param array $chunks |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $chunks) |
37
|
|
|
{ |
38
|
|
|
$this->chunks = $chunks; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return \Traversable |
43
|
|
|
*/ |
44
|
|
|
public function getIterator(): \Traversable |
45
|
|
|
{ |
46
|
|
|
return new \ArrayIterator($this->chunks); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getName(): string |
53
|
|
|
{ |
54
|
|
|
return $this->chunks[\count($this->chunks) - 1]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getFullyQualifiedName(): string |
61
|
|
|
{ |
62
|
|
|
if ($this->fqn === null) { |
63
|
|
|
$this->fqn = \implode(static::NAMESPACE_SEPARATOR, $this->chunks); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->fqn; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param TypeNameInterface $prefix |
71
|
|
|
* @return TypeNameInterface |
72
|
|
|
*/ |
73
|
|
|
public function in(TypeNameInterface $prefix): TypeNameInterface |
74
|
|
|
{ |
75
|
|
|
return new static(\array_merge($prefix->getChunks(), $this->getChunks())); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array|string[] |
80
|
|
|
*/ |
81
|
|
|
public function getChunks(): array |
82
|
|
|
{ |
83
|
|
|
return $this->chunks; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function __toString(): string |
90
|
|
|
{ |
91
|
|
|
return $this->getFullyQualifiedName(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param TypeNameInterface $name |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function is(TypeNameInterface $name): bool |
99
|
|
|
{ |
100
|
|
|
return $name->getFullyQualifiedName() === $this->getFullyQualifiedName(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $chunks |
105
|
|
|
* @return TypeName |
106
|
|
|
*/ |
107
|
|
|
public static function fromArray(array $chunks): TypeNameInterface |
108
|
|
|
{ |
109
|
|
|
return new static($chunks); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return TypeNameInterface |
114
|
|
|
*/ |
115
|
|
|
public static function global(): TypeNameInterface |
116
|
|
|
{ |
117
|
|
|
return new static([]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $name |
122
|
|
|
* @return TypeNameInterface |
123
|
|
|
*/ |
124
|
|
|
public static function fromString(string $name): TypeNameInterface |
125
|
|
|
{ |
126
|
|
|
$escaped = \ltrim($name, self::NAMESPACE_SEPARATOR); |
127
|
|
|
|
128
|
|
|
if (\strlen($escaped) === null) { |
129
|
|
|
return new Anonymous(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($name !== $escaped) { |
133
|
|
|
return new AtGlobal(\explode(self::NAMESPACE_SEPARATOR, $escaped)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return new static(\explode(self::NAMESPACE_SEPARATOR, $escaped)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function jsonSerialize(): string |
143
|
|
|
{ |
144
|
|
|
return $this->getFullyQualifiedName(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|