generated from MarwanAlsoltany/php-package
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRule.php
62 lines (54 loc) · 1.52 KB
/
Rule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* @author Marwan Al-Soltany <[email protected]>
* @copyright Marwan Al-Soltany 2022
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace MAKS\Mighty\Validation\Constraint;
use Attribute;
use MAKS\Mighty\Engine;
use MAKS\Mighty\Result;
use MAKS\Mighty\Validation\Strategy;
use MAKS\Mighty\Validation\Constraint;
use MAKS\Mighty\Validation\Constraint\ValidatesOne;
/**
* Validates any data using a single validation rule.
*
* @package Mighty\Validator
*/
#[Attribute(
Attribute::IS_REPEATABLE |
Attribute::TARGET_CLASS |
Attribute::TARGET_CLASS_CONSTANT |
Attribute::TARGET_PROPERTY |
Attribute::TARGET_METHOD
)]
class Rule extends Constraint implements ValidatesOne
{
/**
* Rule constructor.
*
* @param string $name
* @param mixed[] $arguments
* @param string|null $message
* @param Strategy $strategy
*/
public function __construct(
string $name,
?array $arguments = null,
?string $message = null,
Strategy $strategy = Strategy::FailLazy,
) {
$validation = Engine::createRuleStatement($name, Engine::createRuleArguments($arguments ?? []));
parent::__construct(validation: $validation, messages: [$name => $message], strategy: $strategy);
}
/**
* {@inheritDoc}
*/
public function validate(mixed $value = null): Result
{
return parent::validate($value);
}
}