Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Pager.php
Go to the documentation of this file.
1 <?php
8 
10 
18 {
19  const STORAGE_PREFIX = 'search_result_ids';
20 
26  protected $_storageId = null;
27 
33  protected $_items = null;
34 
40  protected $_backendSession;
41 
46  public function __construct(
47  \Magento\Framework\App\Helper\Context $context,
48  \Magento\Backend\Model\Session $backendSession
49  ) {
50  $this->_backendSession = $backendSession;
51  parent::__construct($context);
52  }
53 
60  public function setStorageId($storageId)
61  {
62  $this->_storageId = $storageId;
63  }
64 
71  public function setItems(array $items)
72  {
73  $this->_items = $items;
74  $this->_backendSession->setData($this->_getStorageKey(), $this->_items);
75 
76  return $this;
77  }
78 
84  protected function _loadItems()
85  {
86  if ($this->_items === null) {
87  $this->_items = (array)$this->_backendSession->getData($this->_getStorageKey());
88  }
89  }
90 
97  public function getNextItemId($id)
98  {
99  $position = $this->_findItemPositionByValue($id);
100  if ($position === false || $position == count($this->_items) - 1) {
101  return false;
102  }
103 
104  return $this->_items[$position + 1];
105  }
106 
113  public function getPreviousItemId($id)
114  {
115  $position = $this->_findItemPositionByValue($id);
116  if ($position === false || $position == 0) {
117  return false;
118  }
119 
120  return $this->_items[$position - 1];
121  }
122 
129  protected function _findItemPositionByValue($value)
130  {
131  $this->_loadItems();
132  return array_search($value, $this->_items);
133  }
134 
141  protected function _getStorageKey()
142  {
143  if (!$this->_storageId) {
144  throw new LocalizedException(__("The storage key wasn't set. Add the storage key and try again."));
145  }
146 
147  return self::STORAGE_PREFIX . $this->_storageId;
148  }
149 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Backend\Model\Session $backendSession)
Definition: Pager.php:46
$value
Definition: gender.phtml:16
$items