AppliersPool.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Model\Report\ConditionAppliers;
  7. /**
  8. * Class AppliersPool
  9. */
  10. class AppliersPool
  11. {
  12. /**
  13. * @var \Magento\Braintree\Model\Report\ConditionAppliers\ApplierInterface[]
  14. */
  15. private $appliersPool = [];
  16. /**
  17. * AppliersPool constructor.
  18. * @param ApplierInterface[] $appliers
  19. */
  20. public function __construct(array $appliers)
  21. {
  22. $this->appliersPool = $appliers;
  23. $this->checkAppliers();
  24. }
  25. /**
  26. * Check appliers's types
  27. *
  28. * @return bool
  29. */
  30. private function checkAppliers()
  31. {
  32. foreach ($this->appliersPool as $applier) {
  33. if (!($applier instanceof ApplierInterface)) {
  34. throw new \InvalidArgumentException('Report filter applier must implement ApplierInterface');
  35. }
  36. }
  37. return true;
  38. }
  39. /**
  40. * Get condition applier for filter
  41. * @param object $filter
  42. * @return null|ApplierInterface
  43. */
  44. public function getApplier($filter)
  45. {
  46. if (is_object($filter)) {
  47. $filterClass = get_class($filter);
  48. if (array_key_exists($filterClass, $this->appliersPool)) {
  49. return $this->appliersPool[$filterClass];
  50. }
  51. }
  52. return null;
  53. }
  54. }