-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValid.php
More file actions
75 lines (66 loc) · 2.15 KB
/
Copy pathValid.php
File metadata and controls
75 lines (66 loc) · 2.15 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* @author Marwan Al-Soltany <MarwanAlsoltany@gmail.com>
* @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\Rule;
use MAKS\Mighty\Result;
use MAKS\Mighty\Validation\Strategy;
use MAKS\Mighty\Validation\Constraint;
use MAKS\Mighty\Validation\Constraint\ValidatableObjectInterface;
use MAKS\Mighty\Validation\Constraint\ValidatesOne;
/**
* Validates the validity of a validatable object.
*
* @package Mighty\Validator
*/
#[Attribute(
Attribute::TARGET_PROPERTY |
Attribute::TARGET_METHOD
)]
class Valid extends Constraint implements ValidatesOne
{
/**
* Valid constructor.
*
* @param string|null $message
* @param Strategy $strategy
*/
public function __construct(
?string $message = null,
Strategy $strategy = Strategy::FailFast,
) {
parent::__construct(validation: 'valid', messages: ['valid' => $message], strategy: $strategy);
}
/**
* {@inheritDoc}
*/
public function validate(mixed $value = null): Result
{
$name = '';
$data = [$name => $value];
$validations = [$name => $this->validation];
$messages = [$name => [$this->validation => $this->messages[$this->validation] ?? null]];
$labels = [$name => static::class];
$result = $this
->getValidator()
->addRule(
(new Rule())
->setName('valid')
->setCallback(static fn ($input, $interface) => $input instanceof $interface && $input->isValid())
->setParameters(['@input', ValidatableObjectInterface::class])
->setMessage('${@label} validation failed or the value is not a instance of ${@parameters.1}')
)
->setData($data)
->setValidations($validations)
->setMessages($messages)
->setLabels($labels)
->validate();
return $result[$name];
}
}