OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxtagset.php
Go to the documentation of this file.
1 <?php
2 
3 
9 class oxTagSet extends oxSuperCfg implements Iterator
10 {
11 
17  protected $_sSeparator = ',';
18 
24  protected $_aTags = array();
25 
31  protected $_aInvalidTags = array();
32 
38  public function setSeparator($sSeparator)
39  {
40  $this->_sSeparator = $sSeparator;
41  }
42 
48  public function getSeparator()
49  {
50  return $this->_sSeparator;
51  }
52 
59  public function set($sTags, $blPrepare = true)
60  {
61  $this->clear();
62  $this->add($sTags, $blPrepare);
63  }
64 
70  public function get()
71  {
72  return $this->_aTags;
73  }
74 
80  public function getInvalidTags()
81  {
82  return $this->_aInvalidTags;
83  }
84 
91  public function add($sTags, $blPrepare = true)
92  {
93  $aTags = explode($this->getSeparator(), $sTags);
94  foreach ($aTags as $sTag) {
95  $this->addTag($sTag, $blPrepare);
96  }
97  }
98 
107  public function addTag($mTag, $blPrepare = true)
108  {
109  $oTag = $this->_formTag($mTag, $blPrepare);
110  $sTagName = $oTag->get();
111  if (!$oTag->isValid()) {
112  if ($sTagName !== "") {
113  $this->_aInvalidTags[$sTagName] = $oTag;
114  }
115 
116  return false;
117  }
118  if ($this->_aTags[$sTagName] === null) {
119  $this->_aTags[$sTagName] = $oTag;
120  } else {
121  $this->_aTags[$sTagName]->increaseHitCount();
122  }
123 
124  return true;
125  }
126 
130  public function clear()
131  {
132  $this->_aTags = array();
133  }
134 
140  public function formString()
141  {
142  $aTags = array();
143  foreach ($this->get() as $oTag) {
144  $aTags = array_merge($aTags, array_fill(0, $oTag->getHitCount(), $oTag->get()));
145  }
146 
147  return implode($this->getSeparator(), $aTags);
148  }
149 
155  public function __toString()
156  {
157  return $this->formString();
158  }
159 
168  public function slice($offset, $length)
169  {
170  $this->_aTags = array_slice($this->get(), $offset, $length, true);
171 
172  return $this->_aTags;
173  }
174 
178  public function sort()
179  {
180  $oStr = getStr();
181  uksort($this->_aTags, array($oStr, 'strrcmp'));
182  }
183 
187  public function sortByHitCount()
188  {
189  uasort($this->_aTags, array($this, '_tagHitsCmp'));
190  }
191 
197  public function current()
198  {
199  return current($this->_aTags);
200  }
201 
205  public function next()
206  {
207  next($this->_aTags);
208  }
209 
215  public function key()
216  {
217  return key($this->_aTags);
218  }
219 
225  public function valid()
226  {
227  return isset($this->_aTags[$this->key()]);
228  }
229 
233  public function rewind()
234  {
235  reset($this->_aTags);
236  }
237 
246  protected function _formTag($mTag, $blPrepare = true)
247  {
248  if ($mTag instanceof oxTag) {
249  $oTag = $mTag;
250  } else {
251  $oTag = oxNew("oxTag");
252  $oTag->set($mTag, $blPrepare);
253  }
254 
255  return $oTag;
256  }
257 
266  protected function _tagHitsCmp($oTag1, $oTag2)
267  {
268  return $oTag2->getHitCount() - $oTag1->getHitCount();
269  }
270 }