QueryModifierFactory.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB\Select;
  7. use Magento\Framework\ObjectManagerInterface;
  8. /**
  9. * Create instance of QueryModifierInterface
  10. */
  11. class QueryModifierFactory
  12. {
  13. /**
  14. * @var ObjectManagerInterface
  15. */
  16. private $objectManager;
  17. /**
  18. * @var array
  19. */
  20. private $queryModifiers;
  21. /**
  22. * Constructor
  23. *
  24. * @param ObjectManagerInterface $objectManager
  25. * @param array $queryModifiers
  26. */
  27. public function __construct(
  28. ObjectManagerInterface $objectManager,
  29. array $queryModifiers = []
  30. ) {
  31. $this->objectManager = $objectManager;
  32. $this->queryModifiers = $queryModifiers;
  33. }
  34. /**
  35. * Create instance of QueryModifierInterface
  36. *
  37. * @param string $type
  38. * @param array $data
  39. * @return QueryModifierInterface
  40. * @throws \InvalidArgumentException
  41. */
  42. public function create($type, array $data = [])
  43. {
  44. if (!isset($this->queryModifiers[$type])) {
  45. throw new \InvalidArgumentException('Unknown query modifier type ' . $type);
  46. }
  47. $queryModifier = $this->objectManager->create($this->queryModifiers[$type], $data);
  48. if (!($queryModifier instanceof QueryModifierInterface)) {
  49. throw new \InvalidArgumentException(
  50. $this->queryModifiers[$type] . ' must implement ' . QueryModifierInterface::class
  51. );
  52. }
  53. return $queryModifier;
  54. }
  55. }