Skip to content

Commit f1f7802

Browse files
committed
Infrastructure for extensible format string validators
1 parent 4b805d9 commit f1f7802

File tree

4 files changed

+123
-9
lines changed

4 files changed

+123
-9
lines changed

src/Model/GeneratorConfiguration.php

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use PHPModelGenerator\Exception\InvalidFilterException;
88
use PHPModelGenerator\Filter\FilterInterface;
99
use PHPModelGenerator\Filter\TransformingFilterInterface;
10+
use PHPModelGenerator\Format\FormatValidatorFromRegEx;
11+
use PHPModelGenerator\Format\FormatValidatorInterface;
1012
use PHPModelGenerator\PropertyProcessor\Filter\DateTimeFilter;
1113
use PHPModelGenerator\PropertyProcessor\Filter\NotEmptyFilter;
1214
use PHPModelGenerator\PropertyProcessor\Filter\TrimFilter;
@@ -39,10 +41,14 @@ class GeneratorConfiguration
3941
protected $errorRegistryClass = ErrorRegistryException::class;
4042
/** @var bool */
4143
protected $serialization = false;
44+
4245
/** @var ClassNameGeneratorInterface */
4346
protected $classNameGenerator;
47+
4448
/** @var FilterInterface[] */
4549
protected $filter;
50+
/** @var FormatValidatorInterface[] */
51+
protected $formats;
4652

4753
/**
4854
* GeneratorConfiguration constructor.
@@ -51,10 +57,9 @@ public function __construct()
5157
{
5258
$this->classNameGenerator = new ClassNameGenerator();
5359

54-
$this
55-
->addFilter(new DateTimeFilter())
56-
->addFilter(new NotEmptyFilter())
57-
->addFilter(new TrimFilter());
60+
// add all built-in filter and format validators
61+
$this->initFilter();
62+
$this->initFormatValidator();
5863
}
5964

6065
/**
@@ -90,6 +95,31 @@ public function addFilter(FilterInterface $filter): self
9095
return $this;
9196
}
9297

98+
/**
99+
* Add an additional format
100+
*
101+
* @param string $formatKey
102+
* @param FormatValidatorInterface $format
103+
*
104+
* @return $this
105+
*/
106+
public function addFormat(string $formatKey, FormatValidatorInterface $format): self
107+
{
108+
$this->formats[$formatKey] = $format;
109+
110+
return $this;
111+
}
112+
113+
/**
114+
* @param string $formatKey
115+
*
116+
* @return FormatValidatorInterface|null
117+
*/
118+
public function getFormat(string $formatKey): ?FormatValidatorInterface
119+
{
120+
return $this->formats[$formatKey] ?? null;
121+
}
122+
93123
/**
94124
* @param array $callback
95125
* @param string $message
@@ -318,4 +348,17 @@ public function setImplicitNull(bool $allowImplicitNull): self
318348

319349
return $this;
320350
}
351+
352+
private function initFilter(): void
353+
{
354+
$this
355+
->addFilter(new DateTimeFilter())
356+
->addFilter(new NotEmptyFilter())
357+
->addFilter(new TrimFilter());
358+
}
359+
360+
// TODO: add builtin format validators
361+
private function initFormatValidator(): void
362+
{
363+
}
321364
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Model\Validator;
6+
7+
use PHPModelGenerator\Exception\String\FormatException;
8+
use PHPModelGenerator\Format\FormatValidatorFromRegEx;
9+
use PHPModelGenerator\Format\FormatValidatorInterface;
10+
use PHPModelGenerator\Model\Property\PropertyInterface;
11+
12+
/**
13+
* Class FormatValidator
14+
*
15+
* @package PHPModelGenerator\Model\Validator
16+
*/
17+
class FormatValidator extends AbstractPropertyValidator
18+
{
19+
/** @var FormatValidatorInterface */
20+
protected $validator;
21+
22+
/**
23+
* FormatValidator constructor.
24+
*
25+
* @param PropertyInterface $property
26+
* @param FormatValidatorInterface $validator
27+
* @param array $exceptionParams
28+
*/
29+
public function __construct(
30+
PropertyInterface $property,
31+
FormatValidatorInterface $validator,
32+
array $exceptionParams = []
33+
) {
34+
$this->validator = $validator;
35+
36+
parent::__construct($property, FormatException::class, $exceptionParams);
37+
}
38+
39+
/**
40+
* Get the source code for the check to perform
41+
*
42+
* @return string
43+
*/
44+
public function getCheck(): string
45+
{
46+
return $this->validator instanceof FormatValidatorFromRegEx
47+
? sprintf(
48+
'!\%s::validate($value, %s)',
49+
get_class($this->validator),
50+
var_export($this->validator->getPattern(), true)
51+
)
52+
: sprintf('!\%s::validate($value)', get_class($this->validator));
53+
}
54+
}

src/Model/Validator/PropertyValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(
3636
}
3737

3838
/**
39-
* Get the source code for the coeck to perform
39+
* Get the source code for the check to perform
4040
*
4141
* @return string
4242
*/

src/PropertyProcessor/Property/StringProcessor.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPModelGenerator\Exception\String\PatternException;
1111
use PHPModelGenerator\Model\Property\PropertyInterface;
1212
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
13+
use PHPModelGenerator\Model\Validator\FormatValidator;
1314
use PHPModelGenerator\Model\Validator\PropertyValidator;
1415

1516
/**
@@ -22,6 +23,7 @@ class StringProcessor extends AbstractTypedValueProcessor
2223
protected const TYPE = 'string';
2324

2425
protected const JSON_FIELD_PATTERN = 'pattern';
26+
protected const JSON_FIELD_FORMAT = 'format';
2527
protected const JSON_FIELD_MIN_LENGTH = 'minLength';
2628
protected const JSON_FIELD_MAX_LENGTH = 'maxLength';
2729

@@ -100,23 +102,38 @@ protected function addLengthValidator(PropertyInterface $property, JsonSchema $p
100102
}
101103

102104
/**
103-
* TODO: implement format validations
104-
*
105105
* @param PropertyInterface $property
106106
* @param JsonSchema $propertySchema
107107
*
108108
* @throws SchemaException
109109
*/
110110
protected function addFormatValidator(PropertyInterface $property, JsonSchema $propertySchema): void
111111
{
112-
if (isset($propertySchema->getJson()['format'])) {
112+
if (!isset($propertySchema->getJson()[self::JSON_FIELD_FORMAT])) {
113+
return;
114+
}
115+
116+
$formatValidator = $this->schemaProcessor
117+
->getGeneratorConfiguration()
118+
->getFormat($propertySchema->getJson()[self::JSON_FIELD_FORMAT]);
119+
120+
if (!$formatValidator) {
113121
throw new SchemaException(
114122
sprintf(
115-
'Format is currently not supported for property %s in file %s',
123+
'Unsupported format %s for property %s in file %s',
124+
$propertySchema->getJson()[self::JSON_FIELD_FORMAT],
116125
$property->getName(),
117126
$propertySchema->getFile()
118127
)
119128
);
120129
}
130+
131+
$property->addValidator(
132+
new FormatValidator(
133+
$property,
134+
$formatValidator,
135+
[$propertySchema->getJson()[self::JSON_FIELD_FORMAT]]
136+
)
137+
);
121138
}
122139
}

0 commit comments

Comments
 (0)