-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Icinga/filter-implementation
Filter Implementation
- Loading branch information
Showing
14 changed files
with
1,232 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace ipl\Stdlib\Filter; | ||
|
||
class All extends Chain | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace ipl\Stdlib\Filter; | ||
|
||
class Any extends Chain | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace ipl\Stdlib\Filter; | ||
|
||
class GreaterThan extends Condition | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace ipl\Stdlib\Filter; | ||
|
||
class GreaterThanOrEqual extends Condition | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace ipl\Stdlib\Filter; | ||
|
||
class LessThan extends Condition | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace ipl\Stdlib\Filter; | ||
|
||
class LessThanOrEqual extends Condition | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace ipl\Stdlib\Filter; | ||
|
||
class None extends Chain | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace ipl\Stdlib\Filter; | ||
|
||
interface Rule | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.