-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Henrique Moody <[email protected]>
- Loading branch information
1 parent
f1b6f0c
commit e99d33f
Showing
13 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# KeyExists | ||
|
||
- `KeyExists(int|string $key)` | ||
|
||
Validates if the given key exists in an [array][] or [ArrayAccess][]. | ||
|
||
```php | ||
v::keyExists('name')->validate(['name' => 'The Respect Panda']); // true | ||
v::keyExists('name')->validate(['email' => '[email protected]']); // false | ||
|
||
v::keyExists(0)->validate(['a', 'b', 'c']); // true | ||
v::keyExists(4)->validate(['a', 'b', 'c']); // false | ||
|
||
v::keyExists('username')->validate(new ArrayObject(['username' => 'therespectpanda'])); // true | ||
v::keyExists(5)->validate(new ArrayObject(['a', 'b', 'c'])); // false | ||
``` | ||
|
||
## Categorization | ||
|
||
- Arrays | ||
- Structures | ||
|
||
## Changelog | ||
|
||
| Version | Description | | ||
| ------: | ------------------ | | ||
| 3.0.0 | Created from `Key` | | ||
|
||
*** | ||
See also: | ||
|
||
- [ArrayType](ArrayType.md) | ||
- [ArrayVal](ArrayVal.md) | ||
- [Each](Each.md) | ||
- [Key](Key.md) | ||
- [Property](Property.md) | ||
|
||
[array]: https://www.php.net/array | ||
[ArrayAccess]: https://www.php.net/arrayaccess |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Rules; | ||
|
||
use ArrayAccess; | ||
use Respect\Validation\Message\Template; | ||
use Respect\Validation\Result; | ||
|
||
use function array_key_exists; | ||
|
||
#[Template( | ||
'Key {{name}} must be present', | ||
'Key {{name}} must not be present', | ||
)] | ||
final class KeyExists extends Standard | ||
{ | ||
public function __construct( | ||
private readonly int|string $key | ||
) { | ||
} | ||
|
||
public function evaluate(mixed $input): Result | ||
{ | ||
return new Result($this->hasKey($input), $input, $this, name: (string) $this->key); | ||
} | ||
|
||
private function hasKey(mixed $input): bool | ||
{ | ||
return $input instanceof ArrayAccess ? $input->offsetExists($this->key) : array_key_exists($this->key, $input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use Respect\Validation\Validator as v; | ||
|
||
run([ | ||
'Default mode' => [v::keyExists('foo'), ['bar' => 'baz']], | ||
'Negative mode' => [v::not(v::keyExists('foo')), ['foo' => 'baz']], | ||
'Custom name' => [v::keyExists('foo')->setName('Custom name'), ['bar' => 'baz']], | ||
'Custom template' => [v::keyExists('foo'), ['bar' => 'baz'], 'Custom template for `{{name}}`'], | ||
]); | ||
?> | ||
--EXPECT-- | ||
Default mode | ||
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺ | ||
Key foo must be present | ||
- Key foo must be present | ||
[ | ||
'foo' => 'Key foo must be present', | ||
] | ||
|
||
Negative mode | ||
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺ | ||
Key foo must not be present | ||
- Key foo must not be present | ||
[ | ||
'foo' => 'Key foo must not be present', | ||
] | ||
|
||
Custom name | ||
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺ | ||
Key Custom name must be present | ||
- Key Custom name must be present | ||
[ | ||
'Custom name' => 'Key Custom name must be present', | ||
] | ||
|
||
Custom template | ||
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺ | ||
Custom template for `foo` | ||
- Custom template for `foo` | ||
[ | ||
'foo' => 'Custom template for `foo`', | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Rules; | ||
|
||
use ArrayObject; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use Respect\Validation\Test\RuleTestCase; | ||
|
||
use function array_map; | ||
|
||
#[CoversClass(KeyExists::class)] | ||
final class KeyExistsTest extends RuleTestCase | ||
{ | ||
/** @return array<string, array{KeyExists, mixed}> */ | ||
public static function providerForValidInput(): array | ||
{ | ||
return [ | ||
'string key from array' => [new KeyExists('foo'), ['foo' => 'bar']], | ||
'int key from array' => [new KeyExists(0), [0 => 'bar']], | ||
'string key from ArrayAccess' => [new KeyExists('foo'), ['foo' => 'bar']], | ||
'int key from ArrayAccess' => [new KeyExists(0),new ArrayObject([0 => 'bar'])], | ||
]; | ||
} | ||
|
||
/** @return array<string, array{KeyExists, mixed}> */ | ||
public static function providerForInvalidInput(): array | ||
{ | ||
return [ | ||
'missing string key' => [new KeyExists('foo'), []], | ||
'missing int key' => [new KeyExists(0), []], | ||
] + array_map( | ||
static fn(mixed $input) => [new KeyExists('bar'), $input], | ||
self::providerForNonArrayValues() | ||
); | ||
} | ||
} |