| @@ 13-59 (lines=47) @@ | ||
| 10 | * |
|
| 11 | * @package PurpleBooth\GitGitHubLint |
|
| 12 | */ |
|
| 13 | class ValidateMessageImplementation implements ValidateMessage |
|
| 14 | { |
|
| 15 | /** |
|
| 16 | * @var array |
|
| 17 | */ |
|
| 18 | private $validators; |
|
| 19 | ||
| 20 | /** |
|
| 21 | * ValidationServiceImplementation constructor. |
|
| 22 | * |
|
| 23 | * @param array $validators |
|
| 24 | */ |
|
| 25 | public function __construct(array $validators) |
|
| 26 | { |
|
| 27 | $this->validators = $validators; |
|
| 28 | ||
| 29 | if (count($validators) < 1) { |
|
| 30 | new \LogicException("You need to provide the validation service with at least 1 validator"); |
|
| 31 | } |
|
| 32 | } |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Test a message against a number of validators |
|
| 36 | * |
|
| 37 | * @param Message $message |
|
| 38 | * |
|
| 39 | * @return Status |
|
| 40 | */ |
|
| 41 | public function validate(Message $message) : Status |
|
| 42 | { |
|
| 43 | $statuses = []; |
|
| 44 | ||
| 45 | /** @var Validator $validator */ |
|
| 46 | foreach ($this->validators as $validator) { |
|
| 47 | $statuses[] = $validator->validate($message); |
|
| 48 | } |
|
| 49 | ||
| 50 | usort( |
|
| 51 | $statuses, |
|
| 52 | function (Status $statusA, Status $statusB) { |
|
| 53 | return $statusA->getWeight() <=> $statusB->getWeight(); |
|
| 54 | } |
|
| 55 | ); |
|
| 56 | ||
| 57 | return array_pop($statuses); |
|
| 58 | } |
|
| 59 | } |
|
| 60 | ||
| @@ 13-56 (lines=44) @@ | ||
| 10 | * |
|
| 11 | * @package PurpleBooth\GitGithubLint |
|
| 12 | */ |
|
| 13 | class ValidationServiceImplementation implements ValidationService |
|
| 14 | { |
|
| 15 | /** |
|
| 16 | * @var array |
|
| 17 | */ |
|
| 18 | private $validators; |
|
| 19 | ||
| 20 | /** |
|
| 21 | * ValidationServiceImplementation constructor. |
|
| 22 | * |
|
| 23 | * @param array $validators |
|
| 24 | */ |
|
| 25 | public function __construct(array $validators) |
|
| 26 | { |
|
| 27 | $this->validators = $validators; |
|
| 28 | ||
| 29 | if (count($validators) < 1) { |
|
| 30 | new \LogicException("You need to provide the validation service with at least 1 validator"); |
|
| 31 | } |
|
| 32 | } |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Test a message against a number of validators |
|
| 36 | * |
|
| 37 | * @param Message $message |
|
| 38 | * |
|
| 39 | * @return Status |
|
| 40 | */ |
|
| 41 | public function validate(Message $message) : Status |
|
| 42 | { |
|
| 43 | $statuses = []; |
|
| 44 | ||
| 45 | /** @var Validator $validator */ |
|
| 46 | foreach ($this->validators as $validator) { |
|
| 47 | $statuses[] = $validator->validate($message); |
|
| 48 | } |
|
| 49 | ||
| 50 | usort($statuses, function (Status $statusA, Status $statusB) { |
|
| 51 | return $statusA->getWeight() <=> $statusB->getWeight(); |
|
| 52 | }); |
|
| 53 | ||
| 54 | return array_pop($statuses); |
|
| 55 | } |
|
| 56 | } |
|
| 57 | ||