summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/stdlib/src/Filter/Chain.php
blob: 9422d3a9ab08ecd117485ca02429a3a5e82e507b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php

namespace ipl\Stdlib\Filter;

use ArrayIterator;
use Countable;
use IteratorAggregate;
use OutOfBoundsException;
use Traversable;

abstract class Chain implements Rule, MetaDataProvider, IteratorAggregate, Countable
{
    use MetaData;

    /** @var array<int, 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 meta data and rules
     */
    public function __clone()
    {
        if ($this->metaData !== null) {
            $this->metaData = clone $this->metaData;
        }

        foreach ($this->rules as $i => $rule) {
            $this->rules[$i] = clone $rule;
        }
    }

    /**
     * Get an iterator this chain's rules
     *
     * @return ArrayIterator<int, Rule>
     */
    public function getIterator(): Traversable
    {
        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;
    }

    /**
     * Prepend a rule to an existing rule in this chain
     *
     * @param Rule $rule
     * @param Rule $before
     *
     * @throws OutOfBoundsException In case no existing rule is found
     * @return $this
     */
    public function insertBefore(Rule $rule, Rule $before)
    {
        $ruleAt = array_search($before, $this->rules, true);
        if ($ruleAt === false) {
            throw new OutOfBoundsException('Reference rule not found');
        }

        array_splice($this->rules, $ruleAt, 0, [$rule]);

        return $this;
    }

    /**
     * Append a rule to an existing rule in this chain
     *
     * @param Rule $rule
     * @param Rule $after
     *
     * @throws OutOfBoundsException In case no existing rule is found
     * @return $this
     */
    public function insertAfter(Rule $rule, Rule $after)
    {
        $ruleAt = array_search($after, $this->rules, true);
        if ($ruleAt === false) {
            throw new OutOfBoundsException('Reference rule not found');
        }

        array_splice($this->rules, $ruleAt + 1, 0, [$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(): int
    {
        return count($this->rules);
    }
}