<?php
/* Usage
$filter = new *Filter(values, search terms, filter type, case sensitivity);
$result = $filter->apply();
*/
/* values
array of values who may or may not be included in the result set dependent on the filter
*/
/* search terms
array of search terms the filter will search for in the values to decide whether to include a value in the result set or not
*/
/* filter type
FILTER_SKIP: matching values will be excluded from the result set
FILTER_ACCEPT: non-matching values will be excluded from the result set
default for filters: FILTER_SKIP
*/
/* case sensitivity
FILTER_CASE_SENSITIVE: filter is case-sensitive
FILTER_CASE_INSENSITIVE: filter is not case-sensitive
default for filters: FILTER_CASE_INSENSITIVE
*/
/* predefined filters
FileExtensionFilter
FileNameFilter
PrefixFilter
SuffixFilter
LimitFilter
*/
class SimpleArray implements Iterator
{
protected
$pointer,
$dataArrayName;
public function current()
{
return $this->{$this->dataArrayName}[$this->pointer];
}
public function key()
{
return $this->pointer;
}
public function next()
{
++$this->pointer;
}
public function valid()
{
return ($this->pointer < count($this->{$this->dataArrayName}));
}
public function rewind()
{
$this->pointer = 0;
}
}
public function rewind()
{
parent::rewind();
if($this->changeOccured) $this->apply();
}
protected function isTraversable($var)
{
if(is_array($var)) return true;
if($var instanceof Iterator) return true;
return false;
}
protected function toArray($vars)
{
$ary = array();
foreach($vars as $var)
{
array_push($ary, $var);
}
return $ary;
}
protected function makeCommonCase(&$item)
{
if($this->isTraversable($item))
{
foreach($item as &$i)
{
$this->makeCommonCase($i);
}
}
else $item = strtolower($item);
}
public function getSearchTerms()
{
return $this->searchTerms;
}
public function setSearchTerms($searchTerms)
{
if(!$this->isTraversable($searchTerms)) $searchTerms = array($searchTerms);
$this->searchTerms = $searchTerms;
$this->changeOccured = 1;
}
public function addSearchTerms($searchTerms)
{
if(!is_array($searchTerms))
{
if($this->isTraversable($searchTerms)) $searchTerms = $this->toArray($searchTerms);
else $searchTerms = array($searchTerms);
}
$this->searchTerms = array_merge($this->searchTerms, $searchTerms);
$this->changeOccured = 1;
}
public function removeSearchTerms($searchTerms)
{
if(!is_array($searchTerms))
{
if($this->isTraversable($searchTerms)) $searchTerms = $this->toArray($searchTerms);
else $searchTerms = array($searchTerms);
}
$this->searchTerms = array_diff($this->searchTerms, $searchTerms);
$this->changeOccured = 1;
}
public function getType()
{
return $this->type;
}
public function setType($type)
{
$this->type = (bool) $type;
$this->changeOccured = 1;
}
public function getCaseSensitivity()
{
return $this->isCaseSensitive;
}
public function setCaseSensitivity($isCaseSensitive)
{
$this->isCaseSensitive = (bool) $isCaseSensitive;
$this->changeOccured = 1;
}
public function getValues()
{
return $this->values;
}
public function setValues($values)
{
if(!$this->isTraversable($values)) $values = array($values);
$this->values = $values;
$this->changeOccured = 1;
}
public function addValues($values)
{
if(!is_array($values))
{
if($this->isTraversable($values)) $values = $this->toArray($values);
else $values = array($values);
}
$this->values = array_merge($this->values, $values);
$this->changeOccured = 1;
}
public function removeValues($values)
{
if(!is_array($values))
{
if($this->isTraversable($values)) $values = $this->toArray($values);
else $values = array($values);
}
$this->values = array_diff($this->values, $values);
$this->changeOccured = 1;
}