This repository has been archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIfThenElseValidator.php
74 lines (66 loc) · 1.74 KB
/
IfThenElseValidator.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
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Validator\Component\Logical;
use Ulrack\Validator\Common\ValidatorInterface;
class IfThenElseValidator implements ValidatorInterface
{
/**
* Contains the validator for the if statement.
*
* @var ValidatorInterface
*/
private $ifValidator;
/**
* Contains the validator the then statement.
*
* @var ValidatorInterface|null
*/
private $thenValidator;
/**
* Contains the validator for the else statement.
*
* @var ValidatorInterface|null
*/
private $elseValidator;
/**
* Constructor
*
* @param ValidatorInterface $ifValidator
* @param ValidatorInterface|null $thenValidator
* @param ValidatorInterface|null $elseValidator
*/
public function __construct(
ValidatorInterface $ifValidator,
?ValidatorInterface $thenValidator,
?ValidatorInterface $elseValidator
) {
$this->ifValidator = $ifValidator;
$this->thenValidator = $thenValidator;
$this->elseValidator = $elseValidator;
}
/**
* Validate the data against the validator.
*
* @param mixed $data The data that needs to be validated.
*
* @return bool
*/
public function __invoke($data): bool
{
if (($this->ifValidator)($data)) {
if ($this->thenValidator !== null
&& !($this->thenValidator)($data)) {
return false;
}
} else {
if ($this->elseValidator !== null
&& !($this->elseValidator)($data)) {
return false;
}
}
return true;
}
}