|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TgScraper\Parsers; |
|
4
|
|
|
|
|
5
|
|
|
use voku\helper\HtmlDomParser; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ObjectDescription |
|
9
|
|
|
* @package TgScraper\Parsers |
|
10
|
|
|
*/ |
|
11
|
|
|
class ObjectDescription |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private array $types; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $description |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(string $description) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->types = self::parseReturnTypes($description); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $description |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
|
|
private static function parseReturnTypes(string $description): array |
|
32
|
|
|
{ |
|
33
|
|
|
$returnTypes = []; |
|
34
|
|
|
$phrases = explode('.', $description); |
|
35
|
|
|
$phrases = array_filter( |
|
36
|
|
|
$phrases, |
|
37
|
|
|
function ($phrase) { |
|
38
|
|
|
return (false !== stripos($phrase, 'returns') or false !== stripos($phrase, 'is returned')); |
|
39
|
|
|
} |
|
40
|
|
|
); |
|
41
|
|
|
foreach ($phrases as $phrase) { |
|
42
|
|
|
$dom = HtmlDomParser::str_get_html($phrase); |
|
43
|
|
|
$a = $dom->findMulti('a'); |
|
44
|
|
|
$em = $dom->findMulti('em'); |
|
45
|
|
|
foreach ($a as $element) { |
|
46
|
|
|
if ($element->text() == 'Messages') { |
|
47
|
|
|
$returnTypes[] = 'Array<Message>'; |
|
48
|
|
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
$arrays = substr_count(strtolower($phrase), 'array'); |
|
51
|
|
|
$returnType = $element->text(); |
|
52
|
|
|
for ($i = 0; $i < $arrays; $i++) { |
|
53
|
|
|
$returnType = sprintf('Array<%s>', $returnType); |
|
54
|
|
|
} |
|
55
|
|
|
$returnTypes[] = $returnType; |
|
56
|
|
|
} |
|
57
|
|
|
foreach ($em as $element) { |
|
58
|
|
|
if (in_array($element->text(), ['False', 'force', 'Array'])) { |
|
59
|
|
|
continue; |
|
60
|
|
|
} |
|
61
|
|
|
$type = str_replace(['True', 'Int', 'String'], ['bool', 'int', 'string'], $element->text()); |
|
62
|
|
|
$returnTypes[] = $type; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
return $returnTypes; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getTypes(): array |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->types; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
} |