PaymentMethodValidationRule.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Quote\Model\ValidationRules;
  8. use Magento\Framework\Validation\ValidationResultFactory;
  9. use Magento\Quote\Model\Quote;
  10. /**
  11. * @inheritdoc
  12. */
  13. class PaymentMethodValidationRule implements QuoteValidationRuleInterface
  14. {
  15. /**
  16. * @var string
  17. */
  18. private $generalMessage;
  19. /**
  20. * @var ValidationResultFactory
  21. */
  22. private $validationResultFactory;
  23. /**
  24. * @param ValidationResultFactory $validationResultFactory
  25. * @param string $generalMessage
  26. */
  27. public function __construct(
  28. ValidationResultFactory $validationResultFactory,
  29. string $generalMessage = ''
  30. ) {
  31. $this->validationResultFactory = $validationResultFactory;
  32. $this->generalMessage = $generalMessage;
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function validate(Quote $quote): array
  38. {
  39. $validationErrors = [];
  40. $validationResult = $quote->getPayment()->getMethod();
  41. if (!$validationResult) {
  42. $validationErrors = [__($this->generalMessage)];
  43. }
  44. return [$this->validationResultFactory->create(['errors' => $validationErrors])];
  45. }
  46. }