1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-31 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Chapi\Service\JobValidator; |
11
|
|
|
|
12
|
|
|
use Chapi\Component\DatePeriod\DatePeriodFactoryInterface; |
13
|
|
|
use Chapi\Entity\Chronos\JobEntity; |
14
|
|
|
use Chapi\Entity\JobEntityInterface; |
15
|
|
|
use Chapi\Entity\JobValidator\ValidationResult; |
16
|
|
|
|
17
|
|
|
class ChronosJobValidatorService implements JobValidatorServiceInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ValidatorFactoryInterface |
21
|
|
|
*/ |
22
|
|
|
private $oValidatorFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private static $aValidationMap = [ |
28
|
|
|
'name' => ValidatorFactoryInterface::NAME_VALIDATOR, |
29
|
|
|
'command' => ValidatorFactoryInterface::COMMAND_VALIDATOR, |
30
|
|
|
'description' => ValidatorFactoryInterface::NOT_EMPTY_VALIDATOR, |
31
|
|
|
'owner' => ValidatorFactoryInterface::NOT_EMPTY_VALIDATOR, |
32
|
|
|
'ownerName' => ValidatorFactoryInterface::NOT_EMPTY_VALIDATOR, |
33
|
|
|
'epsilon' => ValidatorFactoryInterface::EPSILON_VALIDATOR, |
34
|
|
|
'async' => ValidatorFactoryInterface::BOOLEAN_VALIDATOR, |
35
|
|
|
'disabled' => ValidatorFactoryInterface::BOOLEAN_VALIDATOR, |
36
|
|
|
'softError' => ValidatorFactoryInterface::BOOLEAN_VALIDATOR, |
37
|
|
|
'highPriority' => ValidatorFactoryInterface::BOOLEAN_VALIDATOR, |
38
|
|
|
'schedule' => ValidatorFactoryInterface::SCHEDULE_VALIDATOR, |
39
|
|
|
'parents' => ValidatorFactoryInterface::ARRAY_VALIDATOR, |
40
|
|
|
'retries' => ValidatorFactoryInterface::RETRY_VALIDATOR, |
41
|
|
|
'constraints' => ValidatorFactoryInterface::CONSTRAINTS_VALIDATOR, |
42
|
|
|
'container' => ValidatorFactoryInterface::CONTAINER_VALIDATOR, |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* ChronosJobValidatorService constructor. |
47
|
|
|
* @param ValidatorFactoryInterface $oValidatorFactory |
48
|
|
|
*/ |
49
|
4 |
|
public function __construct( |
50
|
|
|
ValidatorFactoryInterface $oValidatorFactory |
51
|
|
|
) |
52
|
|
|
{ |
53
|
4 |
|
$this->oValidatorFactory = $oValidatorFactory; |
54
|
4 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param JobEntityInterface $oJobEntity |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
2 |
|
public function isEntityValid(JobEntityInterface $oJobEntity) |
61
|
|
|
{ |
62
|
2 |
|
foreach ($this->validateJobEntity($oJobEntity) as $_oValidatorResult) |
63
|
|
|
{ |
64
|
2 |
|
if (!$_oValidatorResult->bIsValid) |
65
|
2 |
|
{ |
66
|
1 |
|
return false; |
67
|
|
|
} |
68
|
1 |
|
} |
69
|
|
|
|
70
|
1 |
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param JobEntityInterface $oJobEntity |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
2 |
|
public function getInvalidProperties(JobEntityInterface $oJobEntity) |
78
|
|
|
{ |
79
|
2 |
|
$_aValidationFields = $this->validateJobEntity($oJobEntity); |
80
|
|
|
|
81
|
2 |
|
$_aInvalidFields = []; |
82
|
2 |
|
foreach ($_aValidationFields as $_sProperty => $_oValidationResult) |
83
|
|
|
{ |
84
|
2 |
|
if (false == $_oValidationResult->bIsValid) |
85
|
2 |
|
{ |
86
|
1 |
|
$_aInvalidFields[$_sProperty] = $_oValidationResult->sErrorMessage; |
87
|
1 |
|
} |
88
|
2 |
|
} |
89
|
|
|
|
90
|
2 |
|
return $_aInvalidFields; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param JobEntityInterface $oJobEntity |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
4 |
|
private function validateJobEntity(JobEntityInterface $oJobEntity) |
98
|
|
|
{ |
99
|
4 |
|
$_aValidProperties = []; |
100
|
|
|
|
101
|
4 |
|
foreach (self::$aValidationMap as $_sProperty => $_iValidator) |
102
|
|
|
{ |
103
|
4 |
|
$_aValidProperties[$_sProperty] = $this->getValidationResult($_iValidator, $_sProperty, $oJobEntity); |
104
|
4 |
|
} |
105
|
|
|
|
106
|
4 |
|
return $_aValidProperties; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param int $iValidator |
111
|
|
|
* @param string $sProperty |
112
|
|
|
* @param JobEntityInterface $oJobEntity |
113
|
|
|
* @return ValidationResult |
114
|
|
|
*/ |
115
|
4 |
|
private function getValidationResult($iValidator, $sProperty, JobEntityInterface $oJobEntity) |
116
|
|
|
{ |
117
|
4 |
|
$_oValidator = $this->oValidatorFactory->getValidator($iValidator); |
118
|
4 |
|
return new ValidationResult( |
119
|
4 |
|
$sProperty, |
120
|
4 |
|
$_oValidator->isValid($sProperty, $oJobEntity), |
121
|
4 |
|
$_oValidator->getLastErrorMessage() |
122
|
4 |
|
); |
123
|
|
|
} |
124
|
|
|
} |