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
|
|
|
* @param string $type |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function typeOf($type) |
52
|
|
|
{ |
53
|
|
|
if ($this->inTypesArray($type)) return true; |
54
|
|
|
|
55
|
|
|
$typeSynonyms = $this->getVarTypeSynonyms($type); |
56
|
|
|
foreach ($typeSynonyms as $typeSynonym) { |
57
|
|
|
if ($this->inTypesArray($typeSynonym)) return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
foreach ($this->types as $class) { |
61
|
|
|
if (is_subclass_of($type, $class)) true; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function isReadable() |
71
|
|
|
{ |
72
|
|
|
return $this->access === Parser::ACCESS_READ || $this->access === Parser::ACCESS_BOTH; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function isWritable() |
79
|
|
|
{ |
80
|
|
|
return $this->access === Parser::ACCESS_WRITE || $this->access === Parser::ACCESS_BOTH; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getName() |
87
|
|
|
{ |
88
|
|
|
return $this->field; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $typeName |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
private function getVarTypeSynonyms($typeName) |
96
|
|
|
{ |
97
|
|
|
$typeName = strtolower($typeName); |
98
|
|
|
|
99
|
|
|
$types = [ |
100
|
|
|
'integer' => [ |
101
|
|
|
'int' |
102
|
|
|
], |
103
|
|
|
'boolean' => [ |
104
|
|
|
'bool' |
105
|
|
|
], |
106
|
|
|
'closure' => [ |
107
|
|
|
'callable' |
108
|
|
|
], |
109
|
|
|
'double' => [ |
110
|
|
|
'float' |
111
|
|
|
], |
112
|
|
|
'string' => [], |
113
|
|
|
'NULL' => [], |
114
|
|
|
'resource' => [], |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
return isset($types[$typeName]) ? $types[$typeName] : []; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $type |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
private function inTypesArray($type) |
125
|
|
|
{ |
126
|
|
|
return (in_array(mb_strtolower($type), $this->types, true)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|