Skip to content

Commit 1abe3aa

Browse files
Document index policies
1 parent f4b7d13 commit 1abe3aa

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

docs/README.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ This document provides detailed examples for the different input filters availab
2626

2727
Below is the documentation for scalar type filters available in Ionizer.
2828

29-
3029
### `BoolFilter`
3130

3231
The `BoolFilter` validates a single boolean value.
@@ -125,6 +124,9 @@ try {
125124
Sometimes you need to accept a list of values, rather than a single value. These input filters allow you to limit the
126125
inputs to a flat, one-dimensional array consisting of specific values.
127126

127+
By default, the array filters only examine the **values, not the keys** of an input array. You can specify
128+
[Index Policies](#index-policies) if you interpret the array indices as data too.
129+
128130
### `BoolArrayFilter`
129131

130132
The `BoolArrayFilter` is used to ensure that the input is a one-dimensional array of booleans. It will cast any
@@ -246,6 +248,80 @@ try {
246248
}
247249
```
248250

251+
### Index Policies
252+
253+
Ionizer allows for policies to be defined on the indices (a.k.a. keys) of an array.
254+
255+
#### `AnyIndex`
256+
257+
Allows any string or integer index to be used on an array. This is congruent to not specifying an Index Policy at all.
258+
259+
```php
260+
<?php
261+
use ParagonIE\Ionizer\GeneralFilterContainer;
262+
use ParagonIE\Ionizer\IndexPolicy\AnyIndex;
263+
264+
$gfc = new GeneralFilterContainer();
265+
$gfc->setIndexPolicy(new AnyIndex());
266+
```
267+
268+
#### `IntegersOnly`
269+
270+
Allows any integer key.
271+
272+
```php
273+
<?php
274+
use ParagonIE\Ionizer\GeneralFilterContainer;
275+
use ParagonIE\Ionizer\IndexPolicy\IntegersOnly;
276+
277+
$gfc = new GeneralFilterContainer();
278+
$gfc->setIndexPolicy(new IntegersOnly());
279+
```
280+
281+
#### `StringsOnly`
282+
283+
Allows any string key.
284+
285+
```php
286+
<?php
287+
use ParagonIE\Ionizer\GeneralFilterContainer;
288+
use ParagonIE\Ionizer\IndexPolicy\StringsOnly;
289+
290+
$gfc = new GeneralFilterContainer();
291+
$gfc->setIndexPolicy(new StringsOnly());
292+
```
293+
294+
#### `KeyAllowList`
295+
296+
Allows only a specific set of keys.
297+
298+
```php
299+
<?php
300+
use ParagonIE\Ionizer\GeneralFilterContainer;
301+
use ParagonIE\Ionizer\IndexPolicy\IndexAllowList;
302+
303+
$gfc = new GeneralFilterContainer();
304+
$gfc->setIndexPolicy(new IndexAllowList('foo', 'bar', 'baz'));
305+
```
306+
307+
#### Custom Index Policies
308+
309+
To create your own index policy, create a class that implements
310+
[`IndexPolicyInterface`](../src/Contract/IndexPolicyInterface.php).
311+
312+
```php
313+
<?php
314+
use ParagonIE\Ionizer\Contract\IndexPolicyInterface;
315+
316+
class FooPolicy implements IndexPolicyInterface
317+
{
318+
public function indexIsValid($index): bool
319+
{
320+
return $index === 'foo';
321+
}
322+
}
323+
```
324+
249325
## Other Filters
250326

251327
### `AllowList`

0 commit comments

Comments
 (0)