ValidatorComposite.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Gateway\Validator;
  7. use Magento\Framework\ObjectManager\TMap;
  8. use Magento\Framework\ObjectManager\TMapFactory;
  9. use Magento\Payment\Gateway\Validator\ResultInterfaceFactory;
  10. /**
  11. * Compiles a result using the results of multiple validators
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class ValidatorComposite extends AbstractValidator
  17. {
  18. /**
  19. * @var ValidatorInterface[] | TMap
  20. */
  21. private $validators;
  22. /**
  23. * @var array
  24. */
  25. private $chainBreakingValidators;
  26. /**
  27. * @param ResultInterfaceFactory $resultFactory
  28. * @param TMapFactory $tmapFactory
  29. * @param array $validators
  30. * @param array $chainBreakingValidators
  31. */
  32. public function __construct(
  33. ResultInterfaceFactory $resultFactory,
  34. TMapFactory $tmapFactory,
  35. array $validators = [],
  36. array $chainBreakingValidators = []
  37. ) {
  38. $this->validators = $tmapFactory->create(
  39. [
  40. 'array' => $validators,
  41. 'type' => ValidatorInterface::class
  42. ]
  43. );
  44. $this->chainBreakingValidators = $chainBreakingValidators;
  45. parent::__construct($resultFactory);
  46. }
  47. /**
  48. * Performs domain level validation for business object
  49. *
  50. * @param array $validationSubject
  51. * @return ResultInterface
  52. */
  53. public function validate(array $validationSubject)
  54. {
  55. $isValid = true;
  56. $failsDescriptionAggregate = [];
  57. $errorCodesAggregate = [];
  58. foreach ($this->validators as $key => $validator) {
  59. $result = $validator->validate($validationSubject);
  60. if (!$result->isValid()) {
  61. $isValid = false;
  62. $failsDescriptionAggregate = array_merge(
  63. $failsDescriptionAggregate,
  64. $result->getFailsDescription()
  65. );
  66. $errorCodesAggregate = array_merge(
  67. $errorCodesAggregate,
  68. $result->getErrorCodes()
  69. );
  70. if (!empty($this->chainBreakingValidators[$key])) {
  71. break;
  72. }
  73. }
  74. }
  75. return $this->createResult($isValid, $failsDescriptionAggregate, $errorCodesAggregate);
  76. }
  77. }