1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Validation\Rules\Comparisons; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Validation\Contracts\Errors\ErrorCodes; |
22
|
|
|
use Limoncello\Validation\Contracts\Execution\ContextInterface; |
23
|
|
|
use Limoncello\Validation\I18n\Messages; |
24
|
|
|
use function assert; |
25
|
|
|
use function call_user_func; |
26
|
|
|
use function in_array; |
27
|
|
|
use function is_scalar; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @package Limoncello\Validation |
31
|
|
|
*/ |
32
|
|
|
final class ScalarInValues extends BaseOneValueComparision |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @param mixed $scalars |
36
|
|
|
*/ |
37
|
1 |
|
public function __construct(array $scalars) |
38
|
|
|
{ |
39
|
|
|
assert(call_user_func(function () use ($scalars) { |
40
|
1 |
|
foreach ($scalars as $scalar) { |
41
|
1 |
|
assert(static::isValidType($scalar) === true); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
return true; |
45
|
1 |
|
})); |
46
|
|
|
|
47
|
1 |
|
parent::__construct( |
48
|
1 |
|
$scalars, |
49
|
1 |
|
ErrorCodes::SCALAR_IN_VALUES, |
50
|
1 |
|
Messages::SCALAR_IN_VALUES, |
51
|
1 |
|
$scalars |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
1 |
View Code Duplication |
public static function compare($value, ContextInterface $context): bool |
|
|
|
|
59
|
|
|
{ |
60
|
1 |
|
assert(static::isValidType($value) === true); |
|
|
|
|
61
|
1 |
|
$result = is_scalar($value) === true && in_array($value, static::readValue($context)); |
62
|
|
|
|
63
|
1 |
|
return $result; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param mixed $value |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
1 |
|
private static function isValidType($value): bool |
72
|
|
|
{ |
73
|
1 |
|
return is_scalar($value) === true || $value === null; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Late static binding only has effect in subclasses. A
final
class cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::
withself::
.To learn more about late static binding, please refer to the PHP core documentation.