-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
767fcaa
commit f08a1fa
Showing
11 changed files
with
180 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# ArrayType | ||
|
||
- `v::arrayType()` | ||
|
||
Validates whether the type of an input is array. | ||
|
||
```php | ||
v::arrayType()->validate([]); // true | ||
v::arrayType()->validate([1, 2, 3]); // true | ||
v::arrayType()->validate(new ArrayObject()); // false | ||
``` | ||
|
||
*** | ||
See also: | ||
|
||
* [ArrayVal](ArrayVal.md) | ||
* [Countable](Countable.md) | ||
* [Iterable](Iterable.md) | ||
* [Iterable](Iterable.md) |
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,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Respect/Validation. | ||
* | ||
* (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the "LICENSE.md" | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Respect\Validation\Exceptions; | ||
|
||
class ArrayTypeException extends ValidationException | ||
{ | ||
public static $defaultTemplates = [ | ||
self::MODE_DEFAULT => [ | ||
self::STANDARD => '{{name}} must be of the type array', | ||
], | ||
self::MODE_NEGATIVE => [ | ||
self::STANDARD => '{{name}} must not be of the type array', | ||
], | ||
]; | ||
} |
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,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Respect/Validation. | ||
* | ||
* (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the "LICENSE.md" | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Respect\Validation\Rules; | ||
|
||
class ArrayType extends AbstractRule | ||
{ | ||
public function validate($input) | ||
{ | ||
return is_array($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,10 @@ | ||
--FILE-- | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
use Respect\Validation\Validator as v; | ||
|
||
v::arrayType()->assert([]); | ||
v::arrayType()->check([1, 2, 3]); | ||
?> | ||
--EXPECTF-- |
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,15 @@ | ||
--FILE-- | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
use Respect\Validation\Exceptions\ArrayTypeException; | ||
use Respect\Validation\Validator as v; | ||
|
||
try { | ||
v::arrayType()->check('teste'); | ||
} catch (ArrayTypeException $exception) { | ||
echo $exception->getMainMessage(); | ||
} | ||
?> | ||
--EXPECTF-- | ||
"teste" must be of the type array |
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,15 @@ | ||
--FILE-- | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
use Respect\Validation\Exceptions\AllOfException; | ||
use Respect\Validation\Validator as v; | ||
|
||
try { | ||
v::arrayType()->assert(new ArrayObject()); | ||
} catch (AllOfException $exception) { | ||
echo $exception->getFullMessage(); | ||
} | ||
?> | ||
--EXPECTF-- | ||
\-`[traversable] (ArrayObject: { })` must be of the type array |
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,15 @@ | ||
--FILE-- | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
use Respect\Validation\Exceptions\ArrayTypeException; | ||
use Respect\Validation\Validator as v; | ||
|
||
try { | ||
v::not(v::arrayType())->check([]); | ||
} catch (ArrayTypeException $exception) { | ||
echo $exception->getMainMessage(); | ||
} | ||
?> | ||
--EXPECTF-- | ||
{ } must not be of the type array |
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,15 @@ | ||
--FILE-- | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
use Respect\Validation\Exceptions\AllOfException; | ||
use Respect\Validation\Validator as v; | ||
|
||
try { | ||
v::not(v::arrayType())->assert([1, 2, 3]); | ||
} catch (AllOfException $exception) { | ||
echo $exception->getFullMessage(); | ||
} | ||
?> | ||
--EXPECTF-- | ||
\-{ 1, 2, 3 } must not be of the type array |
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 | ||
|
||
/* | ||
* This file is part of Respect/Validation. | ||
* | ||
* (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the "LICENSE.md" | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Respect\Validation\Rules; | ||
|
||
/** | ||
* @group rule | ||
* @covers Respect\Validation\Rules\ArrayType | ||
*/ | ||
class ArrayTypeTest extends RuleTestCase | ||
{ | ||
public function providerForValidInput() | ||
{ | ||
$rule = new ArrayType(); | ||
|
||
return [ | ||
[$rule, []], | ||
[$rule, [1, 2, 3]], | ||
]; | ||
} | ||
|
||
public function providerForInvalidInput() | ||
{ | ||
$rule = new ArrayType(); | ||
|
||
return [ | ||
[$rule, 'test'], | ||
[$rule, 1], | ||
[$rule, 1.0], | ||
[$rule, true], | ||
[$rule, new \ArrayObject()], | ||
[$rule, new \ArrayIterator()], | ||
]; | ||
} | ||
} |