aboutsummaryrefslogtreecommitdiff
path: root/srcs/phpmyadmin/vendor/williamdes/mariadb-mysql-kbs/src/SlimData.php
blob: fc7cd42bf32f894df95f7baea732deb4208f051b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types = 1);
namespace Williamdes\MariaDBMySQLKBS;

use \stdClass;
use \JsonSerializable;

class SlimData extends stdClass implements JsonSerializable
{

    /**
     * Variables
     *
     * @var KBEntry[]
     */
    private $vars = array();

    /**
     * File revision
     *
     * @var float
     */
    private $version = 1;

    /**
     * Urls
     *
     * @var string[]
     */
    private $urls = array();

    /**
     * Types of documentation
     *
     * @var array<string, string|int>
     */
    private $types = array("MYSQL" => 1, "MARIADB" => 2);

    /**
     * Types of variables
     *
     * @var array<string, string|int>
     */
    private $varTypes = array(
        "string" => 1,
        "boolean" => 2,
        "integer" => 3,
        "numeric" => 4,
        "enumeration" => 5,
        "set" => 6,
        "directory name" => 7,
        "file name" => 8,
        "byte" => 9
    );

    /**
     * Create a slimData object
     *
     * @param float|null                 $version  The version
     * @param array<string, string>|null $types    The types of documentations
     * @param array<string, string>|null $varTypes The types of variables
     */
    public function __construct(
        ?float $version = null,
        ?array $types = null,
        ?array $varTypes = null
    ) {
        if ($version !== null) {
            $this->version = $version;
        }

        if ($types !== null) {
            $this->types = $types;
        }

        if ($varTypes !== null) {
            $this->varTypes = $varTypes;
        }
    }

    /**
     * Add a variable
     *
     * @param string      $name    The name
     * @param string|null $type    The type
     * @param bool|null   $dynamic Is dynamic
     * @return KBEntry The newly created KBEntry
     */
    public function addVariable(string $name, ?string $type, ?bool $dynamic): KBEntry
    {
        $kbe          = new KBEntry($name, $type, $dynamic);
        $this->vars[] = $kbe;
        return $kbe;
    }

    /**
     * Used for json_encode function
     * This can seem useless, do not remove it.
     *
     * @return array
     */
    public function jsonSerialize(): array
    {
        $outObj = array();
        if (count($this->vars) > 0) {
            $vars = new stdClass();
            foreach ($this->vars as $var) {
                $variable    = new stdClass();
                $variable->d = $var->isDynamic();
                if ($variable->d === null) {
                    unset($variable->d);
                }

                if ($var->getType() !== null) {
                    if (isset($this->varTypes[$var->getType()]) === false) {
                        $this->varTypes[$var->getType()] = "".(count($this->varTypes) + 1);
                    }

                    $variable->t = $this->varTypes[$var->getType()];
                }

                if ($var->hasDocumentations()) {
                    $variable->a = array();
                    foreach ($var->getDocumentations() as $kbd) {
                        $entry    = new stdClass();
                        $entry->a = $kbd->getAnchor();
                        if ($entry->a === null) {
                            unset($entry->a);
                        }
                        if (preg_match("!^(https|http)://mariadb.com!", $kbd->getUrl())) {
                            $entry->t = $this->types["MARIADB"];
                        } elseif (preg_match("!^(https|http)://dev.mysql.com!", $kbd->getUrl())) {
                            $entry->t = $this->types["MYSQL"];
                        }
                        if (isset($entry->t)) {// If has no valid type, skip.
                            //Do not allow other urls.
                            $keyIndex = array_search($kbd->getUrl(), $this->urls);
                            if ($keyIndex === false) {
                                $this->urls[] = $kbd->getUrl();
                            }
                            $keyIndex = array_search($kbd->getUrl(), $this->urls);
                            $entry->u = $keyIndex;

                            $variable->a[] = $entry;
                        }
                    }
                }

                $vars->{$var->getName()} = $variable;
            }
            $outObj['vars'] = $vars;
        }
        $outObj['version'] = $this->version;
        if (count($this->vars) > 0) {
            $outObj['types']    = array_flip($this->types);
            $outObj['varTypes'] = array_flip($this->varTypes);
            $outObj['urls']     = $this->urls;
        }
        return $outObj;
    }

}