Skip to content

Commit 5fdb5aa

Browse files
carloscarucceCarlos Alberto B. Carucce
authored andcommitted
poc - string validator
1 parent 8a72fa9 commit 5fdb5aa

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

composer.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "webdevcave/schema-validator",
3+
"type": "library",
4+
"license": "MIT",
5+
"autoload": {
6+
"psr-4": {
7+
"Webdevcave\\SchemaValidator\\": "src/"
8+
}
9+
},
10+
"require": {
11+
"php": ">=8.1"
12+
}
13+
}

src/Schemas/BaseSchema.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Webdevcave\SchemaValidator\Schemas;
4+
5+
use BadMethodCallException;
6+
use Closure;
7+
8+
/**
9+
* @method string[] errorMessages()
10+
*/
11+
abstract class BaseSchema
12+
{
13+
protected bool $optional = false;
14+
protected ?Closure $callback = null;
15+
16+
/**
17+
* @var string[]
18+
*/
19+
protected array $customMessages = [];
20+
21+
/**
22+
* @var string[]
23+
*/
24+
protected array $errorMessages = [];
25+
26+
/**
27+
* @param string $name
28+
* @param array $arguments
29+
*
30+
* @return mixed
31+
*/
32+
public function __call(string $name, array $arguments): mixed
33+
{
34+
if (!property_exists($this, $name)) {
35+
throw new BadMethodCallException('Call to undefined method '.$name.'()');
36+
}
37+
38+
if (!$arguments) {
39+
return $this->$name;
40+
}
41+
42+
$this->$name = array_shift($arguments);
43+
44+
if (!empty($arguments)) {
45+
$this->customMessages[$name] = array_shift($arguments);
46+
}
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* @return static
53+
*/
54+
public function optional(): static
55+
{
56+
$this->optional = true;
57+
return $this;
58+
}
59+
60+
public function refine(callable $callback, $customMessage = "Custom validation error"): static
61+
{
62+
$this->customMessages['refine'] = $customMessage;
63+
$this->callback = $callback(...);
64+
65+
return $this;
66+
}
67+
68+
/**
69+
* @param mixed $value
70+
*
71+
* @return bool
72+
*/
73+
public function validate(mixed $value): bool
74+
{
75+
$this->errorMessages = [];
76+
77+
if (!$this->optional && empty($value)) {
78+
$this->errorMessages[] = $this->customMessages['required'] ?? 'Field is required';
79+
}
80+
81+
$this->validateSchema($value);
82+
83+
if (!is_null($this->callback)) {
84+
$callback = $this->callback;
85+
if (!$callback($value)) {
86+
$this->errorMessages[] = $this->customMessages['refine'];
87+
}
88+
}
89+
90+
return empty($this->errorMessages);
91+
}
92+
93+
/**
94+
* @param mixed $value
95+
*
96+
* @return bool
97+
*/
98+
abstract protected function validateSchema(mixed $value): bool;
99+
}

src/Schemas/StringSchema.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Webdevcave\SchemaValidator\Schemas;
4+
5+
use Closure;
6+
7+
/**
8+
* @method StringSchema min(int $minLength, string $customMessage = null)
9+
* @method StringSchema max(int $maxLength, string $customMessage = null)
10+
* @method StringSchema pattern(string $pattern, string $customMessage = null)
11+
*/
12+
class StringSchema extends BaseSchema
13+
{
14+
protected ?int $min = null;
15+
protected ?int $max = null;
16+
protected ?string $pattern = null;
17+
18+
/**
19+
* @param mixed $value
20+
*
21+
* @return bool
22+
*/
23+
protected function validateSchema(mixed $value): bool
24+
{
25+
if (!is_string($value)) {
26+
$this->errorMessages[] = $this->customMessages['type']
27+
?? "Value must be a string";
28+
}
29+
30+
if (!is_null($this->min)) {
31+
if (mb_strlen($value) < $this->min) {
32+
$this->errorMessages[] = $this->customMessages['minLength']
33+
?? "Value must be at least {$this->min} characters";
34+
}
35+
}
36+
37+
if (!is_null($this->max)) {
38+
if (mb_strlen($value) > $this->max) {
39+
$this->errorMessages[] = $this->customMessages['maxLength']
40+
?? "Value must be at most {$this->max} characters";
41+
}
42+
}
43+
44+
if (!is_null($this->pattern)) {
45+
if (!preg_match($this->pattern, $value)) {
46+
$this->errorMessages[] = $this->customMessages['pattern']
47+
?? "Value pattern must match required format";
48+
}
49+
}
50+
51+
return empty($this->errorMessages);
52+
}
53+
}

src/Validator.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Webdevcave\SchemaValidator;
4+
5+
use Webdevcave\SchemaValidator\Schemas\StringSchema;
6+
7+
class Validator
8+
{
9+
/**
10+
* @return StringSchema
11+
*/
12+
public static function string(): StringSchema
13+
{
14+
return new StringSchema();
15+
}
16+
}

0 commit comments

Comments
 (0)