|
1 | 1 | # Icinga PHP Library - Standard Library |
2 | 2 |
|
3 | | -This is the Stdlib prototype for the Icinga PHP library ([ipl](https://github.com/Icinga/ipl)). |
4 | | -Please do not use this for anything important yet, as all APIs, Interfaces and |
5 | | -paths are still subject to change. |
| 3 | +`ipl/stdlib` provides reusable building blocks for Icinga PHP libraries and |
| 4 | +applications. It covers declarative filtering, event emission, string and |
| 5 | +iterable utilities, lightweight data and message containers, and a |
| 6 | +stable priority queue. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +The recommended way to install this library is via |
| 11 | +[Composer](https://getcomposer.org): |
| 12 | + |
| 13 | +```shell |
| 14 | +composer require ipl/stdlib |
| 15 | +``` |
| 16 | + |
| 17 | +`ipl/stdlib` requires PHP 8.2 or later with the `openssl` extension. |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Filter Rows With Declarative Rules |
| 22 | + |
| 23 | +Build composable filter trees with `ipl\Stdlib\Filter` and evaluate them |
| 24 | +against arrays or objects: |
| 25 | + |
| 26 | +```php |
| 27 | +use ipl\Stdlib\Filter; |
| 28 | + |
| 29 | +$filter = Filter::all( |
| 30 | + Filter::equal('problem', '1'), |
| 31 | + Filter::none(Filter::equal('handled', '1')), |
| 32 | + Filter::like('service', 'www.*') |
| 33 | +); |
| 34 | + |
| 35 | +$row = [ |
| 36 | + 'problem' => '1', |
| 37 | + 'handled' => '0', |
| 38 | + 'service' => 'www.icinga.com', |
| 39 | +]; |
| 40 | + |
| 41 | +if (Filter::match($filter, $row)) { |
| 42 | + // The row matches the rule set. |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Available condition factories: `equal`, `unequal`, `like`, `unlike`, |
| 47 | +`greaterThan`, `greaterThanOrEqual`, `lessThan`, `lessThanOrEqual`. |
| 48 | +Available logical factories: `all`, `any`, `none`, `not`. |
| 49 | + |
| 50 | +### Build Filters Incrementally |
| 51 | + |
| 52 | +When an object needs to collect filter conditions over time, use the `Filters` |
| 53 | +trait. It complements the `Filterable` contract and exposes `filter()`, |
| 54 | +`orFilter()`, `notFilter()`, and `orNotFilter()`: |
| 55 | + |
| 56 | +```php |
| 57 | +use ipl\Stdlib\Contract\Filterable; |
| 58 | +use ipl\Stdlib\Filter; |
| 59 | +use ipl\Stdlib\Filters; |
| 60 | + |
| 61 | +class Query implements Filterable |
| 62 | +{ |
| 63 | + use Filters; |
| 64 | +} |
| 65 | + |
| 66 | +$query = (new Query()) |
| 67 | + ->filter(Filter::equal('problem', '1')) |
| 68 | + ->orNotFilter(Filter::equal('handled', '1')); |
| 69 | + |
| 70 | +$filter = $query->getFilter(); |
| 71 | +``` |
| 72 | + |
| 73 | +## Events |
| 74 | + |
| 75 | +The `Events` trait wraps [Evenement](https://github.com/igorw/evenement) and |
| 76 | +adds event validation. Declare event name constants on the class to give |
| 77 | +callers a typo-safe API and to let `isValidEvent()` enforce an explicit |
| 78 | +allow-list: |
| 79 | + |
| 80 | +```php |
| 81 | +use ipl\Stdlib\Events; |
| 82 | + |
| 83 | +class Connection |
| 84 | +{ |
| 85 | + use Events; |
| 86 | + |
| 87 | + public const ON_CONNECT = 'connected'; |
| 88 | + public const ON_DISCONNECT = 'disconnected'; |
| 89 | + |
| 90 | + protected function isValidEvent($event): bool |
| 91 | + { |
| 92 | + return in_array($event, [static::ON_CONNECT, static::ON_DISCONNECT], true); |
| 93 | + } |
| 94 | + |
| 95 | + public function open(): void |
| 96 | + { |
| 97 | + // ... connect ... |
| 98 | + $this->emit(self::ON_CONNECT, [$this]); |
| 99 | + } |
| 100 | + |
| 101 | + public function close(): void |
| 102 | + { |
| 103 | + // ... disconnect ... |
| 104 | + $this->emit(self::ON_DISCONNECT, [$this]); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +$conn = new Connection(); |
| 109 | +$conn->on(Connection::ON_CONNECT, function (Connection $c): void { |
| 110 | + echo "Connected\n"; |
| 111 | +}); |
| 112 | +$conn->on(Connection::ON_DISCONNECT, function (Connection $c): void { |
| 113 | + echo "Disconnected\n"; |
| 114 | +}); |
| 115 | + |
| 116 | +$conn->open(); |
| 117 | +$conn->close(); |
| 118 | +``` |
| 119 | + |
| 120 | +## Utility Helpers |
| 121 | + |
| 122 | +### Str |
| 123 | + |
| 124 | +`Str` offers string utilities that complement PHP's built-in functions. |
| 125 | +It converts between naming conventions, splits and trims in one step, and |
| 126 | +provides `startsWith` with case-insensitive matching. |
| 127 | + |
| 128 | +```php |
| 129 | +use ipl\Stdlib\Str; |
| 130 | + |
| 131 | +// Convert snake_case or kebab-case identifiers to camelCase: |
| 132 | +Str::camel('host_name'); // 'hostName' |
| 133 | +Str::camel('display-name'); // 'displayName' |
| 134 | + |
| 135 | +// Split on a delimiter and trim whitespace from every part in one pass: |
| 136 | +Str::trimSplit(' foo , bar , baz '); // ['foo', 'bar', 'baz'] |
| 137 | +Str::trimSplit('root:secret', ':'); // ['root', 'secret'] |
| 138 | + |
| 139 | +// Always return exactly $limit parts: pads with null if the delimiter is |
| 140 | +// absent, and fold any remainder into the last part if there are more |
| 141 | +// separators than expected: |
| 142 | +[$user, $pass] = Str::symmetricSplit('root', ':', 2); // ['root', null] |
| 143 | +[$user, $pass] = Str::symmetricSplit('root:secret:extra', ':', 2); // ['root', 'secret:extra'] |
| 144 | + |
| 145 | +// Case-insensitive prefix check: |
| 146 | +Str::startsWith('Foobar', 'foo', caseSensitive: false); // true |
| 147 | +Str::startsWith('foobar', 'foo'); // true |
| 148 | +``` |
| 149 | + |
| 150 | +### Seq |
| 151 | + |
| 152 | +`Seq` searches arrays, iterators, and generators by value, key, or callback |
| 153 | +without first materializing them into arrays. When the second argument to |
| 154 | +`find` or `contains` is a non-callable, it is compared by value; pass a |
| 155 | +closure to match by predicate instead: |
| 156 | + |
| 157 | +```php |
| 158 | +use ipl\Stdlib\Seq; |
| 159 | + |
| 160 | +$users = [ |
| 161 | + 'alice' => 'admin', |
| 162 | + 'bob' => 'viewer', |
| 163 | +]; |
| 164 | + |
| 165 | +Seq::contains($users, 'viewer'); // true |
| 166 | + |
| 167 | +[$key, $value] = Seq::find($users, 'admin'); // ['alice', 'admin'] |
| 168 | + |
| 169 | +// Match by predicate — returns as soon as a result is found: |
| 170 | +[$key, $value] = Seq::find($users, fn(string $role): bool => $role !== 'admin'); // ['bob', 'viewer'] |
| 171 | +``` |
| 172 | + |
| 173 | +### Iterable Helpers |
| 174 | + |
| 175 | +```php |
| 176 | +use function ipl\Stdlib\iterable_key_first; |
| 177 | +use function ipl\Stdlib\iterable_value_first; |
| 178 | + |
| 179 | +$map = [ |
| 180 | + 'id' => 42, |
| 181 | + 'name' => 'Alice', |
| 182 | +]; |
| 183 | + |
| 184 | +iterable_key_first($map); // 'id' |
| 185 | +iterable_value_first($map); // 42 |
| 186 | + |
| 187 | +// Works with generators and iterators — does not require an array: |
| 188 | +iterable_key_first(new ArrayIterator(['a' => 1])); // 'a' |
| 189 | +iterable_key_first([]); // null |
| 190 | +``` |
| 191 | + |
| 192 | +### Grouping With `yield_groups` |
| 193 | + |
| 194 | +`yield_groups` partitions a pre-sorted traversable into named groups. |
| 195 | +The callback must return at least the grouping criterion, but it can |
| 196 | +also return a custom value and key. The traversable **must** be sorted |
| 197 | +by the grouping criterion before being passed in; results are undefined |
| 198 | +otherwise: |
| 199 | + |
| 200 | +```php |
| 201 | +use function ipl\Stdlib\yield_groups; |
| 202 | + |
| 203 | +foreach (yield_groups($rows, fn(object $row): string => $row->category) as $category => $items) { |
| 204 | + // $items contains all rows for $category. |
| 205 | +} |
| 206 | +``` |
| 207 | + |
| 208 | +### Other Utility Classes |
| 209 | + |
| 210 | +- `Data` — mutable key/value store |
| 211 | +- `Messages` — collects user-facing messages and supports `sprintf`-style |
| 212 | + placeholders |
| 213 | +- `PriorityQueue` — extends `SplPriorityQueue` with stable insertion-order for |
| 214 | + items at equal priority; iterate non-destructively with `yieldAll()` |
| 215 | + |
| 216 | +## Changelog |
| 217 | + |
| 218 | +See [CHANGELOG.md](CHANGELOG.md) for a list of notable changes. |
| 219 | + |
| 220 | +## License |
| 221 | + |
| 222 | +`ipl/stdlib` is licensed under the terms of the [MIT License](LICENSE.md). |
0 commit comments