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