State.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\Layer;
  7. use Magento\Catalog\Model\Layer\Filter\Item;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\DataObject;
  10. /**
  11. * Layered navigation state model
  12. *
  13. * @api
  14. * @author Magento Core Team <core@magentocommerce.com>
  15. * @since 100.0.2
  16. */
  17. class State extends DataObject
  18. {
  19. /**
  20. * Add filter item to layer state
  21. *
  22. * @param Item $filter
  23. * @return $this
  24. */
  25. public function addFilter($filter)
  26. {
  27. $filters = $this->getFilters();
  28. $filters[] = $filter;
  29. $this->setFilters($filters);
  30. return $this;
  31. }
  32. /**
  33. * Set layer state filter items
  34. *
  35. * @param Item[] $filters
  36. * @return $this
  37. * @throws LocalizedException
  38. */
  39. public function setFilters($filters)
  40. {
  41. if (!is_array($filters)) {
  42. throw new LocalizedException(__('The filters are invalid. Set them in an array and try again.'));
  43. }
  44. $this->setData('filters', $filters);
  45. return $this;
  46. }
  47. /**
  48. * Get applied to layer filter items
  49. *
  50. * @return Item[]
  51. */
  52. public function getFilters()
  53. {
  54. $filters = $this->getData('filters');
  55. if ($filters === null) {
  56. $filters = [];
  57. $this->setData('filters', $filters);
  58. }
  59. return $filters;
  60. }
  61. }