Skip to content

Commit f4b7d13

Browse files
Rename key to index
This will be less confusing in applications that deal with cryptography
1 parent 233f87b commit f4b7d13

File tree

11 files changed

+140
-140
lines changed

11 files changed

+140
-140
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
declare(strict_types=1);
33
namespace ParagonIE\Ionizer\Contract;
44

5-
interface KeyPolicyInterface
5+
interface IndexPolicyInterface
66
{
77
/**
8-
* @param array-key $key
8+
* @param array-key $index
99
* @return bool
1010
*/
11-
public function keyIsValid($key): bool;
11+
public function indexIsValid($index): bool;
1212
}

src/Filter/ArrayFilter.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
declare(strict_types=1);
33
namespace ParagonIE\Ionizer\Filter;
44

5-
use ParagonIE\Ionizer\Contract\KeyPolicyInterface;
5+
use ParagonIE\Ionizer\Contract\IndexPolicyInterface;
66
use ParagonIE\Ionizer\InputFilter;
77
use ParagonIE\Ionizer\InvalidDataException;
88

@@ -23,19 +23,19 @@ class ArrayFilter extends InputFilter
2323
protected $type = 'array';
2424

2525
/**
26-
* @var ?KeyPolicyInterface $keyPolicy
26+
* @var ?IndexPolicyInterface $indexPolicy
2727
*/
28-
protected $keyPolicy = null;
28+
protected $indexPolicy = null;
2929

3030
/**
3131
* Add restrictions to the keys allowed in this array
3232
*
33-
* @param KeyPolicyInterface $keyPolicy
33+
* @param IndexPolicyInterface $indexPolicy
3434
* @return $this
3535
*/
36-
public function setKeyPolicy(KeyPolicyInterface $keyPolicy): self
36+
public function setIndexPolicy(IndexPolicyInterface $indexPolicy): self
3737
{
38-
$this->keyPolicy = $keyPolicy;
38+
$this->indexPolicy = $indexPolicy;
3939
return $this;
4040
}
4141

