Skip to content

Commit

Permalink
Merge pull request #8 from hopeseekr/better_nullable_arrays
Browse files Browse the repository at this point in the history
Allow nullable and empty arrays of something.
  • Loading branch information
hopeseekr authored Jul 30, 2019
2 parents 64856c3 + 3cff6e5 commit fbde45e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ PHPExperts\DataTypeValidator\DataTypeValidator
✔ Data cannot be null by default
✔ Any data type that starts with a '?' is nullable
✔ Any data type that ends with '[]' is an array of X
✔ Will allow an empty array of something
✔ Will allow a nullable array of something
✔ Will throw a logic exception if a non string rule is given

PHPExperts\DataTypeValidator\DataTypeValidator: Assertions
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@
"psr-4": {
"PHPExperts\\DataTypeValidator\\Tests\\": "tests/"
}
},
"config": {
"classmap-authoritative": true
}
}
7 changes: 6 additions & 1 deletion src/DataTypeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function assertIsSpecificObject($value, string $className)
$this->assertIsType($value, $className);
}

public function assertIsArrayOfSomething($values, string $dataType): void
public function assertIsArrayOfSomething($values, string $dataType)
{
$this->assertIsArray($values);

Expand Down Expand Up @@ -247,6 +247,11 @@ private function validateArraysOfSomething($values, string $expectedType)
// Allow nullable types.
$nullableType = $this->extractNullableProperty($expectedType);
if ($nullableType !== $expectedType) {
// If the data type is nullable and the value is null, let's bail early.
if ($values === null) {
return;
}

$expectedType = $nullableType;
}

Expand Down
13 changes: 7 additions & 6 deletions tests/DataTypeValidatorAssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,20 @@ public function testWillAssertAnObjectByItsFullName()

public function testWillAssertAnArrayOfSomething()
{
// @todo: Should we really accept *both* 'int' and 'int[]' as arrays? Maybe just 'int[]' is better?
$goodArrays = [
'int' => $this->getDataByType('int', DataTypesLists::getValidStrictDataAndTypes()),
'int' => $this->getDataByType('int', DataTypesLists::getValidStrictDataAndTypes()),
'int[]' => $this->getDataByType('int', DataTypesLists::getValidStrictDataAndTypes()),
'bool' => $this->getDataByType('bool', DataTypesLists::getValidStrictDataAndTypes()),
'bool' => $this->getDataByType('bool', DataTypesLists::getValidStrictDataAndTypes()),
'bool[]' => $this->getDataByType('bool', DataTypesLists::getValidStrictDataAndTypes()),
'float' => $this->getDataByType('float', DataTypesLists::getValidStrictDataAndTypes()),
'float' => $this->getDataByType('float', DataTypesLists::getValidStrictDataAndTypes()),
'float[]' => $this->getDataByType('float', DataTypesLists::getValidStrictDataAndTypes()),
'string' => $this->getDataByType('string', DataTypesLists::getValidStrictDataAndTypes()),
'string' => $this->getDataByType('string', DataTypesLists::getValidStrictDataAndTypes()),
'string[]' => $this->getDataByType('string', DataTypesLists::getValidStrictDataAndTypes()),
'isAStrictDataType' => [new IsAStrictDataType(), new IsAStrictDataType(), new IsAStrictDataType()],
'isAStrictDataType[]' => [new IsAStrictDataType(), new IsAStrictDataType(), new IsAStrictDataType()],
'isAStrictDataType[]' => [new IsAStrictDataType(), new IsAStrictDataType(), new IsAStrictDataType()],
'isAFuzzyDataType' => [new IsAFuzzyDataType(), new IsAFuzzyDataType(), new IsAFuzzyDataType()],
'isAFuzzyDataType[]' => [new IsAFuzzyDataType(), new IsAFuzzyDataType(), new IsAFuzzyDataType()],
'isAFuzzyDataType[]' => [new IsAFuzzyDataType(), new IsAFuzzyDataType(), new IsAFuzzyDataType()],
isAFuzzyDataType::class => [new IsAFuzzyDataType(), new IsAFuzzyDataType(), new IsAFuzzyDataType()],
];

Expand Down
56 changes: 56 additions & 0 deletions tests/DataTypeValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,62 @@ public function testAnyDataTypeThatEndsWithABracketsIsAnArrayOfX()
}
}

public function testWillAllowAnEmptyArrayOfSomething()
{
$nullableTypes = [
'?int[]',
'?bool[]',
'?float[]',
'?string[]',
'?isAStrictDataType[]',
'?isAFuzzyDataType[]',
'null|int[]',
'null|bool[]',
'null|float[]',
'null|string[]',
'null|isAStrictDataType[]',
'null|isAFuzzyDataType[]',
'?' . isAFuzzyDataType::class . '[]',
'null|' . isAFuzzyDataType::class . '[]',
];

$rules = array_combine($nullableTypes, $nullableTypes);
$values = array_combine($nullableTypes, array_fill(0, count($nullableTypes), []));

foreach ($values as $expectedType => $array) {
self::assertTrue($this->fuzzy->validate($values, $rules));
self::assertTrue($this->strict->validate($values, $rules));
}
}

public function testWillAllowANullableArrayOfSomething()
{
$nullableTypes = [
'?int[]',
'?bool[]',
'?float[]',
'?string[]',
'?isAStrictDataType[]',
'?isAFuzzyDataType[]',
'null|int[]',
'null|bool[]',
'null|float[]',
'null|string[]',
'null|isAStrictDataType[]',
'null|isAFuzzyDataType[]',
'?' . isAFuzzyDataType::class . '[]',
'null|' . isAFuzzyDataType::class . '[]',
];

$rules = array_combine($nullableTypes, $nullableTypes);
$values = array_combine($nullableTypes, array_fill(0, count($nullableTypes), null));

foreach ($values as $expectedType => $array) {
self::assertTrue($this->fuzzy->validate($values, $rules));
self::assertTrue($this->strict->validate($values, $rules));
}
}

public function testWillThrowALogicExceptionIfANonStringRuleIsGiven()
{
self::expectException('LogicException');
Expand Down

0 comments on commit fbde45e

Please sign in to comment.