forked from Respect/Validation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompositeValidatorsBench.php
More file actions
78 lines (69 loc) · 2.38 KB
/
Copy pathCompositeValidatorsBench.php
File metadata and controls
78 lines (69 loc) · 2.38 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
*/
declare(strict_types=1);
namespace Respect\Validation\Benchmarks;
use Generator;
use PhpBench\Attributes as Bench;
use Respect\Validation\Validator;
use Respect\Validation\ValidatorBuilder;
use Respect\Validation\Validators\Alnum;
use Respect\Validation\Validators\Alpha;
use Respect\Validation\Validators\BoolType;
use Respect\Validation\Validators\Digit;
use Respect\Validation\Validators\Even;
use Respect\Validation\Validators\FloatType;
use Respect\Validation\Validators\IntType;
use Respect\Validation\Validators\Negative;
use Respect\Validation\Validators\Positive;
use Respect\Validation\Validators\StringType;
final class CompositeValidatorsBench
{
/** @param array{string, array<Validator>} $params */
#[Bench\ParamProviders(['provideValidatorBuilder'])]
#[Bench\Iterations(5)]
#[Bench\Revs(50)]
#[Bench\Warmup(1)]
#[Bench\Subject]
public function isValid(array $params): void
{
ValidatorBuilder::__callStatic(...$params)->isValid(42);
}
public function provideValidatorBuilder(): Generator
{
yield 'allOf(10)' => ['allOf', $this->buildValidators(10)];
yield 'oneOf(10)' => ['oneOf', $this->buildValidators(10)];
yield 'anyOf(10)' => ['anyOf', $this->buildValidators(10)];
yield 'noneOf(10)' => ['noneOf', $this->buildValidators(10)];
yield 'allOf(100)' => ['allOf', $this->buildValidators(100)];
yield 'oneOf(100)' => ['oneOf', $this->buildValidators(100)];
yield 'anyOf(100)' => ['anyOf', $this->buildValidators(100)];
yield 'noneOf(100)' => ['noneOf', $this->buildValidators(100)];
}
/** @return array<Validator> */
private function buildValidators(int $count): array
{
$validators = [];
for ($i = 0; $i < $count; $i++) {
$validators[] = $this->makeValidator($i);
}
return $validators;
}
private function makeValidator(int $index): Validator
{
return match ($index % 10) {
0 => new IntType(),
1 => new Positive(),
2 => new Negative(),
3 => new Even(),
4 => new FloatType(),
5 => new StringType(),
6 => new Alpha(),
7 => new Alnum(),
8 => new Digit(),
default => new BoolType(),
};
}
}