-
Notifications
You must be signed in to change notification settings - Fork 247
/
RequestValidatorChain.php
71 lines (62 loc) · 2.01 KB
/
RequestValidatorChain.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventoryInStorePickupShippingApi\Model\Carrier\Validation;
use Magento\Framework\Validation\ValidationResult;
use Magento\Framework\Validation\ValidationResultFactory;
use Magento\Quote\Model\Quote\Address\RateRequest;
/**
* @inheritdoc
*/
class RequestValidatorChain implements RequestValidatorInterface
{
/**
* @var ValidationResultFactory
*/
private $validationResultFactory;
/**
* @var RequestValidatorInterface[]
*/
private $validators;
/**
* @param ValidationResultFactory $validationResultFactory
* @param array $validators
*/
public function __construct(
ValidationResultFactory $validationResultFactory,
array $validators = []
) {
$this->validationResultFactory = $validationResultFactory;
foreach ($validators as $validator) {
if (!$validator instanceof RequestValidatorInterface) {
throw new \InvalidArgumentException(
sprintf(
'In-Store Pickup Carrier Rate Request Validator must implement %s.' .
'%s has been received instead.',
RequestValidatorInterface::class,
get_class($validator)
)
);
}
}
$this->validators = $validators;
}
/**
* @inheritdoc
*/
public function validate(RateRequest $rateRequest): ValidationResult
{
$errors = [];
/** @var RequestValidatorInterface $validator */
foreach ($this->validators as $validator) {
$validationResult = $validator->validate($rateRequest);
if (!$validationResult->isValid()) {
$errors = array_merge($errors, $validationResult->getErrors());
}
}
return $this->validationResultFactory->create(['errors' => $errors]);
}
}