-
Notifications
You must be signed in to change notification settings - Fork 3
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
2 changed files
with
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Graphpinator\ExtraTypes; | ||
|
||
final class UUIDType extends \Graphpinator\Type\ScalarType | ||
{ | ||
protected const NAME = 'UUID'; | ||
protected const DESCRIPTION = 'UUID type - string which contains valid UUID (universally unique identifier).'; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
$this->setSpecifiedBy('https://datatracker.ietf.org/doc/html/rfc4122'); | ||
} | ||
|
||
public function validateNonNullValue(mixed $rawValue) : bool | ||
{ | ||
return \is_string($rawValue) | ||
&& \preg_match('/^{?[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}}?$/', $rawValue) === 1; | ||
} | ||
} |
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,62 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Graphpinator\ExtraTypes\Tests\Unit; | ||
|
||
final class UUIDTypeTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function simpleDataProvider() : array | ||
{ | ||
return [ | ||
['A98C5A1E-A742-4808-96FA-6F409E799937'], | ||
]; | ||
} | ||
|
||
public function invalidDataProvider() : array | ||
{ | ||
return [ | ||
[''], | ||
['A98C5A1E-A742-4808-96FA-6F409E799937A'], | ||
['A98C5A1E-A742-4808-96FA-6F409E799937-'], | ||
[true], | ||
[420], | ||
[420.42], | ||
['beetlejuice'], | ||
[(object) []], | ||
[[]], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider simpleDataProvider | ||
* @param string $rawValue | ||
*/ | ||
public function testValidateValue(string $rawValue) : void | ||
{ | ||
$type = new \Graphpinator\ExtraTypes\UUIDType(); | ||
$value = $type->accept(new \Graphpinator\Resolver\CreateResolvedValueVisitor($rawValue)); | ||
|
||
self::assertSame($type, $value->getType()); | ||
self::assertSame($rawValue, $value->getRawValue()); | ||
} | ||
|
||
/** | ||
* @dataProvider invalidDataProvider | ||
* @param int|bool|string|float|array $rawValue | ||
*/ | ||
public function testValidateValueInvalid($rawValue) : void | ||
{ | ||
$this->expectException(\Graphpinator\Exception\Value\InvalidValue::class); | ||
|
||
$ipv4 = new \Graphpinator\ExtraTypes\UUIDType(); | ||
$ipv4->accept(new \Graphpinator\Resolver\CreateResolvedValueVisitor($rawValue)); | ||
} | ||
|
||
public function testSpecifiedBy() : void | ||
{ | ||
$type = new \Graphpinator\ExtraTypes\UUIDType(); | ||
|
||
self::assertSame('https://datatracker.ietf.org/doc/html/rfc4122', $type->getSpecifiedByUrl()); | ||
} | ||
} |