Skip to content

Commit 50b27f8

Browse files
committed
The validation class was written and included in the DB layer.
1 parent 0dfee42 commit 50b27f8

7 files changed

Lines changed: 441 additions & 218 deletions

File tree

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"minimum-stability": "stable",
2525
"require": {
2626
"php": ">=7.4",
27-
"ext-pdo": "*",
28-
"initphp/validation": "^1.0"
27+
"ext-pdo": "*"
2928
},
3029
"require-dev": {
3130
"phpunit/phpunit": "9.5"

src/DB.php

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace InitPHP\Database;
1717

18-
use InitPHP\Database\Exceptions\DatabaseInvalidArgumentException;
18+
use InitPHP\Database\Validation\Validation;
1919
use \PDO;
2020
use \InitPHP\Database\Connection\{Connection, ConnectionInterface};
2121
use \InitPHP\Database\DataMapper\{DataMapper, DataMapperInterface};
@@ -57,10 +57,19 @@ class DB
5757
'updatedField' => null,
5858
'deletedField' => null,
5959
'timestampFormat' => 'c',
60+
'validation' => [
61+
'methods' => [],
62+
'messages' => [],
63+
'labels' => [],
64+
],
6065
];
6166

6267
private bool $isOnlyDeletes = false;
6368

69+
private Validation $_validation;
70+
71+
private array $errors = [];
72+
6473
public function __construct(array $configurations)
6574
{
6675
$this->configurations = \array_merge($this->configurations, $configurations);
@@ -85,6 +94,7 @@ public function __construct(array $configurations)
8594
'schema' => ($this->configurations['tableSchema'] ?? null),
8695
'schemaID' => ($this->configurations['tableSchemaID'] ?? null),
8796
]);
97+
$this->_validation = new Validation($this->configurations['validation']['methods'], $this->configurations['validation']['messages'], $this->configurations['validation']['labels'], $this);
8898
}
8999

90100
public function __call($name, $arguments)
@@ -153,6 +163,18 @@ public function getSchemaID()
153163
return $this->configurations['tableSchemaID'];
154164
}
155165

166+
public function isError(): bool
167+
{
168+
$this->get_error_merge();
169+
return !empty($this->errors);
170+
}
171+
172+
public function getError(): array
173+
{
174+
$this->get_error_merge();
175+
return $this->errors;
176+
}
177+
156178
/**
157179
* @return ConnectionInterface
158180
*/
@@ -174,34 +196,55 @@ public function getDataMapper(): DataMapperInterface
174196
*/
175197
public function getQueryBuilder(): QueryBuilderInterface
176198
{
177-
$this->is_get_execute = false;
178199
return $this->_queryBuilder;
179200
}
180201

202+
public function getValidation(): Validation
203+
{
204+
return $this->_validation;
205+
}
206+
181207
/**
182208
* @param array $fields
183209
* @return bool
210+
* @throws Exceptions\ValidationException
184211
*/
185212
public function create(array $fields)
186213
{
187214
$data = []; $parameters = [];
188215
$isCreatedField = !empty($this->configurations['createdField']);
189216
if(\count($fields) === \count($fields, \COUNT_RECURSIVE)){
217+
$this->getValidation()->setData($fields);
190218
foreach ($fields as $column => $value) {
219+
if($this->getValidation()->validation($column, null) === FALSE){
220+
$this->errors[] = $this->getValidation()->getError();
221+
return false;
222+
}
191223
$data[$column] = ':'.$column;
192224
$parameters[':' . $column] = $value;
193225
}
226+
if(empty($data)){
227+
return false;
228+
}
194229
if($isCreatedField){
195230
$data[$this->configurations['createdField']] = ':' . $this->configurations['createdField'];
196231
}
197232
}else{
198233
$i = 0;
199234
foreach ($fields as $row) {
200235
$data[$i] = [];
236+
$this->getValidation()->setData($row);
201237
foreach ($row as $column => $value) {
238+
if($this->getValidation()->validation($column, null) === FALSE){
239+
$this->errors[] = $this->getValidation()->getError();
240+
return false;
241+
}
202242
$data[$i][$column] = ':' . $column . '_' . $i;
203243
$parameters[':' . $column . '_' . $i] = $value;
204244
}
245+
if(empty($data)){
246+
continue;
247+
}
205248
if($isCreatedField){
206249
$data[$i][$this->configurations['createdField']] = ':' . $this->configurations['createdField'];
207250
}
@@ -250,16 +293,23 @@ public function readOne(array $selector = [], array $conditions = [], array $par
250293
*/
251294
public function update(array $fields)
252295
{
296+
$schemaID = null;
253297
if(!empty($this->getSchemaID()) && isset($fields[$this->getSchemaID()])){
298+
$schemaID = $fields[$this->getSchemaID()];
254299
$this->getQueryBuilder()->where($this->getSchemaID(), ':' . $this->getSchemaID());
255-
$this->getDataMapper()->setParameter(':' . $this->getSchemaID(), $fields[$this->getSchemaID()]);
300+
$this->getDataMapper()->setParameter(':' . $this->getSchemaID(), $schemaID);
256301
unset($fields[$this->getSchemaID()]);
257302
}
258303
if(empty($fields)){
259304
return false;
260305
}
306+
$this->getValidation()->setData($fields);
261307
$data = [];
262308
foreach ($fields as $column => $value) {
309+
if($this->getValidation()->validation($column, $schemaID) === FALSE){
310+
$this->errors[] = $this->getValidation()->getError();
311+
return false;
312+
}
263313
$data[$column] = ':' . $column;
264314
$this->getDataMapper()->setParameter(':' . $column, $value);
265315
}
@@ -430,4 +480,12 @@ private function deletedFieldBuild(bool $reset = true): void
430480
}
431481
}
432482

483+
private function get_error_merge(): void
484+
{
485+
$error = $this->getDataMapper()->lastError();
486+
if(!empty($error) && !\in_array($error, $this->errors)){
487+
$this->errors[] = $error;
488+
}
489+
}
490+
433491
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* ValidationException.php
4+
*
5+
* This file is part of Database.
6+
*
7+
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
8+
* @copyright Copyright © 2022 Muhammet ŞAFAK
9+
* @license ./LICENSE MIT
10+
* @version 1.1.7
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace InitPHP\Database\Exceptions;
17+
18+
class ValidationException extends \Exception
19+
{
20+
}

src/Helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static function isSQLParameterOrFunction($value): bool
104104
}
105105
return ($value === '?'
106106
|| (bool)\preg_match('/^:[\w]+$/', $value)
107-
|| (bool)\preg_match('/^[a-zA-Z\_]+\(\)$/', $value));
107+
|| (bool)\preg_match('/^[a-zA-Z_]+\(\)$/', $value));
108108
}
109109

110110
public static function queryBindParameter($value, string $syntax = '{value}'): string

0 commit comments

Comments
 (0)