1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?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\StockInterface;
- /**
- * Chain of validators. Extension point for new validators via di configuration
- *
- * @api
- */
- class StockValidatorChain implements StockValidatorInterface
- {
- /**
- * @var ValidationResultFactory
- */
- private $validationResultFactory;
- /**
- * @var StockValidatorInterface[]
- */
- private $validators;
- /**
- * @param ValidationResultFactory $validationResultFactory
- * @param StockValidatorInterface[] $validators
- * @throws LocalizedException
- */
- public function __construct(
- ValidationResultFactory $validationResultFactory,
- array $validators = []
- ) {
- $this->validationResultFactory = $validationResultFactory;
- foreach ($validators as $validator) {
- if (!$validator instanceof StockValidatorInterface) {
- throw new LocalizedException(
- __('Stock Validator must implement StockValidatorInterface.')
- );
- }
- }
- $this->validators = $validators;
- }
- /**
- * @inheritdoc
- */
- public function validate(StockInterface $stock): ValidationResult
- {
- $errors = [];
- foreach ($this->validators as $validator) {
- $validationResult = $validator->validate($stock);
- if (!$validationResult->isValid()) {
- $errors = array_merge($errors, $validationResult->getErrors());
- }
- }
- return $this->validationResultFactory->create(['errors' => $errors]);
- }
- }
|