ValidatorPool.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\Exception\NotFoundException;
  8. use Magento\Framework\ObjectManager\TMap;
  9. use Magento\Framework\ObjectManager\TMapFactory;
  10. /**
  11. * Class ValidatorPool
  12. * @package Magento\Payment\Gateway\Validator
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class ValidatorPool implements \Magento\Payment\Gateway\Validator\ValidatorPoolInterface
  17. {
  18. /**
  19. * @var ValidatorInterface[] | TMap
  20. */
  21. private $validators;
  22. /**
  23. * @param TMapFactory $tmapFactory
  24. * @param array $validators
  25. */
  26. public function __construct(
  27. TMapFactory $tmapFactory,
  28. array $validators = []
  29. ) {
  30. $this->validators = $tmapFactory->create(
  31. [
  32. 'array' => $validators,
  33. 'type' => ValidatorInterface::class
  34. ]
  35. );
  36. }
  37. /**
  38. * Returns configured validator
  39. *
  40. * @param string $code
  41. * @return ValidatorInterface
  42. * @throws NotFoundException
  43. */
  44. public function get($code)
  45. {
  46. if (!isset($this->validators[$code])) {
  47. throw new NotFoundException(__('The validator for the "%1" field doesn\'t exist.', $code));
  48. }
  49. return $this->validators[$code];
  50. }
  51. }