-
Notifications
You must be signed in to change notification settings - Fork 1
Add AssertUniqueItems validator #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,74 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\InputMapper\Compiler\Validator\Array; | ||
|
||
use Attribute; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Stmt; | ||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\TypeNode; | ||
use ShipMonk\InputMapper\Compiler\Php\PhpCodeBuilder; | ||
use ShipMonk\InputMapper\Compiler\Validator\ValidatorCompiler; | ||
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)] | ||
class AssertUniqueItems implements ValidatorCompiler | ||
{ | ||
|
||
/** | ||
* @return list<Stmt> | ||
*/ | ||
public function compile(Expr $value, TypeNode $type, Expr $path, PhpCodeBuilder $builder): array | ||
{ | ||
[$indexVariableName, $itemVariableName, $innerLoopIndexVariableName] = $builder->uniqVariableNames( | ||
'index', | ||
'item', | ||
'innerIndex', | ||
'innerLoopItem', | ||
); | ||
|
||
$statements = []; | ||
|
||
$length = $builder->funcCall($builder->importFunction('count'), [$value]); | ||
|
||
$statements[] = $builder->foreach($value, $builder->var($itemVariableName), $builder->var($indexVariableName), [ | ||
$builder->for( | ||
$builder->assignExpr( | ||
$builder->var($innerLoopIndexVariableName), | ||
$builder->plus($builder->var($indexVariableName), $builder->val(1)), | ||
), | ||
$builder->lt($builder->var($innerLoopIndexVariableName), $length), | ||
$builder->preIncrement($builder->var($innerLoopIndexVariableName)), | ||
[ | ||
$builder->if( | ||
$builder->same( | ||
$builder->var($itemVariableName), | ||
$builder->arrayDimFetch($value, $builder->var($innerLoopIndexVariableName)), | ||
), | ||
[ | ||
$builder->throw( | ||
$builder->staticCall( | ||
$builder->importClass(MappingFailedException::class), | ||
'duplicateValue', | ||
[ | ||
$builder->var($itemVariableName), | ||
$path, | ||
$builder->val('list with unique items'), | ||
], | ||
), | ||
), | ||
], | ||
), | ||
], | ||
), | ||
]); | ||
|
||
return $statements; | ||
} | ||
|
||
public function getInputType(): TypeNode | ||
{ | ||
return new IdentifierTypeNode('list'); | ||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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,45 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonkTests\InputMapper\Compiler\Validator\Array; | ||
|
||
use ShipMonk\InputMapper\Compiler\Mapper\Array\MapList; | ||
use ShipMonk\InputMapper\Compiler\Mapper\Scalar\MapInt; | ||
use ShipMonk\InputMapper\Compiler\Mapper\Scalar\MapString; | ||
use ShipMonk\InputMapper\Compiler\Validator\Array\AssertUniqueItems; | ||
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException; | ||
use ShipMonkTests\InputMapper\Compiler\Validator\ValidatorCompilerTestCase; | ||
|
||
class AssertUniqueItemsTest extends ValidatorCompilerTestCase | ||
{ | ||
|
||
public function testUniqueItemsIntValidator(): void | ||
{ | ||
$mapperCompiler = new MapList(new MapInt()); | ||
$validatorCompiler = new AssertUniqueItems(); | ||
$validator = $this->compileValidator('UniqueItemsIntValidator', $mapperCompiler, $validatorCompiler); | ||
|
||
$validator->map([1, 2, 3]); | ||
|
||
self::assertException( | ||
MappingFailedException::class, | ||
'Failed to map data at path /: Expected list with unique items, got 1 multiple times', | ||
static fn() => $validator->map([1, 2, 1]), | ||
); | ||
} | ||
|
||
public function testUniqueItemsStringValidator(): void | ||
{ | ||
$mapperCompiler = new MapList(new MapString()); | ||
$validatorCompiler = new AssertUniqueItems(); | ||
$validator = $this->compileValidator('UniqueItemsStringValidator', $mapperCompiler, $validatorCompiler); | ||
|
||
$validator->map(['abc', 'def', 'fg']); | ||
|
||
self::assertException( | ||
MappingFailedException::class, | ||
'Failed to map data at path /: Expected list with unique items, got "def" multiple times', | ||
static fn() => $validator->map(['abc', 'def', 'def', 'fgq']), | ||
); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
tests/Compiler/Validator/Array/Data/UniqueItemsIntValidatorMapper.php
This file contains hidden or 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,56 @@ | ||
<?php declare (strict_types=1); | ||
|
||
namespace ShipMonkTests\InputMapper\Compiler\Validator\Array\Data; | ||
|
||
use ShipMonk\InputMapper\Compiler\Mapper\Wrapper\ValidatedMapperCompiler; | ||
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException; | ||
use ShipMonk\InputMapper\Runtime\Mapper; | ||
use ShipMonk\InputMapper\Runtime\MapperProvider; | ||
use function array_is_list; | ||
use function count; | ||
use function is_array; | ||
use function is_int; | ||
|
||
/** | ||
* Generated mapper by {@see ValidatedMapperCompiler}. Do not edit directly. | ||
* | ||
* @implements Mapper<list<int>> | ||
*/ | ||
class UniqueItemsIntValidatorMapper implements Mapper | ||
{ | ||
public function __construct(private readonly MapperProvider $provider) | ||
{ | ||
} | ||
|
||
/** | ||
* @param list<string|int> $path | ||
* @return list<int> | ||
* @throws MappingFailedException | ||
*/ | ||
public function map(mixed $data, array $path = []): array | ||
{ | ||
if (!is_array($data) || !array_is_list($data)) { | ||
throw MappingFailedException::incorrectType($data, $path, 'list'); | ||
} | ||
|
||
$mapped = []; | ||
|
||
foreach ($data as $index => $item) { | ||
if (!is_int($item)) { | ||
throw MappingFailedException::incorrectType($item, [...$path, $index], 'int'); | ||
} | ||
|
||
$mapped[] = $item; | ||
} | ||
|
||
foreach ($mapped as $index2 => $item2) { | ||
for ($innerIndex = $index2 + 1; $innerIndex < count($mapped); ++$innerIndex) { | ||
if ($item2 === $mapped[$innerIndex]) { | ||
throw MappingFailedException::duplicateValue($item2, $path, 'list with unique items'); | ||
} | ||
} | ||
} | ||
|
||
return $mapped; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
tests/Compiler/Validator/Array/Data/UniqueItemsStringValidatorMapper.php
This file contains hidden or 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,56 @@ | ||
<?php declare (strict_types=1); | ||
|
||
namespace ShipMonkTests\InputMapper\Compiler\Validator\Array\Data; | ||
|
||
use ShipMonk\InputMapper\Compiler\Mapper\Wrapper\ValidatedMapperCompiler; | ||
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException; | ||
use ShipMonk\InputMapper\Runtime\Mapper; | ||
use ShipMonk\InputMapper\Runtime\MapperProvider; | ||
use function array_is_list; | ||
use function count; | ||
use function is_array; | ||
use function is_string; | ||
|
||
/** | ||
* Generated mapper by {@see ValidatedMapperCompiler}. Do not edit directly. | ||
* | ||
* @implements Mapper<list<string>> | ||
*/ | ||
class UniqueItemsStringValidatorMapper implements Mapper | ||
{ | ||
public function __construct(private readonly MapperProvider $provider) | ||
{ | ||
} | ||
|
||
/** | ||
* @param list<string|int> $path | ||
* @return list<string> | ||
* @throws MappingFailedException | ||
*/ | ||
public function map(mixed $data, array $path = []): array | ||
{ | ||
if (!is_array($data) || !array_is_list($data)) { | ||
throw MappingFailedException::incorrectType($data, $path, 'list'); | ||
} | ||
|
||
$mapped = []; | ||
|
||
foreach ($data as $index => $item) { | ||
if (!is_string($item)) { | ||
throw MappingFailedException::incorrectType($item, [...$path, $index], 'string'); | ||
} | ||
|
||
$mapped[] = $item; | ||
} | ||
|
||
foreach ($mapped as $index2 => $item2) { | ||
for ($innerIndex = $index2 + 1; $innerIndex < count($mapped); ++$innerIndex) { | ||
if ($item2 === $mapped[$innerIndex]) { | ||
throw MappingFailedException::duplicateValue($item2, $path, 'list with unique items'); | ||
} | ||
} | ||
} | ||
|
||
return $mapped; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.