-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
6 changed files
with
280 additions
and
3 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,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 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,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 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 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; | ||
} | ||
} |