Skip to content

Commit

Permalink
Merge pull request #238 from gsteel/is-array-validator
Browse files Browse the repository at this point in the history
Add an IsArray Validator
  • Loading branch information
Ocramius authored Mar 26, 2024
2 parents 2f3aa22 + ed51b4d commit 29087fb
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/book/set.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The following validators come with the laminas-validator distribution.
- [Identical](validators/identical.md)
- [InArray](validators/in-array.md)
- [Ip](validators/ip.md)
- [IsArray](validators/is-array.md)
- [Isbn](validators/isbn.md)
- [IsCountable](validators/is-countable.md)
- [IsInstanceOf](validators/isinstanceof.md)
Expand Down
14 changes: 14 additions & 0 deletions docs/book/validators/is-array.md
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
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ nav:
- Identical: validators/identical.md
- InArray: validators/in-array.md
- Ip: validators/ip.md
- IsArray: validators/is-array.md
- Isbn: validators/isbn.md
- IsCountable: validators/is-countable.md
- IsInstanceOf: validators/isinstanceof.md
Expand Down
7 changes: 6 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<code><![CDATA[$this]]></code>
<code><![CDATA[$this]]></code>
<code><![CDATA[$this]]></code>
<code><![CDATA[$this]]></code>
</LessSpecificReturnStatement>
<MixedArgumentTypeCoercion>
<code><![CDATA[$value]]></code>
Expand Down Expand Up @@ -81,7 +82,6 @@
<code><![CDATA[$this->options]]></code>
<code><![CDATA[$this->options]]></code>
<code><![CDATA[$this->options]]></code>
<code><![CDATA[$this->options]]></code>
</UndefinedThisPropertyAssignment>
</file>
<file src="src/Barcode.php">
Expand Down Expand Up @@ -2654,6 +2654,11 @@
<code><![CDATA[ipvFutureProvider]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/IsArrayTest.php">
<PossiblyUnusedMethod>
<code><![CDATA[invalidValueProvider]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/IsCountableTest.php">
<PossiblyInvalidArgument>
<code><![CDATA[$options]]></code>
Expand Down
40 changes: 40 additions & 0 deletions src/IsArray.php
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;
}
}
2 changes: 2 additions & 0 deletions src/ValidatorPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class ValidatorPluginManager extends AbstractPluginManager
'Int' => I18nValidator\IsInt::class,
'ip' => Ip::class,
'Ip' => Ip::class,
'IsArray' => IsArray::class,
'isbn' => Isbn::class,
'Isbn' => Isbn::class,
'isCountable' => IsCountable::class,
Expand Down Expand Up @@ -378,6 +379,7 @@ class ValidatorPluginManager extends AbstractPluginManager
InArray::class => InvokableFactory::class,
I18nValidator\IsInt::class => InvokableFactory::class,
Ip::class => InvokableFactory::class,
IsArray::class => InvokableFactory::class,
Isbn::class => InvokableFactory::class,
IsCountable::class => InvokableFactory::class,
IsInstanceOf::class => InvokableFactory::class,
Expand Down
9 changes: 4 additions & 5 deletions test/Db/TestAsset/ConcreteDbValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ final class ConcreteDbValidator extends AbstractDb
'barMessage' => '%value% was wrong',
];

/**
* @param mixed $value
* @return bool
*/
public function isValid($value)
/** @var array<never, never> */
protected $options = [];

public function isValid(mixed $value): bool
{
$this->setValue($value);
$this->error(self::FOO_MESSAGE);
Expand Down
55 changes: 55 additions & 0 deletions test/IsArrayTest.php
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]
);
}
}

0 comments on commit 29087fb

Please sign in to comment.