-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from LinioIT/feature/uuid-transformer
feature: provide a UUID transformer.
- Loading branch information
Showing
4 changed files
with
61 additions
and
1 deletion.
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Linio\Component\Input\Exception; | ||
|
||
class TransformationException extends \RuntimeException | ||
{ | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Linio\Component\Input\Transformer; | ||
|
||
use Exception; | ||
use Linio\Component\Input\Exception\TransformationException; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
class UuidTransformer implements TransformerInterface | ||
{ | ||
public function transform($value) | ||
{ | ||
try { | ||
return Uuid::fromString($value); | ||
} catch (Exception $exception) { | ||
throw new TransformationException($exception->getMessage()); | ||
} | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Linio\Component\Input\Transformer; | ||
|
||
use Linio\Component\Input\Exception\TransformationException; | ||
use PHPUnit\Framework\TestCase; | ||
use Ramsey\Uuid\Uuid; | ||
use Ramsey\Uuid\UuidInterface; | ||
|
||
class UuidTransformerTest extends TestCase | ||
{ | ||
public function testItDoesTransformStringIntoUuid() | ||
{ | ||
$transformer = new UuidTransformer(); | ||
$transformed = $transformer->transform('d1d6228d-604c-4a8a-9396-42e6c3b17754'); | ||
$this->assertInstanceOf(UuidInterface::class, $transformed); | ||
$this->assertEquals(Uuid::fromString('d1d6228d-604c-4a8a-9396-42e6c3b17754'), $transformed); | ||
} | ||
|
||
public function testItDoesThrowExceptionBecauseOfInvalidString() | ||
{ | ||
$transformer = new UuidTransformer(); | ||
|
||
$this->expectException(TransformationException::class); | ||
$transformer->transform('d1d6228d-604c'); | ||
} | ||
} |