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
7 changed files
with
371 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
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
128 changes: 128 additions & 0 deletions
128
...e/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaCollectionRestriction.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,128 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction; | ||
|
||
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Constraints\Collection; | ||
use Symfony\Component\Validator\Constraints\Optional; | ||
use Symfony\Component\Validator\Constraints\Required; | ||
use Symfony\Component\Validator\Constraints\Type; | ||
|
||
/** | ||
* @author Tomas Norkūnas <norkunas.tom@gmail.com> | ||
*/ | ||
final class PropertySchemaCollectionRestriction implements PropertySchemaRestrictionMetadataInterface | ||
{ | ||
/** | ||
* @var iterable<PropertySchemaRestrictionMetadataInterface> | ||
*/ | ||
private $restrictionsMetadata; | ||
|
||
/** | ||
* @param iterable<PropertySchemaRestrictionMetadataInterface> $restrictionsMetadata | ||
*/ | ||
public function __construct(iterable $restrictionsMetadata = []) | ||
{ | ||
$this->restrictionsMetadata = $restrictionsMetadata; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param Collection $constraint | ||
*/ | ||
public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array | ||
{ | ||
$restriction = [ | ||
'type' => 'object', | ||
'properties' => [], | ||
'additionalProperties' => $constraint->allowExtraFields, | ||
]; | ||
$required = []; | ||
|
||
foreach ($constraint->fields as $field => $baseConstraint) { | ||
/** @var Required|Optional $baseConstraint */ | ||
if ($baseConstraint instanceof Required) { | ||
$required[] = $field; | ||
} | ||
|
||
$restriction['properties'][$field] = $this->mergeConstraintRestrictions($baseConstraint, $propertyMetadata); | ||
} | ||
|
||
if ($required) { | ||
$restriction['required'] = $required; | ||
} | ||
|
||
return $restriction; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Constraint $constraint, PropertyMetadata $propertyMetadata): bool | ||
{ | ||
return $constraint instanceof Collection; | ||
} | ||
|
||
/** | ||
* @param Required|Optional $constraint | ||
*/ | ||
private function mergeConstraintRestrictions(Constraint $constraint, PropertyMetadata $propertyMetadata): array | ||
{ | ||
$propertyRestrictions = [['type' => 'string']]; | ||
$nestedConstraints = method_exists($constraint, 'getNestedContraints') ? $constraint->getNestedContraints() : $constraint->constraints; | ||
|
||
foreach ($nestedConstraints as $nestedContraint) { | ||
if ($nestedContraint instanceof Type) { | ||
/** @var string $nestedType */ | ||
$nestedType = $nestedContraint->type; | ||
$propertyRestrictions[0]['type'] = $this->getConstraintType($nestedType); | ||
} | ||
|
||
foreach ($this->restrictionsMetadata as $restrictionMetadata) { | ||
if ($restrictionMetadata->supports($nestedContraint, $propertyMetadata) && !empty($nestedContraintRestriction = $restrictionMetadata->create($nestedContraint, $propertyMetadata))) { | ||
$propertyRestrictions[] = $nestedContraintRestriction; | ||
} | ||
} | ||
} | ||
|
||
return array_merge([], ...$propertyRestrictions); | ||
} | ||
|
||
private function getConstraintType(string $type): string | ||
{ | ||
if (\in_array($type, ['bool', 'boolean'], true)) { | ||
return 'boolean'; | ||
} | ||
|
||
if (\in_array($type, ['int', 'integer', 'long'], true)) { | ||
return 'integer'; | ||
} | ||
|
||
if (\in_array($type, ['float', 'double', 'real', 'numeric'], true)) { | ||
return 'number'; | ||
} | ||
|
||
if (\in_array($type, ['array', 'iterable'], true)) { | ||
return 'array'; | ||
} | ||
|
||
if ('object' === $type) { | ||
return 'object'; | ||
} | ||
|
||
return 'string'; | ||
} | ||
} |
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
121 changes: 121 additions & 0 deletions
121
...mfony/Validator/Metadata/Property/Restriction/PropertySchemaCollectionRestrictionTest.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,121 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Validator\Metadata\Property\Restriction; | ||
|
||
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaCollectionRestriction; | ||
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaFormat; | ||
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaLengthRestriction; | ||
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRegexRestriction; | ||
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
use ApiPlatform\Core\Tests\ProphecyTrait; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Constraints\Collection; | ||
use Symfony\Component\Validator\Constraints\Email; | ||
use Symfony\Component\Validator\Constraints\Length; | ||
use Symfony\Component\Validator\Constraints\NotBlank; | ||
use Symfony\Component\Validator\Constraints\NotNull; | ||
use Symfony\Component\Validator\Constraints\Optional; | ||
use Symfony\Component\Validator\Constraints\Positive; | ||
use Symfony\Component\Validator\Constraints\Regex; | ||
use Symfony\Component\Validator\Constraints\Required; | ||
|
||
/** | ||
* @author Tomas Norkūnas <norkunas.tom@gmail.com> | ||
*/ | ||
final class PropertySchemaCollectionRestrictionTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private $propertySchemaCollectionRestriction; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->propertySchemaCollectionRestriction = new PropertySchemaCollectionRestriction([ | ||
new PropertySchemaLengthRestriction(), | ||
new PropertySchemaRegexRestriction(), | ||
new PropertySchemaFormat(), | ||
new PropertySchemaCollectionRestriction(), | ||
]); | ||
} | ||
|
||
/** | ||
* @dataProvider supportsProvider | ||
*/ | ||
public function testSupports(Constraint $constraint, PropertyMetadata $propertyMetadata, bool $expectedResult): void | ||
{ | ||
self::assertSame($expectedResult, $this->propertySchemaCollectionRestriction->supports($constraint, $propertyMetadata)); | ||
} | ||
|
||
public function supportsProvider(): \Generator | ||
{ | ||
yield 'supported' => [new Collection(['fields' => []]), new PropertyMetadata(), true]; | ||
|
||
yield 'not supported' => [new Positive(), new PropertyMetadata(), false]; | ||
} | ||
|
||
/** | ||
* @dataProvider createProvider | ||
*/ | ||
public function testCreate(Constraint $constraint, PropertyMetadata $propertyMetadata, array $expectedResult): void | ||
{ | ||
self::assertSame($expectedResult, $this->propertySchemaCollectionRestriction->create($constraint, $propertyMetadata)); | ||
} | ||
|
||
public function createProvider(): \Generator | ||
{ | ||
yield 'empty' => [new Collection(['fields' => []]), new PropertyMetadata(), ['type' => 'object', 'properties' => [], 'additionalProperties' => false]]; | ||
|
||
yield 'with fields' => [ | ||
new Collection([ | ||
'allowExtraFields' => true, | ||
'fields' => [ | ||
'name' => new Required([ | ||
new NotBlank(), | ||
]), | ||
'email' => [ | ||
new NotNull(), | ||
new Length(['min' => 2, 'max' => 255]), | ||
new Email(['mode' => Email::VALIDATION_MODE_LOOSE]), | ||
], | ||
'phone' => new Optional([ | ||
new \Symfony\Component\Validator\Constraints\Type(['type' => 'string']), | ||
new Regex(['pattern' => '/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/']), | ||
]), | ||
'age' => new Optional([ | ||
new \Symfony\Component\Validator\Constraints\Type(['type' => 'int']), | ||
]), | ||
'social' => new Collection([ | ||
'fields' => [ | ||
'githubUsername' => new NotNull(), | ||
], | ||
]), | ||
], | ||
]), | ||
new PropertyMetadata(), | ||
[ | ||
'type' => 'object', | ||
'properties' => [ | ||
'name' => ['type' => 'string'], | ||
'email' => ['type' => 'string', 'format' => 'email'], | ||
'phone' => ['type' => 'string', 'pattern' => '[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*'], | ||
'age' => ['type' => 'integer'], | ||
'social' => ['type' => 'object', 'properties' => ['githubUsername' => ['type' => 'string']], 'additionalProperties' => false, 'required' => ['githubUsername']], | ||
], | ||
'additionalProperties' => true, | ||
'required' => ['name', 'email', 'social'], | ||
], | ||
]; | ||
} | ||
} |
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
Oops, something went wrong.