Skip to content

Commit

Permalink
Merge pull request #2 from m1x0n/feature/money
Browse files Browse the repository at this point in the history
Add Money support
  • Loading branch information
Barry O Sullivan authored Apr 4, 2017
2 parents 90d052c + 301f36a commit d5a3502
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 30 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require": {
"php": ">=5.5.9",
"ramsey/uuid": "~2.8",
"respect/validation": "1.0.3"
"respect/validation": "1.0.3",
"moneyphp/money": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "5.2.1"
Expand Down
135 changes: 106 additions & 29 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Deserializer/Deserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ class Deserializer implements Contracts\Deserializer
private $composite;
private $set;
private $type_entity;
private $money;

public function __construct(Reflector $reflector)
{
$this->single_value = new Deserializer\SingleValue();
$this->composite = new Deserializer\Composite($this, $reflector);
$this->set = new Deserializer\Set($this);
$this->type_entity = new Deserializer\TypeEntity($this, $reflector);
$this->money = new Deserializer\Money();
}

public function deserialize($class, $parameters)
Expand All @@ -38,6 +40,9 @@ private function deserializer_repo_fetch($class)
if ($this->is_instance_of($class, Type\AbstractSet::class)) {
return $this->set;
}
if ($this->is_instance_of($class, \Money\Money::class)) {
return $this->money;
}

throw new \Exception("No deserializer found for class ".$class);
}
Expand Down
24 changes: 24 additions & 0 deletions src/Deserializer/Deserializer/Money.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace EventSourced\ValueObject\Deserializer\Deserializer;

use EventSourced\ValueObject\Deserializer\Exception;

class Money
{
public function deserialize($class, $serialized)
{
if (is_array($serialized)) {
$serialized = (object)$serialized;
}

try {
return new \Money\Money(
$serialized->amount,
new \Money\Currency($serialized->currency)
);
} catch (\Exception $e) {
throw new Exception($e->getMessage());
}
}
}
Loading

0 comments on commit d5a3502

Please sign in to comment.