Skip to content

Commit 7828d4b

Browse files
Added factory class. Updated all tests to not crash due to factory class integration changes. Updated README to reflect factory class addition
1 parent 3d313c6 commit 7828d4b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+406
-290
lines changed

README.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ A simple validation library that allows you to write custom validators for your
1111
- [Installation](#installation)
1212
- [Usage](#usage)
1313
- [Obtaining a validator](#obtaining-a-validator)
14+
- [Through a factory](#through-a-factory)
1415
- [Configuring array field validation](#configuring-array-field-validation)
1516
- [Configuring custom error messages](#configuring-custom-error-messages)
1617
- [Using the validator](#using-the-validator)
@@ -112,8 +113,10 @@ A validator can be obtained through the `ValidatorBuilder` class:
112113

113114
```php
114115
use PHPValidation\Builders\ValidatorBuilder;
116+
use PHPValidation\Strategies\DefaultValidationStrategy;
115117

116-
$builder = new ValidatorBuilder();
118+
$strategy = new DefaultValidationStrategy();
119+
$builder = new ValidatorBuilder($strategy);
117120

118121
// Your configuration logic here...
119122

@@ -129,7 +132,8 @@ use PHPValidation\Validator;
129132

130133
use function PHPValidation\Functions\required;
131134

132-
$builder = new ValidatorBuilder();
135+
$strategy = new DefaultValidationStrategy();
136+
$builder = new ValidatorBuilder($strategy);
133137

134138
// Configures the field validators for each array field
135139
$builder->setValidators([
@@ -144,7 +148,8 @@ $builder->setErrorMessages([
144148
]);
145149

146150
// Passes a new validation handler/strategy to the actual validator
147-
$builder->setStrategy(new DefaultValidationStrategy());
151+
$newStrategy = // Your custom strategy here...
152+
$builder->setStrategy($newStrategy);
148153

149154
// Registers a new validator class that will be returned when you build the validator
150155
$builder->setValidatorClassName(Validator::class);
@@ -154,6 +159,18 @@ $validator = $builder->build();
154159

155160
**Note**: The builder already comes preconfigured with a strategy and validator class name, the example above just lists all possible configuration options.
156161

162+
#### Through a factory
163+
A validator can also be obtained through the default factory:
164+
165+
```php
166+
namespace PHPValidation\Factories\ValidatorFactory;
167+
168+
$factory = new ValidatorFactory();
169+
$validator = $factory->createDefaultValidator();
170+
```
171+
172+
**Note**: Adding more methods to this default factory class is possible by inheriting it.
173+
157174
### Configuring array field validation
158175
Within the validation builder, an array must be set that defines how a given array should be validated. This uses the following structure:
159176

@@ -190,8 +207,10 @@ Once you have configured the validator builder, the validator can be built by us
190207

191208
```php
192209
use PHPValidation\Builders\ValidatorBuilder;
210+
use PHPValidation\Strategies\DefaultValidationStrategy;
193211

194-
$builder = new ValidatorBuilder();
212+
$strategy = new DefaultValidationStrategy();
213+
$builder = new ValidatorBuilder($strategy);
195214

196215
// Your configuration logic here...
197216

@@ -202,10 +221,12 @@ The validator can then be used to validate an array:
202221

203222
```php
204223
use PHPValidation\Builders\ValidatorBuilder;
224+
use PHPValidation\Strategies\DefaultValidationStrategy;
205225

206226
use function PHPValidation\Functions\required;
207227

208-
$builder = new ValidatorBuilder();
228+
$strategy = new DefaultValidationStrategy();
229+
$builder = new ValidatorBuilder($strategy);
209230

210231
$builder->setValidators([
211232
'field1' => [required()],
@@ -276,7 +297,8 @@ use PHPValidation\Builders\ValidatorBuilder;
276297
use PHPValidation\Strategies\DefaultValidationStrategy;
277298
use PHPValidation\Validator;
278299

279-
$builder = new ValidatorBuilder();
300+
$strategy = new DefaultValidationStrategy();
301+
$builder = new ValidatorBuilder($strategy);
280302

281303
$builder->setValidators([
282304
'field1' => [new RequiredField(), new CustomValidator()],
@@ -294,7 +316,8 @@ use PHPValidation\Validator;
294316

295317
use function PHPValidation\Functions\required;
296318

297-
$builder = new ValidatorBuilder();
319+
$strategy = new DefaultValidationStrategy();
320+
$builder = new ValidatorBuilder($strategy);
298321

299322
$builder->setValidators([
300323
'field1' => [required(), custom_validator()],

phpinsights.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@
9696
'absoluteLineLimit' => 120,
9797
'ignoreComments' => true,
9898
],
99+
\NunoMaduro\PhpInsights\Domain\Insights\ForbiddenNormalClasses::class => [
100+
'exclude' => [
101+
'./src/Factories/ValidatorFactory.php',
102+
],
103+
],
104+
\SlevomatCodingStandard\Sniffs\Operators\DisallowEqualOperatorsSniff::class => [
105+
'exclude' => [
106+
'./src/Fields/EqualsField.php',
107+
],
108+
],
99109
],
100110

101111
/*

src/Builders/ValidatorBuilder.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace PHPValidation\Builders;
66

77
use ErrorException;
8-
use PHPValidation\Strategies\DefaultValidationStrategy;
98
use PHPValidation\Strategies\ValidationStrategyInterface;
109
use PHPValidation\Validator;
1110
use PHPValidation\ValidatorInterface;
@@ -27,9 +26,9 @@ final class ValidatorBuilder
2726
*/
2827
private array $errorMessages = [];
2928

30-
public function __construct()
29+
public function __construct(ValidationStrategyInterface $strategy)
3130
{
32-
$this->strategy = new DefaultValidationStrategy();
31+
$this->strategy = $strategy;
3332
}
3433

3534
/**

src/Factories/ValidatorFactory.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPValidation\Factories;
6+
7+
use PHPValidation\Builders\ValidatorBuilder;
8+
use PHPValidation\Strategies\DefaultValidationStrategy;
9+
use PHPValidation\ValidatorInterface;
10+
11+
class ValidatorFactory
12+
{
13+
/**
14+
* @param array<string, FieldValidatorInterface|array<string, mixed>> $validators An infinite associative array
15+
* where each level has a key => FieldValidatorInterface pair.
16+
* ```
17+
* [
18+
* 'field1' => [required(), notEmpty(), minLength(6), maxLength(32)],
19+
* 'nestedField' => [
20+
* 'field1' => [isNumber(), between(4, 21)],
21+
* ],
22+
* ]
23+
* ```
24+
*
25+
* @param array<string, string|array<string, mixed>> $errorMessages An infinite associative array
26+
* where each field has a key => string pair that displays a custom error message.
27+
* ```
28+
* [
29+
* 'field1' => [
30+
* 'required' => "Field1 is required",
31+
* 'notEmpty' => "Field1 must be filled in",
32+
* 'minLength' => "Field1 must be at least 6 characters long",
33+
* 'maxLength' => "Field1 cannot be longer than 32 characters",
34+
* ],
35+
* 'nestedField' => [
36+
* 'field1' => [
37+
* 'isNumber' => "Field1 must be a number",
38+
* 'between' => "Field1 must be between 4 and 21",
39+
* ],
40+
* ],
41+
* ]
42+
* ```
43+
*/
44+
public function createDefaultValidator(array $validators, array $errorMessages): ValidatorInterface
45+
{
46+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
47+
48+
$builder->setValidators($validators);
49+
$builder->setErrorMessages($errorMessages);
50+
51+
return $builder->build();
52+
}
53+
}

tests/integration/Fields/BetweenFieldTest.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use PHPValidation\Builders\ValidatorBuilder;
9+
use PHPValidation\Strategies\DefaultValidationStrategy;
910
use stdClass;
1011

1112
use function PHPValidation\Functions\between;
@@ -15,7 +16,7 @@ final class BetweenFieldTest extends TestCase
1516
public function testIfIntegerBetweenMinMaxIsValid(): void
1617
{
1718
// Arrange
18-
$builder = new ValidatorBuilder();
19+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
1920

2021
$builder->setValidators([
2122
'field' => [between(0, 100)],
@@ -36,7 +37,7 @@ public function testIfIntegerBetweenMinMaxIsValid(): void
3637
public function testIfIntegerMinIsValid(): void
3738
{
3839
// Arrange
39-
$builder = new ValidatorBuilder();
40+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
4041

4142
$builder->setValidators([
4243
'field' => [between(0, 100)],
@@ -57,7 +58,7 @@ public function testIfIntegerMinIsValid(): void
5758
public function testIfIntegerMaxIsValid(): void
5859
{
5960
// Arrange
60-
$builder = new ValidatorBuilder();
61+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
6162

6263
$builder->setValidators([
6364
'field' => [between(0, 100)],
@@ -78,7 +79,7 @@ public function testIfIntegerMaxIsValid(): void
7879
public function testIfIntegerUnderMinIsInvalid(): void
7980
{
8081
// Arrange
81-
$builder = new ValidatorBuilder();
82+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
8283

8384
$fieldValidator = between(0, 100);
8485

@@ -109,7 +110,7 @@ public function testIfIntegerUnderMinIsInvalid(): void
109110
public function testIfIntegerOverMaxIsInvalid(): void
110111
{
111112
// Arrange
112-
$builder = new ValidatorBuilder();
113+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
113114

114115
$fieldValidator = between(0, 100);
115116

@@ -140,7 +141,7 @@ public function testIfIntegerOverMaxIsInvalid(): void
140141
public function testIfFloatBetweenMinMaxIsValid(): void
141142
{
142143
// Arrange
143-
$builder = new ValidatorBuilder();
144+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
144145

145146
$builder->setValidators([
146147
'field' => [between(0, 100)],
@@ -161,7 +162,7 @@ public function testIfFloatBetweenMinMaxIsValid(): void
161162
public function testIfFloatMinIsValid(): void
162163
{
163164
// Arrange
164-
$builder = new ValidatorBuilder();
165+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
165166

166167
$builder->setValidators([
167168
'field' => [between(0, 100)],
@@ -182,7 +183,7 @@ public function testIfFloatMinIsValid(): void
182183
public function testIfFloatMaxIsValid(): void
183184
{
184185
// Arrange
185-
$builder = new ValidatorBuilder();
186+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
186187

187188
$builder->setValidators([
188189
'field' => [between(0, 100)],
@@ -203,7 +204,7 @@ public function testIfFloatMaxIsValid(): void
203204
public function testIfFloatUnderMinIsInvalid(): void
204205
{
205206
// Arrange
206-
$builder = new ValidatorBuilder();
207+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
207208

208209
$fieldValidator = between(0, 100);
209210

@@ -234,7 +235,7 @@ public function testIfFloatUnderMinIsInvalid(): void
234235
public function testIfFloatOverMaxIsInvalid(): void
235236
{
236237
// Arrange
237-
$builder = new ValidatorBuilder();
238+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
238239

239240
$fieldValidator = between(0, 100);
240241

@@ -265,7 +266,7 @@ public function testIfFloatOverMaxIsInvalid(): void
265266
public function testIfStringBetweenMinMaxIsValid(): void
266267
{
267268
// Arrange
268-
$builder = new ValidatorBuilder();
269+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
269270

270271
$builder->setValidators([
271272
'field' => [between(0, 100)],
@@ -286,7 +287,7 @@ public function testIfStringBetweenMinMaxIsValid(): void
286287
public function testIfStringMinIsValid(): void
287288
{
288289
// Arrange
289-
$builder = new ValidatorBuilder();
290+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
290291

291292
$builder->setValidators([
292293
'field' => [between(0, 100)],
@@ -307,7 +308,7 @@ public function testIfStringMinIsValid(): void
307308
public function testIfStringMaxIsValid(): void
308309
{
309310
// Arrange
310-
$builder = new ValidatorBuilder();
311+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
311312

312313
$builder->setValidators([
313314
'field' => [between(0, 100)],
@@ -328,7 +329,7 @@ public function testIfStringMaxIsValid(): void
328329
public function testIfStringUnderMinIsInvalid(): void
329330
{
330331
// Arrange
331-
$builder = new ValidatorBuilder();
332+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
332333

333334
$fieldValidator = between(0, 100);
334335

@@ -359,7 +360,7 @@ public function testIfStringUnderMinIsInvalid(): void
359360
public function testIfStringOverMaxIsInvalid(): void
360361
{
361362
// Arrange
362-
$builder = new ValidatorBuilder();
363+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
363364

364365
$fieldValidator = between(0, 100);
365366

@@ -390,7 +391,7 @@ public function testIfStringOverMaxIsInvalid(): void
390391
public function testIfNonNumericStringFieldIsInvalid(): void
391392
{
392393
// Arrange
393-
$builder = new ValidatorBuilder();
394+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
394395

395396
$fieldValidator = between(0, 100);
396397

@@ -421,7 +422,7 @@ public function testIfNonNumericStringFieldIsInvalid(): void
421422
public function testIfNonNumericStringWithNumbersFieldIsInvalid(): void
422423
{
423424
// Arrange
424-
$builder = new ValidatorBuilder();
425+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
425426

426427
$fieldValidator = between(0, 100);
427428

@@ -452,7 +453,7 @@ public function testIfNonNumericStringWithNumbersFieldIsInvalid(): void
452453
public function testIfArrayValueIsInvalid(): void
453454
{
454455
// Arrange
455-
$builder = new ValidatorBuilder();
456+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
456457

457458
$fieldValidator = between(0, 100);
458459

@@ -483,7 +484,7 @@ public function testIfArrayValueIsInvalid(): void
483484
public function testIfObjectValueIsInvalid(): void
484485
{
485486
// Arrange
486-
$builder = new ValidatorBuilder();
487+
$builder = new ValidatorBuilder(new DefaultValidationStrategy());
487488

488489
$fieldValidator = between(0, 100);
489490

0 commit comments

Comments
 (0)