Issues (73)

codegen/generate.php (1 issue)

Labels
Severity
1
<?php
2
3
require '../vendor/autoload.php';
4
require 'ClientAbstract.php';
5
require 'RequestAbstract.php';
6
require 'ResponseAbstract.php';
7
require 'ConfigAbstract.php';
8
require 'func.php';
9
10
use GuzzleHttp\Client;
11
use carono\etxtru\codegen\ClientAbstract;
12
13
clearFolder('config');
14
clearFolder('request');
15
clearFolder('response');
16
17
18
$apidoc = 'apidoc.html';
19
if (file_exists($apidoc)) {
20
    $content = file_get_contents($apidoc);
21
} else {
22
    $content = (new Client())->get('https://www.etxt.ru/api/')->getBody()->getContents();
23
    file_put_contents($apidoc, $content);
24
}
25
26
27
$query = \phpQuery::newDocumentHTML($content);
0 ignored issues
show
$content of type string is incompatible with the type unknown_type expected by parameter $markup of phpQuery::newDocumentHTML(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
$query = \phpQuery::newDocumentHTML(/** @scrutinizer ignore-type */ $content);
Loading history...
28
$data = [];
29
30
foreach ($query->find('h4') as $title) {
31
    $name = pq($title)->text();
32
    if (strpos($name, '.') === false) {
33
        continue;
34
    }
35
    echo $name . "\n";
36
    $description = $query->find("td:contains('$name')")->next('td')->text();
37
    list($api, $method) = explode('.', $name);
38
39
40
    $params = [];
41
    if (pq($title)->nextAll('h5')->eq(0)->text() === 'Параметры') {
42
        foreach (pq($title)->nextAll('h5:contains("Параметры")')->eq(0)->next('table')->eq(0)->find('tr')->eq(0)->nextAll() as $tr) {
43
            $td = pq($tr)->find('td');
44
            foreach (explode(', ', $td->eq(0)->text()) as $name) {
45
                $params[] = [
46
                    'name' => $name,
47
                    'type' => $td->eq(1)->text(),
48
                    'description' => $td->eq(2)->text(),
49
                    'required' => isRequired($td->eq(2)->text())
50
                ];
51
            }
52
        }
53
    }
54
    $results = [];
55
    foreach (pq($title)->nextAll('h5:contains("Результат")')->next('table')->eq(0)->find('tr')->eq(0)->nextAll() as $tr) {
56
        $td = pq($tr)->find('td');
57
        foreach (explode(', ', $td->eq(0)->text()) as $name) {
58
            $results[] = [
59
                'name' => $name,
60
                'description' => $td->eq(1)->text(),
61
            ];
62
        }
63
    }
64
65
    if (!isset($data[$api])) {
66
        $data[$api] = [
67
            'name' => $api,
68
            'description' => $description,
69
            'methods' => []
70
        ];
71
    }
72
73
    $data[$api]['methods'][] = [
74
        'name' => $method,
75
        'description' => pq($title)->next('p')->text(),
76
        'params' => $params,
77
        'returns' => $results
78
    ];
79
};
80
file_put_contents('data.json', json_encode($data));
81
82
$abstractClient = new ClientAbstract();
83
$abstractClient->renderToFile($data);