|
1
|
|
|
<?php namespace Limoncello\Validation; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2017 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Limoncello\Validation\Contracts\Execution\ContextStorageInterface; |
|
20
|
|
|
use Limoncello\Validation\Contracts\Rules\RuleInterface; |
|
21
|
|
|
use Limoncello\Validation\Contracts\ValidatorInterface; |
|
22
|
|
|
use Limoncello\Validation\Execution\ContextStorage; |
|
23
|
|
|
use Limoncello\Validation\Validator\BaseValidator; |
|
24
|
|
|
use Limoncello\Validation\Validator\SingleValidation; |
|
25
|
|
|
use Psr\Container\ContainerInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @package Limoncello\Validation |
|
29
|
|
|
*/ |
|
30
|
|
|
class SingleValidator extends BaseValidator |
|
31
|
|
|
{ |
|
32
|
|
|
use SingleValidation; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ContainerInterface|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $container; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param RuleInterface $rule |
|
41
|
|
|
* @param ContainerInterface|null $container |
|
42
|
|
|
*/ |
|
43
|
4 |
|
public function __construct(RuleInterface $rule, ContainerInterface $container = null) |
|
44
|
|
|
{ |
|
45
|
4 |
|
parent::__construct(); |
|
46
|
|
|
|
|
47
|
4 |
|
$this->setRule($rule); |
|
48
|
|
|
|
|
49
|
4 |
|
$this->container = $container; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param RuleInterface $rule |
|
54
|
|
|
* @param ContainerInterface|null $container |
|
55
|
|
|
* |
|
56
|
|
|
* @return ValidatorInterface |
|
57
|
|
|
*/ |
|
58
|
4 |
|
public static function validator(RuleInterface $rule, ContainerInterface $container = null): ValidatorInterface |
|
59
|
|
|
{ |
|
60
|
4 |
|
$validator = new static ($rule, $container); |
|
61
|
|
|
|
|
62
|
4 |
|
return $validator; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritdoc |
|
67
|
|
|
* |
|
68
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
69
|
|
|
*/ |
|
70
|
4 |
|
public function validate($input): bool |
|
71
|
|
|
{ |
|
72
|
4 |
|
if ($this->areAggregatorsDirty() === true) { |
|
73
|
3 |
|
$this->resetAggregators(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
4 |
|
$this->validateSingleImplementation($input, $this->getCaptureAggregator(), $this->getErrorAggregator()); |
|
77
|
4 |
|
$this->markAggregatorsAsDirty(); |
|
78
|
|
|
|
|
79
|
4 |
|
$noErrors = $this->getErrorAggregator()->count() <= 0; |
|
80
|
|
|
|
|
81
|
4 |
|
return $noErrors; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return ContainerInterface|null |
|
86
|
|
|
*/ |
|
87
|
4 |
|
protected function getContainer(): ?ContainerInterface |
|
88
|
|
|
{ |
|
89
|
4 |
|
return $this->container; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* During validation you can pass to rules your custom context which might have any additional |
|
94
|
|
|
* resources needed by your rules (extra properties, database connection settings, container, and etc). |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $blocks |
|
97
|
|
|
* |
|
98
|
|
|
* @return ContextStorageInterface |
|
99
|
|
|
*/ |
|
100
|
4 |
|
protected function createContextStorageFromBlocks(array $blocks): ContextStorageInterface |
|
101
|
|
|
{ |
|
102
|
4 |
|
return new ContextStorage($blocks, $this->getContainer()); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|