Skip to content

Commit

Permalink
renamed hydrator and normalizer container, updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderer committed Feb 12, 2016
1 parent b85ec40 commit 92445df
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 44 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,22 @@ Serialization is controlled by registering callables used in normalization phase
```php
use Thunder\Serializard\Format\JsonFormat;
use Thunder\Serializard\FormatContainer\FormatContainer;
use Thunder\Serializard\HandlerContainer\HandlerContainer;
use Thunder\Serializard\HydratorContainer\FallbackHydratorContainer;
use Thunder\Serializard\NormalizerContainer\FallbackNormalizerContainer;
use Thunder\Serializard\Serializard;

$formats = new FormatContainer();
$formats->add('json', new JsonFormat());

$normalizers = new HandlerContainer();
$normalizers = new FallbackNormalizerContainer();
$normalizers->add(User::class, 'user', function(User $user) {
return [
'id' => $user->getId(),
'name' => $user->getName(),
];
});

$hydrators = new HandlerContainer();
$hydrators = new FallbackHydratorContainer();

$serializard = new Serializard($formats, $normalizers, $hydrators);
echo $serializard->serialize(new User(1, 'Thomas'), 'json');
Expand All @@ -76,15 +77,16 @@ Unserialization can be controlled by registering callables able to reconstruct o
```php
use Thunder\Serializard\Format\JsonFormat;
use Thunder\Serializard\FormatContainer\FormatContainer;
use Thunder\Serializard\HandlerContainer\HandlerContainer;
use Thunder\Serializard\HydratorContainer\FallbackHydratorContainer;
use Thunder\Serializard\NormalizerContainer\FallbackNormalizerContainer;
use Thunder\Serializard\Serializard;

$formats = new FormatContainer();
$formats->add('json', new JsonFormat());

$normalizers = new HandlerContainer();
$normalizers = new FallbackNormalizerContainer();

