Issues (32)

src/Resource.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Erykai\Database;
4
5
use PDO;
6
7
/**
8
 * CLASS RESOURCE DATABASE
9
 */
10
abstract class Resource
11
{
12
    use TraitDatabase;
13
    /**
14
     * @var PDO
15
     */
16
    protected PDO $conn;
17
    /**
18
     * @var string
19
     */
20
    protected string $nameId;
21
    /**
22
     * @var string
23
     */
24
    protected string $table;
25
    /**
26
     * @var string
27
     */
28
    protected string $query;
29
    /**
30
     * @var string
31
     */
32
    protected string $queryEnd = "";
33
    /**
34
     * @var mixed
35
     */
36
    protected mixed $data = null;
37
    /**
38
     * @var object|null
39
     */
40
    protected null|object $stmt = null;
41
    /**
42
     * @var string|null
43
     */
44
    protected null|string $columns = null;
45
    /**
46
     * @var string|null
47
     */
48
    protected null|string $values = null;
49
    /**
50
     * @var ?array
51
     */
52
    protected ?array $params;
53
    /**
54
     * @var array
55
     */
56
    protected array $notNull;
57
    /**
58
     * @var object
59
     */
60
    private object $response;
61
    /**
62
     * @param string $table
63
     * @param array $notNull
64
     * @param string $id
65
     */
66
    public function __construct(string $table, array $notNull, string $id = 'id')
67
    {
68
        $this->conn();
69
        $this->nameId = $id;
70
        $this->table = $table;
71
        $this->notNull = $notNull;
72
    }
73
74
    /**
75
     * @return $this|null
76
     */
77
    protected function create(): ?Resource
78
    {
79
        if (!$this->notNull($this->data)) {
80
            return null;
81
        }
82
        if (isset($this->data->scalar)) {
83
            unset($this->data->scalar);
84
        }
85
86
        $this->params = get_object_vars($this->data);
87
        foreach ($this->params as $key => $param) {
88
            $this->columns .= ',' . $key;
89
            $this->values .= ',:' . $key;
90
        }
91
92
        $this->columns = substr($this->columns, 1);
93
        $this->values = substr($this->values, 1);
94
        $this->query = "INSERT INTO $this->table ($this->columns) VALUES ($this->values)";
95
        $this->stmt = $this->conn->prepare($this->query);
96
        $this->bind($this->params);
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return bool|$this
103
     */
104
    protected function update(): bool|static
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STATIC on line 104 at column 38
Loading history...
105
    {
106
        $id = $this->nameId;
107
        if(!isset($this->data->$id)){
108
            $this->setResponse(400, "error","there is no $id field in the table", "error", dynamic: $id);
109
            return false;
110
        }
111
        $this->setColumns($this->data);
112
        $this->setQuery();
113
        return $this;
114
115
    }
116
117
    /**
118
     * @param array $params
119
     */
120
    protected function bind(array $params): void
121
    {
122
        foreach ($params as $key => &$param) {
123
            $this->stmt->bindParam(":$key", $param);
124
        }
125
    }
126
127
    /**
128
     * @param object $data
129
     * @return bool
130
     */
131
    protected function notNull(object $data): bool
132
    {
133
        $data = get_object_vars($data);
134
        foreach ($this->notNull as $key) {
135
            if (!array_key_exists($key, $data) || (!isset($data[$key]) && $data[$key] !== 0)) {
136
                $this->setResponse(400,'error',"the $key is mandatory, it cannot be null or empty", "error", dynamic: $key);
137
                return false;
138
            }
139
        }
140
        return true;
141
    }
142
143
    /**
144
     * @param object $data
145
     */
146
    protected function setColumns(object $data): void
147
    {
148
        $this->params = get_object_vars($data);
149
        foreach ($this->params as $key => $param) {
150
            $this->columns .= "`$key`=:$key,";
151
        }
152
        $this->columns = substr($this->columns, 0, -1);
153
    }
154
155
    /**
156
     *
157
     */
158
    protected function setQuery(): void
159
    {
160
        $id = $this->nameId;
161
        $this->query = "UPDATE $this->table SET  $this->columns WHERE $id = :$id";
162
        $this->stmt = $this->conn->prepare($this->query);
163
        $this->bind($this->params);
164
    }
165
166
    /**
167
     * @return object
168
     */
169
    protected function getResponse(): object
170
    {
171
        return $this->response;
172
    }
173
174
    /**
175
     * @param int $code
176
     * @param string $type
177
     * @param string $text
178
     * @param string $model
179
     * @param object|null $data
180
     * @param string|null $dynamic
181
     */
182
    protected function setResponse(int $code, string $type, string $text, string $model, ?object $data = null, ?string $dynamic = null): void
183
    {
184
        http_response_code($code);
185
        $this->response = (object)[
186
            "code" => $code,
187
            "type" => $type,
188
            "text" => $text,
189
            "model" => $model,
190
            "data" => $data,
191
            "dynamic" => $dynamic
192
        ];
193
    }
194
}