ChildrenValidationLocator.php 1.2 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\SalesRule\Model\Quote;
  8. use Magento\Quote\Model\Quote\Item\AbstractItem as QuoteItem;
  9. /**
  10. * Used to determine necessity to validate rule on item's children that may depends on product type.
  11. */
  12. class ChildrenValidationLocator
  13. {
  14. /**
  15. * @var array
  16. */
  17. private $productTypeChildrenValidationMap;
  18. /**
  19. * @param array $productTypeChildrenValidationMap
  20. * <pre>
  21. * [
  22. * 'ProductType1' => true,
  23. * 'ProductType2' => false
  24. * ]
  25. * </pre>
  26. */
  27. public function __construct(
  28. array $productTypeChildrenValidationMap = []
  29. ) {
  30. $this->productTypeChildrenValidationMap = $productTypeChildrenValidationMap;
  31. }
  32. /**
  33. * Checks necessity to validate rule on item's children.
  34. *
  35. * @param QuoteItem $item
  36. * @return bool
  37. */
  38. public function isChildrenValidationRequired(QuoteItem $item): bool
  39. {
  40. $type = $item->getProduct()->getTypeId();
  41. if (isset($this->productTypeChildrenValidationMap[$type])) {
  42. return (bool)$this->productTypeChildrenValidationMap[$type];
  43. }
  44. return true;
  45. }
  46. }