1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TgScraper\Parsers; |
4
|
|
|
|
5
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Field |
9
|
|
|
* @package TgScraper\Parsers |
10
|
|
|
*/ |
11
|
|
|
class Field |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Parsed types map. |
16
|
|
|
*/ |
17
|
|
|
public const TYPES = [ |
18
|
|
|
'Integer' => 'int', |
19
|
|
|
'Float' => 'float', |
20
|
|
|
'String' => 'string', |
21
|
|
|
'Boolean' => 'bool', |
22
|
|
|
'True' => 'bool', |
23
|
|
|
'False' => 'bool' |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private string $name; |
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private array $types; |
34
|
|
|
/** |
35
|
|
|
* @var FieldDescription |
36
|
|
|
*/ |
37
|
|
|
private FieldDescription $description; |
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
private bool $optional; |
42
|
|
|
/** |
43
|
|
|
* @var mixed |
44
|
|
|
*/ |
45
|
|
|
private mixed $defaultValue; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $name |
49
|
|
|
* @param string $types |
50
|
|
|
* @param bool $optional |
51
|
|
|
* @param string $description |
52
|
|
|
*/ |
53
|
|
|
public function __construct(string $name, string $types, bool $optional, string $description) |
54
|
|
|
{ |
55
|
|
|
$this->name = $name; |
56
|
|
|
$this->types = $this->parseTypesString($types); |
57
|
|
|
$this->optional = $optional; |
58
|
|
|
$this->description = new FieldDescription($description); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $type |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
private function parseTypeString(string $type): string |
66
|
|
|
{ |
67
|
|
|
if ($type == 'True') { |
68
|
|
|
$this->defaultValue = true; |
69
|
|
|
return self::TYPES['Boolean']; |
70
|
|
|
} elseif ($type == 'False') { |
71
|
|
|
$this->defaultValue = false; |
72
|
|
|
return self::TYPES['Boolean']; |
73
|
|
|
} |
74
|
|
|
$type = trim(str_replace('number', '', $type)); |
75
|
|
|
return trim(str_replace(array_keys(self::TYPES), array_values(self::TYPES), $type)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $text |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
private function parseTypesString(string $text): array |
83
|
|
|
{ |
84
|
|
|
$types = []; |
85
|
|
|
$parts = explode(' or ', $text); |
86
|
|
|
foreach ($parts as $part) { |
87
|
|
|
$part = trim(str_replace(' and', ',', $part)); |
88
|
|
|
$arrays = 0; |
89
|
|
|
while (stripos($part, 'array of') === 0) { |
90
|
|
|
$part = substr($part, 9); |
91
|
|
|
$arrays++; |
92
|
|
|
} |
93
|
|
|
$pieces = explode(',', $part); |
94
|
|
|
foreach ($pieces as $index => $piece) { |
95
|
|
|
$pieces[$index] = $this->parseTypeString($piece); |
96
|
|
|
} |
97
|
|
|
$type = implode('|', $pieces); |
98
|
|
|
for ($i = 0; $i < $arrays; $i++) { |
99
|
|
|
$type = sprintf('Array<%s>', $type); |
100
|
|
|
} |
101
|
|
|
$types[] = $type; |
102
|
|
|
} |
103
|
|
|
return $types; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getName(): string |
110
|
|
|
{ |
111
|
|
|
return $this->name; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getTypes(): array |
118
|
|
|
{ |
119
|
|
|
return $this->types; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function isOptional(): bool |
126
|
|
|
{ |
127
|
|
|
return $this->optional; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function getDefaultValue(): mixed |
134
|
|
|
{ |
135
|
|
|
if (!isset($this->defaultValue)) { |
136
|
|
|
$this->defaultValue = $this->description->getDefaultValue(); |
137
|
|
|
} |
138
|
|
|
return $this->defaultValue; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
#[ArrayShape([ |
145
|
|
|
'name' => "string", |
146
|
|
|
'types' => "array", |
147
|
|
|
'optional' => "bool", |
148
|
|
|
'description' => "string", |
149
|
|
|
'default' => "mixed" |
150
|
|
|
])] public function toArray(): array |
151
|
|
|
{ |
152
|
|
|
$result = [ |
153
|
|
|
'name' => $this->name, |
154
|
|
|
'types' => $this->types, |
155
|
|
|
'optional' => $this->optional, |
156
|
|
|
'description' => (string)$this->description, |
157
|
|
|
]; |
158
|
|
|
$defaultValue = $this->getDefaultValue(); |
159
|
|
|
if (null !== $defaultValue) { |
160
|
|
|
$result['default'] = $defaultValue; |
161
|
|
|
} |
162
|
|
|
return $result; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
} |