Skip to content

Commit 3ca03d2

Browse files
authored
Merge pull request #15 from Icinga/filter-implementation
Filter Implementation
2 parents ee9474e + 87ceb3a commit 3ca03d2

File tree

14 files changed

+1232
-0
lines changed

14 files changed

+1232
-0
lines changed

src/Filter.php

Lines changed: 474 additions & 0 deletions
Large diffs are not rendered by default.

src/Filter/All.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ipl\Stdlib\Filter;
4+
5+
class All extends Chain
6+
{
7+
8+
}

src/Filter/Any.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ipl\Stdlib\Filter;
4+
5+
class Any extends Chain
6+
{
7+
8+
}

src/Filter/Chain.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
namespace ipl\Stdlib\Filter;
4+
5+
use ArrayIterator;
6+
use Countable;
7+
use ipl\Stdlib\Properties;
8+
use IteratorAggregate;
9+
use OutOfBoundsException;
10+
11+
abstract class Chain implements Rule, IteratorAggregate, Countable
12+
{
13+
use Properties;
14+
15+
/** @var Rule[] */
16+
protected $rules = [];
17+
18+
/**
19+
* Create a new Chain
20+
*
21+
* @param Rule ...$rules
22+
*/
23+
public function __construct(Rule ...$rules)
24+
{
25+
foreach ($rules as $rule) {
26+
$this->add($rule);
27+
}
28+
}
29+
30+
/**
31+
* Clone this chain's rules
32+
*/
33+
public function __clone()
34+
{
35+
foreach ($this->rules as $i => $rule) {
36+
$this->rules[$i] = clone $rule;
37+
}
38+
}
39+
40+
/**
41+
* Get an iterator this chain's rules
42+
*
43+
* @return ArrayIterator
44+
*/
45+
public function getIterator()
46+
{
47+
return new ArrayIterator($this->rules);
48+
}
49+
50+
/**
51+
* Add a rule to this chain
52+
*
53+
* @param Rule $rule
54+
*
55+
* @return $this
56+
*/
57+
public function add(Rule $rule)
58+
{
59+
$this->rules[] = $rule;
60+
61+
return $this;
62+
}
63+
64+
/**
65+
* Get whether this chain contains the given rule
66+
*
67+
* @param Rule $rule
68+
*
69+
* @return bool
70+
*/
71+
public function has(Rule $rule)
72+
{
73+
return array_search($rule, $this->rules, true) !== false;
74+
}
75+
76+
/**
77+
* Replace a rule with another one in this chain
78+
*
79+
* @param Rule $rule
80+
* @param Rule $replacement
81+
*
82+
* @throws OutOfBoundsException In case no existing rule is found
83+
* @return $this
84+
*/
85+
public function replace(Rule $rule, Rule $replacement)
86+
{
87+
$ruleAt = array_search($rule, $this->rules, true);
88+
if ($ruleAt === false) {
89+
throw new OutOfBoundsException('Rule to replace not found');
90+
}
91+
92+
array_splice($this->rules, $ruleAt, 1, [$replacement]);
93+
94+
return $this;
95+
}
96+
97+
/**
98+
* Remove a rule from this chain
99+
*
100+
* @param Rule $rule
101+
*
102+
* @return $this
103+
*/
104+
public function remove(Rule $rule)
105+
{
106+
$ruleAt = array_search($rule, $this->rules, true);
107+
if ($ruleAt !== false) {
108+
array_splice($this->rules, $ruleAt, 1, []);
109+
}
110+
111+
return $this;
112+
}
113+
114+
/**
115+
* Get whether this chain has any rules
116+
*
117+
* @return bool
118+
*/
119+
public function isEmpty()
120+
{
121+
return empty($this->rules);
122+
}
123+
124+
/**
125+
* Count this chain's rules
126+
*
127+
* @return int
128+
*/
129+
public function count()
130+
{
131+
return count($this->rules);
132+
}
133+
}

src/Filter/Condition.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace ipl\Stdlib\Filter;
4+
5+
use Exception;
6+
use InvalidArgumentException;
7+
use ipl\Stdlib\Properties;
8+
9+
abstract class Condition implements Rule
10+
{
11+
use Properties;
12+
13+
/** @var string */
14+
protected $column;
15+
16+
/** @var mixed */
17+
protected $value;
18+
19+
/**
20+
* Create a new Condition
21+
*
22+
* @param string $column
23+
* @param mixed $value
24+
*/
25+
public function __construct($column, $value)
26+
{
27+
$this->setColumn($column)
28+
->setValue($value);
29+
}
30+
31+
/**
32+
* Set this condition's column
33+
*
34+
* @param string $column
35+
*
36+
* @return $this
37+
*/
38+
public function setColumn($column)
39+
{
40+
$this->column = $column;
41+
42+
return $this;
43+
}
44+
45+
/**
46+
* Get this condition's column
47+
*
48+
* @return string
49+
*/
50+
public function getColumn()
51+
{
52+
return $this->column;
53+
}
54+
55+
/**
56+
* Set this condition's value
57+
*
58+
* @param mixed $value
59+
*
60+
* @return $this
61+
*/
62+
public function setValue($value)
63+
{
64+
$this->value = $value;
65+
66+
return $this;
67+
}
68+
69+
/**
70+
* Get this condition's value
71+
*
72+
* @return mixed
73+
*/
74+
public function getValue()
75+
{
76+
return $this->value;
77+
}
78+
}

src/Filter/Equal.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace ipl\Stdlib\Filter;
4+
5+
class Equal extends Condition
6+
{
7+
/** @var bool */
8+
protected $ignoreCase = false;
9+
10+
/**
11+
* Ignore case on both sides of the equation
12+
*
13+
* @return $this
14+
*/
15+
public function ignoreCase()
16+
{
17+
$this->ignoreCase = true;
18+
19+
return $this;
20+
}
21+
22+
/**
23+
* Return whether this rule ignores case
24+
*
25+
* @return bool
26+
*/
27+
public function ignoresCase()
28+
{
29+
return $this->ignoreCase;
30+
}
31+
}

src/Filter/GreaterThan.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ipl\Stdlib\Filter;
4+
5+
class GreaterThan extends Condition
6+
{
7+
8+
}

src/Filter/GreaterThanOrEqual.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ipl\Stdlib\Filter;
4+
5+
class GreaterThanOrEqual extends Condition
6+
{
7+
8+
}

src/Filter/LessThan.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ipl\Stdlib\Filter;
4+
5+
class LessThan extends Condition
6+
{
7+
8+
}

src/Filter/LessThanOrEqual.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ipl\Stdlib\Filter;
4+
5+
class LessThanOrEqual extends Condition
6+
{
7+
8+
}

0 commit comments

Comments
 (0)