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

GreaterThan::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 GreaterThan::$targetValue}) or within an attribute ({@see GreaterThan::$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 NotEqual::$type} for all possible options.
17
 *
18
 * - `new GreaterThan()` is a shortcut for `new Compare(operator: '>')`.
19
 *
20
 * @see CompareHandler
21
 * @see AbstractCompare
22
 *
23
 * @psalm-import-type WhenType from WhenInterface
24
 */
25 4
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
26
final class GreaterThan extends AbstractCompare
27
{
28
    /**
29
     * @param mixed $targetValue The value to be greater than. When both this property and {@see $targetAttribute} are
30
     * set, this property takes precedence.
31
     * @param string|null $targetAttribute The attribute to be greater than. When both this property and
32
     * {@see $targetValue} are set, the {@see $targetValue} takes precedence.
33
     * @param string $incorrectInputMessage A message used when the input is incorrect.
34
     *
35
     * You may use the following placeholders in the message:
36
     *
37
     * - `{attribute}`: the translated label of the attribute being validated.
38
     * - `{type}`: the type of the value being validated.
39
     * @param string $incorrectDataSetTypeMessage A message used when the value returned from a custom
40
     * data set s not scalar.
41
     *
42
     * You may use the following placeholders in the message:
43
     *
44
     * - `{type}`: type of the value.
45
     * @param string|null $message A message used when the value is not valid.
46
     *
47
     * You may use the following placeholders in the message:
48
     *
49
     * - `{attribute}`: the translated label of the attribute being validated.
50
     * - `{targetValue}`: the value to be compared with.
51
     * - `{targetAttribute}`: the name of the attribute to be compared with.
52
     * - `{targetAttributeValue}`: the value extracted from the attribute to be compared with if this attribute was set.
53
     * - `{targetValueOrAttribute}`: the value to be compared with or, if it's absent, the name of the attribute to be
54 4
     * compared with.
55 1
     * - `{value}`: the value being validated.
56
     *
57
     * When {@see CompareType::ORIGINAL} is used with complex types (neither scalar nor `null`), `{targetValue}`,
58 3
     * `{targetAttributeValue}` and `{targetValueOrAttribute}` parameters might contain the actual type instead of the
59 3
     * value, e.g. "object" for predictable formatting.
60 3
     * @param string $type The type of the values being compared:
61 3
     *
62 3
     * - {@see CompareType::NUMBER}: default, both values will be converted to float numbers before comparison.
63 3
     * - {@see CompareType::ORIGINAL} - compare the values as is.
64 3
     * - {@see CompareType::STRING} - cast both values to strings before comparison.
65
     *
66
     * {@see CompareType::NUMBER} and {@see CompareType::STRING} allow only scalar and `null` values, also objects
67 3
     * implementing {@see Stringable} interface.
68 3
     *
69
     * {@see CompareType::ORIGINAL} allows any values. All PHP comparison rules apply here, see comparison operators -
70
     * {@see https://www.php.net/manual/en/language.operators.comparison.php} and PHP type comparison tables -
71
     * {@see https://www.php.net/manual/en/types.comparisons.php} sections in official PHP documentation.
72 1
     *
73
     * @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type
74 1
     *
75
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty.
76
     * See {@see SkipOnEmptyInterface}.
77
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error.
78
     * See {@see SkipOnErrorInterface}.
79
     * @param Closure|null $when  A callable to define a condition for applying the rule.
80
     * See {@see WhenInterface}.
81
     *
82
     * @psalm-param WhenType $when
83
     */
84
    public function __construct(
85
        mixed $targetValue = null,
86
        string|null $targetAttribute = null,
87
        string $incorrectInputMessage = self::DEFAULT_INCORRECT_INPUT_MESSAGE,
88
        string $incorrectDataSetTypeMessage = self::DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE,
89
        string|null $message = null,
90
        string $type = CompareType::NUMBER,
91
        bool|callable|null $skipOnEmpty = false,
92
        bool $skipOnError = false,
93
        Closure|null $when = null,
94
    ) {
95
        parent::__construct(
96
            targetValue: $targetValue,
97
            targetAttribute: $targetAttribute,
98
            incorrectInputMessage: $incorrectInputMessage,
99
            incorrectDataSetTypeMessage: $incorrectDataSetTypeMessage,
100
            message: $message,
101
            type: $type,
102
            operator: '>',
103
            skipOnEmpty: $skipOnEmpty,
104
            skipOnError: $skipOnError,
105
            when: $when,
106
        );
107
    }
108
}
109