Passed
Push — master ( e05223...e18482 )
by
unknown
06:52 queued 03:38
created

Equal::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use Yiisoft\Validator\WhenInterface;
10
11
/**
12
 * Defines validation options to check that the specified value is equal to "target" value provided directly
13
 * ({@see Equal::$targetValue}) or within an attribute ({@see Equal::$targetAttribute}).
14
 *
15
 * The default comparison is based on number values (including float values). It's also possible to compare values as
16
 * strings byte by byte and compare original values as is. See {@see Equal::$type} for all possible options.
17
 *
18
 * - `new Equal()` is a shortcut for `new Compare(operator: '==')`.
19
 * - `new Equal(strict:true)` is a shortcut for `new Compare(operator: '===')`.
20
 *
21
 * @see CompareHandler
22
 * @see AbstractCompare
23
 *
24
 * @psalm-import-type WhenType from WhenInterface
25 15
 */
26
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
27
final class Equal extends AbstractCompare
28
{
29
    /**
30
     * @param mixed $targetValue The value to be equal to. When both this property and {@see $targetAttribute} are set,
31
     * this property takes precedence.
32
     * @param string|null $targetAttribute The attribute to be equal to. When both this property and {@see $targetValue}
33
     * are set, the {@see $targetValue} takes precedence.
34
     * @param string $incorrectInputMessage A message used when the input is incorrect.
35
     *
36
     * You may use the following placeholders in the message:
37
     *
38
     * - `{attribute}`: the translated label of the attribute being validated.
39
     * - `{type}`: the type of the value being validated.
40
     * @param string $incorrectDataSetTypeMessage A message used when the value returned from a custom
41
     * data set is not a scalar.
42
     *
43
     * You may use the following placeholders in the message:
44
     *
45
     * - `{type}`: type of the value.
46
     * @param string|null $message A message used when the value is not valid.
47
     *
48
     * You may use the following placeholders in the message:
49
     *
50
     * - `{attribute}`: the translated label of the attribute being validated.
51
     * - `{targetValue}`: the value to be compared with.
52
     * - `{targetAttribute}`: the name of the attribute to be compared with.
53
     * - `{targetAttributeValue}`: the value extracted from the attribute to be compared with if this attribute was set.
54
     * - `{targetValueOrAttribute}`: the value to be compared with or, if it's absent, the name of the attribute to be
55
     * compared with.
56
     * - `{value}`: the value being validated.
57
     *
58 15
     * When {@see CompareType::ORIGINAL} is used with complex types (neither scalar nor `null`), `{targetValue}`,
59 1
     * `{targetAttributeValue}` and `{targetValueOrAttribute}` parameters might contain the actual type instead of the
60
     * value, e.g. "object" for predictable formatting.
61
     * @param string $type The type of the values being compared:
62 14
     *
63 14
     * - {@see CompareType::NUMBER}: default, both values will be converted to float numbers before comparison.
64 14
     * - {@see CompareType::ORIGINAL} - compare the values as is.
65 14
     * - {@see CompareType::STRING} - cast both values to strings before comparison.
66 14
     *
67 14
     * {@see CompareType::NUMBER} and {@see CompareType::STRING} allow only scalar and `null` values, also objects
68 14
     * implementing {@see Stringable} interface.
69 14
     *
70
     * {@see CompareType::ORIGINAL} allows any values. All PHP comparison rules apply here, see comparison operators -
71 14
     * {@see https://www.php.net/manual/en/language.operators.comparison.php} and PHP type comparison tables -
72 14
     * {@see https://www.php.net/manual/en/types.comparisons.php} sections in official PHP documentation.
73
     *
74
     * @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type
75
     *
76 1
     * @param bool $strict Whether to check strictly without type juggling.
77
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty.
78 1
     * See {@see SkipOnEmptyInterface}.
79
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error.
80
     * See {@see SkipOnErrorInterface}.
81
     * @param Closure|null $when A callable to define a condition for applying the rule.
82
     * See {@see WhenInterface}.
83
     *
84
     * @psalm-param WhenType $when
85
     */
86
    public function __construct(
87
        mixed $targetValue = null,
88
        ?string $targetAttribute = null,
89
        string $incorrectInputMessage = self::DEFAULT_INCORRECT_INPUT_MESSAGE,
90
        string $incorrectDataSetTypeMessage = self::DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE,
91
        string|null $message = null,
92
        string $type = CompareType::NUMBER,
93
        bool $strict = false,
94
        bool|callable|null $skipOnEmpty = false,
95
        bool $skipOnError = false,
96
        Closure|null $when = null,
97
    ) {
98
        parent::__construct(
99
            targetValue: $targetValue,
100
            targetAttribute: $targetAttribute,
101
            incorrectInputMessage: $incorrectInputMessage,
102
            incorrectDataSetTypeMessage: $incorrectDataSetTypeMessage,
103
            message: $message,
104
            type: $type,
105
            operator: $strict ? '===' : '==',
106
            skipOnEmpty: $skipOnEmpty,
107
            skipOnError: $skipOnError,
108
            when: $when,
109
        );
110
    }
111
}
112