-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/fliglio/web into 2.1.x
- Loading branch information
Showing
11 changed files
with
286 additions
and
27 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,29 @@ | ||
<?php | ||
|
||
namespace Fliglio\Web; | ||
|
||
class CollectionApiMapper implements ApiMapper { | ||
|
||
private $mapper; | ||
|
||
public function __construct(ApiMapper $mapper) { | ||
$this->mapper = $mapper; | ||
} | ||
|
||
public function marshal($entities) { | ||
$vos = []; | ||
|
||
foreach ($entities as $key => $entity) { | ||
$vos[$key] = $this->mapper->marshal($entity); | ||
} | ||
return $vos; | ||
} | ||
public function unmarshal($serialized) { | ||
$entities = []; | ||
|
||
foreach ($serialized as $key => $vo) { | ||
$entities[$key] = $this->mapper->unmarshal($vo); | ||
} | ||
return $entities; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace Fliglio\Web; | ||
|
||
use Fliglio\Http\Exceptions\BadRequestException; | ||
|
||
class Entity { | ||
private $body; | ||
|
||
public function __construct($body, $contentType) { | ||
$this->body = $body; | ||
$this->contentType = $contentType; | ||
} | ||
public function get() { | ||
return $this->body; | ||
} | ||
public function bind($entityType) { | ||
if (!in_array('Fliglio\Web\MappableApi', class_implements($entityType))) { | ||
throw new \Exception($entityType . " doesn't implement Fliglio\Web\MappableApi"); | ||
} | ||
|
||
$arr = null; | ||
switch($this->contentType) { | ||
|
||
// assume json | ||
case 'application/json': | ||
default: | ||
$arr = json_decode($this->body, true); | ||
} | ||
|
||
$entity = $entityType::unmarshal($arr); | ||
|
||
if ($entity instanceof Validation) { | ||
try { | ||
$entity->validate(); | ||
} catch (ValidationException $e) { | ||
throw new BadRequestException($e->getMessage()); | ||
} | ||
} | ||
return $entity; | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Fliglio\Web; | ||
|
||
interface MappableApi { | ||
public function marshal(); | ||
public static function unmarshal($serialized); | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace Fliglio\Web; | ||
|
||
trait MappableApiTrait { | ||
|
||
public function marshal() { | ||
return self::getApiMapper()->marshal($this); | ||
} | ||
public static function unmarshal($valueObject) { | ||
return self::getApiMapper()->unmarshal($valueObject); | ||
} | ||
|
||
public static function getClass() { | ||
return get_called_class(); | ||
} | ||
public static function getApiMapper() { | ||
$className = self::getClass(); | ||
$mapperClassName = $className . 'ApiMapper'; | ||
return new $mapperClassName(); | ||
} | ||
} |
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,70 @@ | ||
<?php | ||
namespace Fliglio\Web; | ||
|
||
use Fliglio\Http\Http; | ||
use Doctrine\Common\Annotations\AnnotationRegistry; | ||
|
||
class ApiMapperTest extends \PHPUnit_Framework_TestCase { | ||
|
||
public function testApiMapper() { | ||
|
||
// given | ||
$mapper = new FooApiMapper(); | ||
|
||
$entity = new Foo("foo"); | ||
$vo = ["myProp" => "foo"]; | ||
|
||
// when | ||
$foundVo = $mapper->marshal($entity); | ||
$foundEntity = $mapper->unmarshal($vo); | ||
|
||
// then | ||
$this->assertEquals($entity, $foundEntity); | ||
$this->assertEquals($vo, $foundVo); | ||
} | ||
|
||
public function testStaticApiMapper() { | ||
|
||
// given | ||
$entity = new Foo("foo"); | ||
$vo = ["myProp" => "foo"]; | ||
|
||
// when | ||
$foundVo = $entity->marshal(); | ||
$foundEntity = Foo::unmarshal($vo); | ||
|
||
// then | ||
$this->assertEquals($entity, $foundEntity); | ||
$this->assertEquals($vo, $foundVo); | ||
} | ||
public function testStaticApiMapperWithoutNamingConvention() { | ||
|
||
// given | ||
$entity = new Bar("bar", new Foo("foo"), [new Foo("baz"), new Foo("biz")]); | ||
|
||
// when | ||
$vo = $entity->marshal(); | ||
$foundEntity = Bar::unmarshal($vo); | ||
|
||
// then | ||
$this->assertEquals($entity, $foundEntity); | ||
} | ||
|
||
public function testCollectionApiMapper() { | ||
|
||
// given | ||
$mapper = new CollectionApiMapper(new FooApiMapper()); | ||
|
||
|
||
$entities = [new Foo("foo"), new Foo("bar")]; | ||
$vo = [["myProp" => "foo"], ["myProp" => "bar"]]; | ||
|
||
// when | ||
$foundVo = $mapper->marshal($entities); | ||
$foundEntities = $mapper->unmarshal($vo); | ||
|
||
// then | ||
$this->assertEquals($entities, $foundEntities); | ||
$this->assertEquals($vo, $foundVo); | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace Fliglio\Web; | ||
|
||
class BarApiMapper implements ApiMapper { | ||
|
||
public function marshal($bar) { | ||
$fooMapper = $bar->getFoo()->getApiMapper(); | ||
return [ | ||
'name' => $bar->getName(), | ||
'foo' => $fooMapper->marshal($bar->getFoo()), | ||
'foos' => (new CollectionApiMapper($fooMapper))->marshal($bar->getFoos()), | ||
]; | ||
} | ||
|
||
public function unmarshal($fooArr) { | ||
$fooMapper = new FooApiMapper(); | ||
return new Bar( | ||
$fooArr['name'], | ||
$fooMapper->unmarshal($fooArr['foo']), | ||
(new CollectionApiMapper($fooMapper))->unmarshal($fooArr['foos']) | ||
); | ||
} | ||
} |
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
Oops, something went wrong.