Skip to content

Commit

Permalink
Merge pull request #15 from Icinga/filter-implementation
Browse files Browse the repository at this point in the history
Filter Implementation
  • Loading branch information
nilmerg authored Jan 14, 2021
2 parents ee9474e + 87ceb3a commit 3ca03d2
Show file tree
Hide file tree
Showing 14 changed files with 1,232 additions and 0 deletions.
474 changes: 474 additions & 0 deletions src/Filter.php

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/Filter/All.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ipl\Stdlib\Filter;

class All extends Chain
{

}
8 changes: 8 additions & 0 deletions src/Filter/Any.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ipl\Stdlib\Filter;

class Any extends Chain
{

}
133 changes: 133 additions & 0 deletions src/Filter/Chain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace ipl\Stdlib\Filter;

use ArrayIterator;
use Countable;
use ipl\Stdlib\Properties;
use IteratorAggregate;
use OutOfBoundsException;

abstract class Chain implements Rule, IteratorAggregate, Countable
{
use Properties;

/** @var Rule[] */
protected $rules = [];

/**
* Create a new Chain
*
* @param Rule ...$rules
*/
public function __construct(Rule ...$rules)
{
foreach ($rules as $rule) {
$this->add($rule);
}
}

/**
* Clone this chain's rules
*/
public function __clone()
{
foreach ($this->rules as $i => $rule) {
$this->rules[$i] = clone $rule;
}
}

/**
* Get an iterator this chain's rules
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->rules);
}

/**
* Add a rule to this chain
*
* @param Rule $rule
*
* @return $this
*/
public function add(Rule $rule)
{
$this->rules[] = $rule;

return $this;
}

/**
* Get whether this chain contains the given rule
*
* @param Rule $rule
*
* @return bool
*/
public function has(Rule $rule)
{
return array_search($rule, $this->rules, true) !== false;
}

/**
* Replace a rule with another one in this chain
*
* @param Rule $rule
* @param Rule $replacement
*
* @throws OutOfBoundsException In case no existing rule is found
* @return $this
*/
public function replace(Rule $rule, Rule $replacement)
{
$ruleAt = array_search($rule, $this->rules, true);
if ($ruleAt === false) {
throw new OutOfBoundsException('Rule to replace not found');
}

array_splice($this->rules, $ruleAt, 1, [$replacement]);

return $this;
}

/**
* Remove a rule from this chain
*
* @param Rule $rule
*
* @return $this
*/
public function remove(Rule $rule)
{
$ruleAt = array_search($rule, $this->rules, true);
if ($ruleAt !== false) {
array_splice($this->rules, $ruleAt, 1, []);
}

return $this;
}

/**
* Get whether this chain has any rules
*
* @return bool
*/
public function isEmpty()
{
return empty($this->rules);
}

/**
* Count this chain's rules
*
* @return int
*/
public function count()
{
return count($this->rules);
}
}
78 changes: 78 additions & 0 deletions src/Filter/Condition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace ipl\Stdlib\Filter;

use Exception;
use InvalidArgumentException;
use ipl\Stdlib\Properties;

abstract class Condition implements Rule
{
use Properties;

/** @var string */
protected $column;

/** @var mixed */
protected $value;

/**
* Create a new Condition
*
* @param string $column
* @param mixed $value
*/
public function __construct($column, $value)
{
$this->setColumn($column)
->setValue($value);
}

/**
* Set this condition's column
*
* @param string $column
*
* @return $this
*/
public function setColumn($column)
{
$this->column = $column;

return $this;
}

/**
* Get this condition's column
*
* @return string
*/
public function getColumn()
{
return $this->column;
}

/**
* Set this condition's value
*
* @param mixed $value
*
* @return $this
*/
public function setValue($value)
{
$this->value = $value;

return $this;
}

/**
* Get this condition's value
*
* @return mixed
*/
public function getValue()
{
return $this->value;
}
}
31 changes: 31 additions & 0 deletions src/Filter/Equal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace ipl\Stdlib\Filter;

class Equal extends Condition
{
/** @var bool */
protected $ignoreCase = false;

/**
* Ignore case on both sides of the equation
*
* @return $this
*/
public function ignoreCase()
{
$this->ignoreCase = true;

return $this;
}

/**
* Return whether this rule ignores case
*
* @return bool
*/
public function ignoresCase()
{
return $this->ignoreCase;
}
}
8 changes: 8 additions & 0 deletions src/Filter/GreaterThan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ipl\Stdlib\Filter;

class GreaterThan extends Condition
{

}
8 changes: 8 additions & 0 deletions src/Filter/GreaterThanOrEqual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ipl\Stdlib\Filter;

class GreaterThanOrEqual extends Condition
{

}
8 changes: 8 additions & 0 deletions src/Filter/LessThan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ipl\Stdlib\Filter;

class LessThan extends Condition
{

}
8 changes: 8 additions & 0 deletions src/Filter/LessThanOrEqual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ipl\Stdlib\Filter;

class LessThanOrEqual extends Condition
{

}
8 changes: 8 additions & 0 deletions src/Filter/None.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ipl\Stdlib\Filter;

class None extends Chain
{

}
8 changes: 8 additions & 0 deletions src/Filter/Rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ipl\Stdlib\Filter;

interface Rule
{

}
31 changes: 31 additions & 0 deletions src/Filter/Unequal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace ipl\Stdlib\Filter;

class Unequal extends Condition
{
/** @var bool */
protected $ignoreCase = false;

/**
* Ignore case on both sides of the equation
*
* @return $this
*/
public function ignoreCase()
{
$this->ignoreCase = true;

return $this;
}

/**
* Return whether this rule ignores case
*
* @return bool
*/
public function ignoresCase()
{
return $this->ignoreCase;
}
}
Loading

0 comments on commit 3ca03d2

Please sign in to comment.