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
8 changed files
with
164 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
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
59 changes: 59 additions & 0 deletions
59
src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaUrlRestriction.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,59 @@ | ||
<?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\Url; | ||
use Symfony\Component\Validator\Constraints\UrlValidator; | ||
|
||
/** | ||
* @author Tomas Norkūnas <norkunas.tom@gmail.com> | ||
*/ | ||
final class PropertySchemaUrlRestriction implements PropertySchemaRestrictionMetadataInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param Url $constraint | ||
*/ | ||
public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array | ||
{ | ||
$pattern = $constraint->relativeProtocol ? str_replace('(%s):', '(?:(%s):)?', UrlValidator::PATTERN) : UrlValidator::PATTERN; | ||
|
||
$pattern = array_map(static function (string $line) { | ||
$line = trim($line); | ||
|
||
$commentPos = strrpos($line, '#'); | ||
if (false !== $commentPos) { | ||
$line = trim(substr($line, 0, $commentPos)); | ||
} | ||
|
||
return $line; | ||
}, explode("\n", $pattern)); | ||
|
||
$pattern = substr(implode('', $pattern), 1, -4); // strip options and delimiters | ||
$pattern = sprintf($pattern, implode('|', $constraint->protocols)); | ||
|
||
return ['pattern' => $pattern]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Constraint $constraint, PropertyMetadata $propertyMetadata): bool | ||
{ | ||
return $constraint instanceof Url; | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
...idge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaUrlRestrictionTest.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 | ||
|
||
/* | ||
* 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\PropertySchemaUrlRestriction; | ||
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\Positive; | ||
use Symfony\Component\Validator\Constraints\Url; | ||
|
||
/** | ||
* @author Tomas Norkūnas <norkunas.tom@gmail.com> | ||
*/ | ||
final class PropertySchemaUrlRestrictionTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private $propertySchemaUrlRestriction; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->propertySchemaUrlRestriction = new PropertySchemaUrlRestriction(); | ||
} | ||
|
||
/** | ||
* @dataProvider supportsProvider | ||
*/ | ||
public function testSupports(Constraint $constraint, PropertyMetadata $propertyMetadata, bool $expectedResult): void | ||
{ | ||
self::assertSame($expectedResult, $this->propertySchemaUrlRestriction->supports($constraint, $propertyMetadata)); | ||
} | ||
|
||
public function supportsProvider(): \Generator | ||
{ | ||
yield 'supported' => [new Url(), new PropertyMetadata(), true]; | ||
yield 'not supported' => [new Positive(), new PropertyMetadata(), false]; | ||
} | ||
|
||
public function testCreate(): void | ||
{ | ||
self::assertArrayHasKey('pattern', $this->propertySchemaUrlRestriction->create(new Url(), new PropertyMetadata())); | ||
} | ||
} |
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