-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from gsteel/is-array-validator
Add an IsArray Validator
- Loading branch information
Showing
8 changed files
with
123 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# IsArray Validator | ||
|
||
- **Since 2.52.0** | ||
|
||
`Laminas\Validator\IsArray` checks that a given value is an array. There are no options. | ||
|
||
## Example Usage | ||
|
||
```php | ||
$validator = new Laminas\Validator\IsArray(); | ||
|
||
$validator->isValid('Not an Array'); // false | ||
$validator->isValid(['Any Array']); // true | ||
``` |
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,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Validator; | ||
|
||
use function get_debug_type; | ||
use function is_array; | ||
|
||
final class IsArray extends AbstractValidator | ||
{ | ||
public const NOT_ARRAY = 'NotArray'; | ||
|
||
/** @var array<non-empty-string, non-empty-string> */ | ||
protected $messageTemplates = [ | ||
self::NOT_ARRAY => 'Expected an array value but %type% provided', | ||
]; | ||
|
||
/** @var array<string, string> */ | ||
protected $messageVariables = [ | ||
'type' => 'type', | ||
]; | ||
|
||
protected ?string $type = null; | ||
|
||
/** @var array<never, never> */ | ||
protected $options = []; | ||
|
||
public function isValid(mixed $value): bool | ||
{ | ||
if (is_array($value)) { | ||
return true; | ||
} | ||
|
||
$this->type = get_debug_type($value); | ||
$this->error(self::NOT_ARRAY); | ||
|
||
return false; | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\Validator; | ||
|
||
use Laminas\Validator\IsArray; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(IsArray::class)] | ||
class IsArrayTest extends TestCase | ||
{ | ||
/** @return array<string, array{0: mixed}> */ | ||
public static function invalidValueProvider(): array | ||
{ | ||
return [ | ||
'String' => ['foo'], | ||
'Integer' => [123], | ||
'Float' => [4.2], | ||
'Object' => [(object) ['foo' => 'bar']], | ||
'Boolean' => [true], | ||
'Null' => [null], | ||
]; | ||
} | ||
|
||
#[DataProvider('invalidValueProvider')] | ||
public function testInvalidValuesAreDeemedInvalid(mixed $input): void | ||
{ | ||
$validator = new IsArray(); | ||
self::assertFalse($validator->isValid($input)); | ||
} | ||
|
||
public function testThatAnArrayIsConsideredValid(): void | ||
{ | ||
self::assertTrue( | ||
(new IsArray())->isValid(['whatever']), | ||
); | ||
} | ||
|
||
public function testThatTheInvalidTypeIsPresentInTheErrorMessage(): void | ||
{ | ||
$validator = new IsArray(); | ||
self::assertFalse($validator->isValid('Foo')); | ||
$messages = $validator->getMessages(); | ||
|
||
self::assertArrayHasKey(IsArray::NOT_ARRAY, $messages); | ||
|
||
self::assertEquals( | ||
'Expected an array value but string provided', | ||
$messages[IsArray::NOT_ARRAY] | ||
); | ||
} | ||
} |