123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\InventoryApi\Model;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Validation\ValidationResult;
- use Magento\Framework\Validation\ValidationResultFactory;
- use Magento\InventoryApi\Api\Data\SourceInterface;
- use Magento\InventoryApi\Model\SourceValidatorInterface;
- /**
- * Chain of validators. Extension point for new validators via di configuration
- *
- * @api
- */
- class SourceValidatorChain implements SourceValidatorInterface
- {
- /**
- * @var ValidationResultFactory
- */
- private $validationResultFactory;
- /**
- * @var SourceValidatorInterface[]
- */
- private $validators;
- /**
- * @param ValidationResultFactory $validationResultFactory
- * @param SourceValidatorInterface[] $validators
- * @throws LocalizedException
- */
- public function __construct(
- ValidationResultFactory $validationResultFactory,
- array $validators = []
- ) {
- $this->validationResultFactory = $validationResultFactory;
- foreach ($validators as $validator) {
- if (!$validator instanceof SourceValidatorInterface) {
- throw new LocalizedException(
- __('Source Validator must implement SourceValidatorInterface.')
- );
- }
- }
- $this->validators = $validators;
- }
- /**
- * @inheritdoc
- */
- public function validate(SourceInterface $source): ValidationResult
- {
- $errors = [];
- foreach ($this->validators as $validator) {
- $validationResult = $validator->validate($source);
- if (!$validationResult->isValid()) {
- $errors = array_merge($errors, $validationResult->getErrors());
- }
- }
- return $this->validationResultFactory->create(['errors' => $errors]);
- }
- }
|