Skip to content

Commit

Permalink
Merge pull request #35 from LinioIT/feature/uuid-transformer
Browse files Browse the repository at this point in the history
feature: provide a UUID transformer.
  • Loading branch information
Klaus Silveira authored Aug 31, 2018
2 parents c3d3598 + b530839 commit b713e5d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"license": "BSD-3-Clause",
"require": {
"php": ">=7.0",
"doctrine/inflector": "^1.0"
"doctrine/inflector": "^1.0",
"ramsey/uuid": "^3.8"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/TransformationException.php
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
{
}
21 changes: 21 additions & 0 deletions src/Transformer/UuidTransformer.php
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());
}
}
}
29 changes: 29 additions & 0 deletions tests/Transformer/UuidTransformerTest.php
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');
}
}

0 comments on commit b713e5d

Please sign in to comment.