1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP version 7.1 |
4
|
|
|
* |
5
|
|
|
* This source file is subject to the license that is bundled with this package in the file LICENSE. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace PhUml\Code; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* It represents a variable's type declaration |
12
|
|
|
*/ |
13
|
|
|
class TypeDeclaration |
14
|
|
|
{ |
15
|
|
|
/** @var string[] All valid types for PHP 7.1 */ |
16
|
|
|
private static $builtInTypes = [ |
17
|
|
|
'int', 'bool', 'string', 'array', 'float', 'callable', 'iterable' |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $name; |
22
|
|
|
|
23
|
219 |
|
public function __construct(?string $name) |
24
|
|
|
{ |
25
|
219 |
|
$this->name = $name; |
26
|
219 |
|
} |
27
|
|
|
|
28
|
165 |
|
public static function absent(): TypeDeclaration |
29
|
|
|
{ |
30
|
165 |
|
return new TypeDeclaration(null); |
31
|
|
|
} |
32
|
|
|
|
33
|
159 |
|
public static function from(?string $text): TypeDeclaration |
34
|
|
|
{ |
35
|
159 |
|
return new TypeDeclaration($text); |
36
|
|
|
} |
37
|
|
|
|
38
|
120 |
|
public function isPresent(): bool |
39
|
|
|
{ |
40
|
120 |
|
return null !== $this->name; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* This will help when building the relationships between classes/interfaces since built-in |
45
|
|
|
* types are not part of a UML class diagram |
46
|
|
|
*/ |
47
|
48 |
|
public function isBuiltIn(): bool |
48
|
|
|
{ |
49
|
48 |
|
$type = $this->name; |
50
|
48 |
|
if ($this->isArray()) { |
51
|
6 |
|
$type = $this->removeArraySuffix(); |
52
|
|
|
} |
53
|
48 |
|
return $this->isPresent() && \in_array($type, self::$builtInTypes, true); |
54
|
|
|
} |
55
|
|
|
|
56
|
48 |
|
private function isArray(): bool |
57
|
|
|
{ |
58
|
48 |
|
return strpos($this->name, '[]') === \strlen($this->name) - 2; |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
private function removeArraySuffix(): string |
62
|
|
|
{ |
63
|
6 |
|
return substr($this->name, 0, -2); |
64
|
|
|
} |
65
|
|
|
|
66
|
66 |
|
public function __toString() |
67
|
|
|
{ |
68
|
66 |
|
return (string)$this->name; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|