|
1
|
|
|
<?php namespace Limoncello\Validation\Rules\Comparisons; |
|
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\Blocks\ExecutionBlockInterface; |
|
20
|
|
|
use Limoncello\Validation\Contracts\Execution\ContextInterface; |
|
21
|
|
|
use Limoncello\Validation\Contracts\Rules\ComparisionInterface; |
|
22
|
|
|
use Limoncello\Validation\Rules\BaseRule; |
|
23
|
|
|
use Limoncello\Validation\Rules\Generic\Fail; |
|
24
|
|
|
use Limoncello\Validation\Rules\Generic\IfOperator; |
|
25
|
|
|
use Limoncello\Validation\Rules\Generic\Success; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @package Limoncello\Validation |
|
29
|
|
|
* |
|
30
|
|
|
* @SuppressWarnings(PHPMD.NumberOfChildren) |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract class BaseOneValueComparision extends BaseRule implements ComparisionInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Property key. |
|
36
|
|
|
*/ |
|
37
|
|
|
const PROPERTY_VALUE = self::PROPERTY_LAST + 1; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
private $value; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var int |
|
46
|
|
|
*/ |
|
47
|
|
|
private $errorCode; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var mixed |
|
51
|
|
|
*/ |
|
52
|
|
|
private $errorContext; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param mixed $value |
|
56
|
|
|
* @param int $errorCode |
|
57
|
|
|
* @param mixed $errorContext |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct($value, int $errorCode, $errorContext = null) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->value = $value; |
|
62
|
|
|
$this->errorCode = $errorCode; |
|
63
|
|
|
$this->errorContext = $errorContext; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritdoc |
|
68
|
|
|
*/ |
|
69
|
|
View Code Duplication |
public function toBlock(): ExecutionBlockInterface |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
$operator = new IfOperator( |
|
72
|
|
|
[static::class, 'compare'], |
|
73
|
|
|
new Success(), |
|
74
|
|
|
new Fail($this->getErrorCode(), $this->getErrorContext()), |
|
75
|
|
|
[static::PROPERTY_VALUE => $this->getValue()] |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
$operator->setParent($this); |
|
79
|
|
|
if ($this->isCaptureEnabled() === true) { |
|
80
|
|
|
$operator->enableCapture(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return ($operator)->toBlock(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getValue() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->value; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return int |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function getErrorCode(): int |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->errorCode; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getErrorContext() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->errorContext; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param ContextInterface $context |
|
112
|
|
|
* |
|
113
|
|
|
* @return mixed |
|
114
|
|
|
*/ |
|
115
|
|
|
protected static function readValue(ContextInterface $context) |
|
116
|
|
|
{ |
|
117
|
|
|
$limit = $context->getProperties()->getProperty(static::PROPERTY_VALUE); |
|
118
|
|
|
|
|
119
|
|
|
return $limit; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.