$hydrators = new HandlerContainer();
$hydrators = new FallbackHydratorContainer();
$hydrators->add(User::class, 'user', function(array $data) {
return new User($data['id'], $data['name']);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class HydratorContainer implements HydratorContainerInterface
final class FallbackHydratorContainer implements HydratorContainerInterface
{
private $handlers = array();
private $interfaces = array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class NormalizerContainer implements NormalizerContainerInterface
final class FallbackNormalizerContainer implements NormalizerContainerInterface
{
private $handlers = array();
private $interfaces = array();
Expand Down
12 changes: 6 additions & 6 deletions src/SerializardFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
use Thunder\Serializard\Format\XmlFormat;
use Thunder\Serializard\Format\YamlFormat;
use Thunder\Serializard\FormatContainer\FormatContainer;
use Thunder\Serializard\HydratorContainer\HydratorContainer;
use Thunder\Serializard\NormalizerContainer\NormalizerContainer;
use Thunder\Serializard\HydratorContainer\FallbackHydratorContainer;
use Thunder\Serializard\NormalizerContainer\FallbackNormalizerContainer;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class SerializardFacade
{
/** @var NormalizerContainer */
/** @var FallbackNormalizerContainer */
private $normalizers;
/** @var HydratorContainer */
/** @var FallbackHydratorContainer */
private $hydrators;
/** @var FormatContainer */
private $formats;

public function __construct()
{
$this->normalizers = new NormalizerContainer();
$this->hydrators = new HydratorContainer();
$this->normalizers = new FallbackNormalizerContainer();
$this->hydrators = new FallbackHydratorContainer();

$formats = new FormatContainer();
$formats->add('json', new JsonFormat());
Expand Down
8 changes: 4 additions & 4 deletions tests/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
namespace Thunder\Serializard\Tests;

use Thunder\Serializard\Format\ArrayFormat;
use Thunder\Serializard\HydratorContainer\HydratorContainer;
use Thunder\Serializard\NormalizerContainer\NormalizerContainer;
use Thunder\Serializard\HydratorContainer\FallbackHydratorContainer;
use Thunder\Serializard\NormalizerContainer\FallbackNormalizerContainer;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
Expand All @@ -14,13 +14,13 @@ public function testArrayUnserializeInvalidTypeException()
{
$format = new ArrayFormat();
$this->setExpectedException('RuntimeException');
$format->unserialize(new \stdClass(), 'stdClass', new HydratorContainer());
$format->unserialize(new \stdClass(), 'stdClass', new FallbackHydratorContainer());
}

public function testMissingUnserializationHandlerException()
{
$format = new ArrayFormat();
$this->setExpectedException('RuntimeException');
$format->unserialize(array(), 'stdClass', new HydratorContainer());
$format->unserialize(array(), 'stdClass', new FallbackHydratorContainer());
}
}
16 changes: 8 additions & 8 deletions tests/HydratorContainerTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Thunder\Serializard\Tests;

use Thunder\Serializard\HydratorContainer\HydratorContainer;
use Thunder\Serializard\HydratorContainer\FallbackHydratorContainer;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
Expand All @@ -10,7 +10,7 @@ class HydratorContainerTest extends \PHPUnit_Framework_TestCase
{
public function testAlias()
{
$handlers = new HydratorContainer();
$handlers = new FallbackHydratorContainer();
$handlers->add('stdClass', 'std', function() { return 'value'; });
$handlers->addAlias('DateTime', 'stdClass');

Expand All @@ -20,7 +20,7 @@ public function testAlias()

public function testInterface()
{
$hydrators = new HydratorContainer();
$hydrators = new FallbackHydratorContainer();
$interfaceName = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface';
$interfaceTypeA = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeA';
$interfaceTypeB = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeB';
Expand All @@ -32,7 +32,7 @@ public function testInterface()

public function testInheritance()
{
$hydrators = new HydratorContainer();
$hydrators = new FallbackHydratorContainer();
$ancestorName = 'Thunder\Serializard\Tests\Fake\FakeUserParentParent';
$parentName = 'Thunder\Serializard\Tests\Fake\FakeUserParent';
$userName = 'Thunder\Serializard\Tests\Fake\FakeUser';
Expand All @@ -49,7 +49,7 @@ public function testMultipleInterfacesException()
$typeAnother = 'Thunder\Serializard\Tests\Fake\Interfaces\AnotherTypeInterface';
$typeMultiple = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeMultiple';

$hydrators = new HydratorContainer();
$hydrators = new FallbackHydratorContainer();
$hydrators->add($typeInterface, 'type', function() { return 'multiple'; });
$hydrators->add($typeAnother, 'type', function() { return 'multiple'; });

Expand All @@ -59,21 +59,21 @@ public function testMultipleInterfacesException()

public function testInvalidClassOrInterfaceName()
{
$handlers = new HydratorContainer();
$handlers = new FallbackHydratorContainer();
$this->setExpectedException('RuntimeException');
$handlers->add('invalid', 'root', function() {});
}

public function testAliasForInvalidClass()
{
$handlers = new HydratorContainer();
$handlers = new FallbackHydratorContainer();
$this->setExpectedException('RuntimeException');
$handlers->addAlias('stdClass', 'DateTime');
}

public function testInvalidHandler()
{
$handlers = new HydratorContainer();
$handlers = new FallbackHydratorContainer();
$this->setExpectedException('RuntimeException');
$handlers->add('stdClass', 'name', 'invalid');
}
Expand Down
16 changes: 8 additions & 8 deletions tests/NormalizerContainerTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Thunder\Serializard\Tests;

use Thunder\Serializard\NormalizerContainer\NormalizerContainer;
use Thunder\Serializard\NormalizerContainer\FallbackNormalizerContainer;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
Expand All @@ -10,7 +10,7 @@ class NormalizerContainerTest extends \PHPUnit_Framework_TestCase
{
public function testAlias()
{
$handlers = new NormalizerContainer();
$handlers = new FallbackNormalizerContainer();
$handlers->add('stdClass', 'std', function() { return 'value'; });
$handlers->addAlias('DateTime', 'stdClass');

Expand All @@ -20,7 +20,7 @@ public function testAlias()

public function testInterface()
{
$hydrators = new NormalizerContainer();
$hydrators = new FallbackNormalizerContainer();
$interfaceName = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface';
$interfaceTypeA = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeA';
$interfaceTypeB = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeB';
Expand All @@ -32,7 +32,7 @@ public function testInterface()

public function testInheritance()
{
$hydrators = new NormalizerContainer();
$hydrators = new FallbackNormalizerContainer();
$ancestorName = 'Thunder\Serializard\Tests\Fake\FakeUserParentParent';
$parentName = 'Thunder\Serializard\Tests\Fake\FakeUserParent';
$userName = 'Thunder\Serializard\Tests\Fake\FakeUser';
Expand All @@ -49,7 +49,7 @@ public function testMultipleInterfacesException()
$typeAnother = 'Thunder\Serializard\Tests\Fake\Interfaces\AnotherTypeInterface';
$typeMultiple = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeMultiple';

$hydrators = new NormalizerContainer();
$hydrators = new FallbackNormalizerContainer();
$hydrators->add($typeInterface, 'type', function() { return 'multiple'; });
$hydrators->add($typeAnother, 'type', function() { return 'multiple'; });

Expand All @@ -59,21 +59,21 @@ public function testMultipleInterfacesException()

public function testInvalidClassOrInterfaceName()
{
$handlers = new NormalizerContainer();
$handlers = new FallbackNormalizerContainer();
$this->setExpectedException('RuntimeException');
$handlers->add('invalid', 'root', function() {});
}

public function testAliasForInvalidClass()
{
$handlers = new NormalizerContainer();
$handlers = new FallbackNormalizerContainer();
$this->setExpectedException('RuntimeException');
$handlers->addAlias('stdClass', 'DateTime');
}

public function testInvalidHandler()
{
$handlers = new NormalizerContainer();
$handlers = new FallbackNormalizerContainer();
$this->setExpectedException('RuntimeException');
$handlers->add('stdClass', 'name', 'invalid');
}
Expand Down
20 changes: 10 additions & 10 deletions tests/SerializardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use Thunder\Serializard\Format\XmlFormat;
use Thunder\Serializard\Format\YamlFormat;
use Thunder\Serializard\FormatContainer\FormatContainer;
use Thunder\Serializard\HydratorContainer\HydratorContainer;
use Thunder\Serializard\HydratorContainer\FallbackHydratorContainer;
use Thunder\Serializard\HydratorContainer\HydratorContainerInterface as Hydrators;
use Thunder\Serializard\Normalizer\ReflectionNormalizer;
use Thunder\Serializard\NormalizerContainer\NormalizerContainer;
use Thunder\Serializard\NormalizerContainer\FallbackNormalizerContainer;
use Thunder\Serializard\Serializard;
use Thunder\Serializard\Tests\Fake\FakeTag;
use Thunder\Serializard\Tests\Fake\FakeUser;
Expand Down Expand Up @@ -73,15 +73,15 @@ public function provideExamples()
public function testInterfaces()
{
$interface = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface';
$normalizers = new NormalizerContainer();
$normalizers = new FallbackNormalizerContainer();
$normalizers->add($interface, 'type', function(TypeInterface $type) {
return array(
'type' => $type->getType(),
'value' => $type->getValue(),
);
});

$hydrators = new HydratorContainer();
$hydrators = new FallbackHydratorContainer();

$formats = new FormatContainer();
$formats->add('array', new ArrayFormat());
Expand All @@ -98,11 +98,11 @@ public function testCycleException($var, $format)
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser';
$tagClass = 'Thunder\Serializard\Tests\Fake\FakeTag';

$normalizers = new NormalizerContainer();
$normalizers = new FallbackNormalizerContainer();
$normalizers->add($userClass, 'user', new ReflectionNormalizer());
$normalizers->add($tagClass, 'tag', new ReflectionNormalizer());

$hydrators = new HydratorContainer();
$hydrators = new FallbackHydratorContainer();

$formats = new FormatContainer();
$formats->add('xml', new XmlFormat());
Expand Down Expand Up @@ -136,7 +136,7 @@ private function getSerializard()
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser';
$tagClass = 'Thunder\Serializard\Tests\Fake\FakeTag';

$normalizers = new NormalizerContainer();
$normalizers = new FallbackNormalizerContainer();
$normalizers->add($userClass, 'user', new ReflectionNormalizer());
$normalizers->add($tagClass, 'tag', function(FakeTag $tag) {
return array(
Expand All @@ -145,7 +145,7 @@ private function getSerializard()
);
});

$hydrators = new HydratorContainer();
$hydrators = new FallbackHydratorContainer();
$hydrators->add($userClass, 'user', function(array $data, Hydrators $handlers) use($tagClass) {
$tagHandler = $handlers->getHandler($tagClass);

Expand Down Expand Up @@ -176,8 +176,8 @@ public function testParent()

$formats = new FormatContainer();
$formats->add('array', new ArrayFormat());
$normalizers = new NormalizerContainer();
$hydrators = new HydratorContainer();
$normalizers = new FallbackNormalizerContainer();
$hydrators = new FallbackHydratorContainer();
$serializard = new Serializard($formats, $normalizers, $hydrators);

$normalizers->add($userClass.'ParentParent', 'user', function(FakeUserParentParent $user) { return 'ancestor'; });
Expand Down

0 comments on commit 92445df

Please sign in to comment.