Resolver.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Catalog\Model\Layer;
  8. class Resolver
  9. {
  10. const CATALOG_LAYER_CATEGORY = 'category';
  11. const CATALOG_LAYER_SEARCH = 'search';
  12. /**
  13. * Catalog view layer models list
  14. *
  15. * @var array
  16. */
  17. protected $layersPool;
  18. /**
  19. * Filter factory
  20. *
  21. * @var \Magento\Framework\ObjectManagerInterface
  22. */
  23. protected $objectManager;
  24. /**
  25. * @var \Magento\Catalog\Model\Layer
  26. */
  27. protected $layer = null;
  28. /**
  29. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  30. * @param array $layersPool
  31. */
  32. public function __construct(
  33. \Magento\Framework\ObjectManagerInterface $objectManager,
  34. array $layersPool
  35. ) {
  36. $this->objectManager = $objectManager;
  37. $this->layersPool = $layersPool;
  38. }
  39. /**
  40. * Create Catalog Layer by specified type
  41. *
  42. * @param string $layerType
  43. * @return void
  44. */
  45. public function create($layerType)
  46. {
  47. if (isset($this->layer)) {
  48. throw new \RuntimeException('Catalog Layer has been already created');
  49. }
  50. if (!isset($this->layersPool[$layerType])) {
  51. throw new \InvalidArgumentException($layerType . ' does not belong to any registered layer');
  52. }
  53. $this->layer = $this->objectManager->create($this->layersPool[$layerType]);
  54. }
  55. /**
  56. * Get current Catalog Layer
  57. *
  58. * @return \Magento\Catalog\Model\Layer
  59. */
  60. public function get()
  61. {
  62. if (!isset($this->layer)) {
  63. $this->layer = $this->objectManager->create($this->layersPool[self::CATALOG_LAYER_CATEGORY]);
  64. }
  65. return $this->layer;
  66. }
  67. }