@@ -58,10 +58,10 @@ public function process($data = null)
5858
\sprintf('Expected an array (%s).', $this->index)
5959
);
6060
}
61-
if (!is_null($this->keyPolicy)) {
61+
if (!is_null($this->indexPolicy)) {
6262
$keys = array_keys($data);
6363
foreach ($keys as $arrayKey) {
64-
if (!$this->keyPolicy->keyIsValid($arrayKey)) {
64+
if (!$this->indexPolicy->indexIsValid($arrayKey)) {
6565
throw new \TypeError(
6666
\sprintf("Invalid key (%s) in violation of key policy", $arrayKey)
6767
);

src/IndexPolicy/AnyIndex.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace ParagonIE\Ionizer\IndexPolicy;
4+
5+
use ParagonIE\Ionizer\Contract\IndexPolicyInterface;
6+
7+
class AnyIndex implements IndexPolicyInterface
8+
{
9+
/**
10+
* Any integer or string index is valid.
11+
*
12+
* @param array-key $index
13+
* @return bool
14+
*/
15+
public function indexIsValid($index): bool
16+
{
17+
return is_string($index) || is_int($index);
18+
}
19+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22
declare(strict_types=1);
3-
namespace ParagonIE\Ionizer\KeyPolicy;
3+
namespace ParagonIE\Ionizer\IndexPolicy;
44

5-
use ParagonIE\Ionizer\Contract\KeyPolicyInterface;
5+
use ParagonIE\Ionizer\Contract\IndexPolicyInterface;
66

7-
class KeyAllowList implements KeyPolicyInterface
7+
class IndexAllowList implements IndexPolicyInterface
88
{
99
/** @var array-key[] $allowed */
1010
private $allowed = [];
@@ -20,11 +20,11 @@ public function __construct(...$allowed)
2020
/**
2121
* Any integer or string key is valid.
2222
*
23-
* @param array-key $key
23+
* @param array-key $index
2424
* @return bool
2525
*/
26-
public function keyIsValid($key): bool
26+
public function indexIsValid($index): bool
2727
{
28-
return in_array($key, $this->allowed, true);
28+
return in_array($index, $this->allowed, true);
2929
}
3030
}

src/IndexPolicy/IntegersOnly.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace ParagonIE\Ionizer\IndexPolicy;
4+
5+
use ParagonIE\Ionizer\Contract\IndexPolicyInterface;
6+
7+
class IntegersOnly implements IndexPolicyInterface
8+
{
9+
/**
10+
* Any integer key is valid.
11+
*
12+
* @param array-key $index
13+
* @return bool
14+
*/
15+
public function indexIsValid($index): bool
16+
{
17+
return is_int($index);
18+
}
19+
}

src/IndexPolicy/StringsOnly.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace ParagonIE\Ionizer\IndexPolicy;
4+
5+
use ParagonIE\Ionizer\Contract\IndexPolicyInterface;
6+
7+
class StringsOnly implements IndexPolicyInterface
8+
{
9+
/**
10+
* Any integer or string key is valid.
11+
*
12+
* @param array-key $index
13+
* @return bool
14+
*/
15+
public function indexIsValid($index): bool
16+
{
17+
return is_string($index);
18+
}
19+
}

src/KeyPolicy/AnyKeys.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/KeyPolicy/IntegersOnly.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/KeyPolicy/StringsOnly.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/IndexPolicyTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace ParagonIE\Ionizer\Test;
4+
5+
use ParagonIE\Ionizer\IndexPolicy\{
6+
AnyIndex,
7+
IntegersOnly,
8+
IndexAllowList,
9+
StringsOnly
10+
};
11+
use PHPUnit\Framework\TestCase;
12+
use PHPUnit\Framework\Attributes\CoversClass;
13+
14+
15+
#[CoversClass(AnyIndex::class)]
16+
#[CoversClass(IntegersOnly::class)]
17+
#[CoversClass(IndexAllowList::class)]
18+
#[CoversClass(StringsOnly::class)]
19+
class IndexPolicyTest extends TestCase
20+
{
21+
public function testAnyKeys()
22+
{
23+
$any = new AnyIndex();
24+
$this->assertTrue($any->indexIsValid('foo'));
25+
$this->assertTrue($any->indexIsValid(1));
26+
$this->assertFalse($any->indexIsValid(1.2));
27+
$this->assertFalse($any->indexIsValid([]));
28+
$this->assertFalse($any->indexIsValid(null));
29+
}
30+
31+
public function testIntegersOnly()
32+
{
33+
$int = new IntegersOnly();
34+
$this->assertTrue($int->indexIsValid(0));
35+
$this->assertTrue($int->indexIsValid(1));
36+
$this->assertTrue($int->indexIsValid(-1));
37+
$this->assertFalse($int->indexIsValid('1'));
38+
$this->assertFalse($int->indexIsValid(1.0));
39+
$this->assertFalse($int->indexIsValid('foo'));
40+
}
41+
42+
public function testKeyAllowList()
43+
{
44+
$allow = new IndexAllowList('foo', 'bar', 'baz');
45+
$this->assertTrue($allow->indexIsValid('foo'));
46+
$this->assertTrue($allow->indexIsValid('bar'));
47+
$this->assertTrue($allow->indexIsValid('baz'));
48+
$this->assertFalse($allow->indexIsValid('qux'));
49+
50+
$allow = new IndexAllowList(1, 2, 3);
51+
$this->assertTrue($allow->indexIsValid(1));
52+
$this->assertTrue($allow->indexIsValid(2));
53+
$this->assertTrue($allow->indexIsValid(3));
54+
$this->assertFalse($allow->indexIsValid(4));
55+
$this->assertFalse($allow->indexIsValid('1'));
56+
}
57+
58+
public function testStringsOnly()
59+
{
60+
$str = new StringsOnly();
61+
$this->assertTrue($str->indexIsValid('foo'));
62+
$this->assertTrue($str->indexIsValid('1'));
63+
$this->assertFalse($str->indexIsValid(1));
64+
$this->assertFalse($str->indexIsValid(1.0));
65+
}
66+
}

0 commit comments

Comments
 (0)