Passed
Pull Request — master (#248)
by Rustam
03:00
created

GreaterThan::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 24
ccs 10
cts 10
cp 1
rs 9.6333
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use JetBrains\PhpStorm\ArrayShape;
10
use RuntimeException;
11
use Yiisoft\Validator\ParametrizedRuleInterface;
12
use Yiisoft\Validator\BeforeValidationInterface;
13
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
14
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
15
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
16
use Yiisoft\Validator\ValidationContext;
17
18
/**
19
 * Validates if the specified value is greater than another value or attribute.
20
 *
21
 * The value being validated with {@see GreaterThan::$targetValue} or {@see GreaterThan::$targetAttribute}, which
22
 * is set in the constructor.
23
 *
24
 * The default validation function is based on string values, which means the values
25
 * are equals byte by byte. When validating numbers, make sure to change {@see GreaterThan::$type} to
26
 * {@see GreaterThan::TYPE_NUMBER} to enable numeric validation.
27
 */
28
#[Attribute(Attribute::TARGET_PROPERTY)]
29
final class GreaterThan implements ParametrizedRuleInterface, BeforeValidationInterface
30
{
31
    use BeforeValidationTrait;
32
    use HandlerClassNameTrait;
33
    use RuleNameTrait;
34
35
    /**
36
     * Constant for specifying validation as string values.
37
     * No conversion will be done before validation.
38
     *
39
     * @see $type
40
     */
41
    public const TYPE_STRING = 'string';
42
    /**
43
     * Constant for specifying validation as numeric values.
44
     * String values will be converted into numbers before validation.
45
     *
46
     * @see $type
47
     */
48
    public const TYPE_NUMBER = 'number';
49
50 1
    public function __construct(
51
        /**
52
         * @var mixed the constant value to be greater than. When both this property
53
         * and {@see $targetAttribute} are set, this property takes precedence.
54
         */
55
        private $targetValue = null,
56
        /**
57
         * @var string|null the attribute to be greater than. When both this property
58
         * and {@see $targetValue} are set, the {@see $targetValue} takes precedence.
59
         */
60
        private ?string $targetAttribute = null,
61
        /**
62
         * @var string|null user-defined error message
63
         */
64
        private ?string $message = null,
65
        /**
66
         * @var string the type of the values being validated.
67
         */
68
        private string $type = self::TYPE_STRING,
69
        private bool $skipOnEmpty = false,
70
        private bool $skipOnError = false,
71
        /**
72
         * @var Closure(mixed, ValidationContext):bool|null
73
         */
74
        private ?Closure $when = null,
75
    ) {
76 1
        if ($this->targetValue === null && $this->targetAttribute === null) {
77
            throw new RuntimeException('Either "targetValue" or "targetAttribute" must be specified.');
78
        }
79
    }
80
81
    /**
82
     * @return mixed
83
     */
84 6
    public function getTargetValue(): mixed
85
    {
86 6
        return $this->targetValue;
87
    }
88
89
    /**
90
     * @return string|null
91
     */
92 1
    public function getTargetAttribute(): ?string
93
    {
94 1
        return $this->targetAttribute;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 6
    public function getType(): string
101
    {
102 6
        return $this->type;
103
    }
104
105 9
    public function getMessage(): string
106
    {
107 9
        return $this->message ?? 'Value must be greater than "{targetValueOrAttribute}".';
108
    }
109
110 5
    #[ArrayShape([
111
        'targetValue' => 'mixed',
112
        'targetAttribute' => 'string|null',
113
        'message' => 'array',
114
        'type' => 'string',
115
        'skipOnEmpty' => 'bool',
116
        'skipOnError' => 'bool',
117
    ])]
118
    public function getOptions(): array
119
    {
120
        return [
121 5
            'targetValue' => $this->targetValue,
122 5
            'targetAttribute' => $this->targetAttribute,
123
            'message' => [
124 5
                'message' => $this->getMessage(),
125
                'parameters' => [
126 5
                    'targetValue' => $this->targetValue,
127 5
                    'targetAttribute' => $this->targetAttribute,
128 5
                    'targetValueOrAttribute' => $this->targetValue ?? $this->targetAttribute,
129
                ],
130
            ],
131 5
            'type' => $this->type,
132 5
            'skipOnEmpty' => $this->skipOnEmpty,
133 5
            'skipOnError' => $this->skipOnError,
134
        ];
135
    }
136
}
137