-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSimplePropertyValidatorFactory.php
More file actions
57 lines (46 loc) · 1.74 KB
/
SimplePropertyValidatorFactory.php
File metadata and controls
57 lines (46 loc) · 1.74 KB
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
<?php
declare(strict_types=1);
namespace PHPModelGenerator\Model\Validator\Factory;
use PHPModelGenerator\Exception\SchemaException;
use PHPModelGenerator\Model\Property\PropertyInterface;
use PHPModelGenerator\Model\Schema;
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;
abstract class SimplePropertyValidatorFactory extends AbstractValidatorFactory
{
public function modify(
SchemaProcessor $schemaProcessor,
Schema $schema,
PropertyInterface $property,
JsonSchema $propertySchema,
): void {
if (!$this->hasValidValue($property, $propertySchema)) {
return;
}
$property->addValidator(
$this->getValidator($property, $propertySchema->getJson()[$this->key]),
);
}
protected function hasValidValue(PropertyInterface $property, JsonSchema $propertySchema): bool
{
$json = $propertySchema->getJson();
if (!isset($json[$this->key])) {
return false;
}
if (!$this->isValueValid($json[$this->key])) {
throw new SchemaException(
sprintf(
"Invalid %s (%s) for property '%s' in file %s",
$this->key,
str_replace("\n", '', var_export($json[$this->key], true)),
$property->getName(),
$propertySchema->getFile(),
),
);
}
return true;
}
abstract protected function isValueValid(mixed $value): bool;
abstract protected function getValidator(PropertyInterface $property, mixed $value): PropertyValidatorInterface;
}