|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Properties package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Serafim <[email protected]> |
|
6
|
|
|
* @date 14.04.2016 13:29 |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Serafim\Properties\Support; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Declaration |
|
15
|
|
|
* @package Serafim\Properties\Support |
|
16
|
|
|
*/ |
|
17
|
|
|
class Declaration |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array|string[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private $types = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $field; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $access; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Declaration constructor. |
|
36
|
|
|
* @param string $types |
|
37
|
|
|
* @param string $field |
|
38
|
|
|
* @param string $accessType |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($types, $field, $accessType = Parser::ACCESS_BOTH) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->types = explode('|', mb_strtolower(trim($types))); |
|
43
|
|
|
$this->field = $field; |
|
44
|
|
|
$this->access = $accessType; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return array|string[] |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getAvailableTypesArray() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->types; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $type |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public function typeOf($type) |
|
60
|
|
|
{ |
|
61
|
|
|
if ($this->inTypesArray($type)) { |
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$typeSynonyms = $this->getVarTypeAliases($type); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($typeSynonyms as $typeSynonym) { |
|
68
|
|
|
if ($this->inTypesArray($typeSynonym)) { |
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this->classOf($type); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param $type |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
private function inTypesArray($type) |
|
81
|
|
|
{ |
|
82
|
|
|
return in_array(mb_strtolower($type), $this->types, true); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param $typeName |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
private function getVarTypeAliases($typeName) |
|
90
|
|
|
{ |
|
91
|
|
|
$typeName = strtolower($typeName); |
|
92
|
|
|
|
|
93
|
|
|
$types = [ |
|
94
|
|
|
'integer' => [ |
|
95
|
|
|
'int', |
|
96
|
|
|
], |
|
97
|
|
|
'boolean' => [ |
|
98
|
|
|
'bool', |
|
99
|
|
|
], |
|
100
|
|
|
'closure' => [ |
|
101
|
|
|
'callable', |
|
102
|
|
|
], |
|
103
|
|
|
'double' => [ |
|
104
|
|
|
'float', |
|
105
|
|
|
], |
|
106
|
|
|
'string' => [], |
|
107
|
|
|
'null' => [], |
|
108
|
|
|
'resource' => [], |
|
109
|
|
|
]; |
|
110
|
|
|
|
|
111
|
|
|
return isset($types[$typeName]) ? $types[$typeName] : []; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $type |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
private function classOf($type) |
|
119
|
|
|
{ |
|
120
|
|
|
foreach ($this->types as $class) { |
|
121
|
|
|
if (is_subclass_of($type, $class)) { |
|
|
|
|
|
|
122
|
|
|
return true; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return false; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
|
|
public function isReadable() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->access === Parser::ACCESS_READ || $this->access === Parser::ACCESS_BOTH; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return bool |
|
139
|
|
|
*/ |
|
140
|
|
|
public function isWritable() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->access === Parser::ACCESS_WRITE || $this->access === Parser::ACCESS_BOTH; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getName() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->field; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|