-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
230 additions
and
6 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
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,90 @@ | ||
<?php | ||
|
||
namespace Mapped\Extension; | ||
|
||
use Mapped\ExtensionInterface; | ||
use Mapped\Transformer\Callback; | ||
use Mapped\Mapping; | ||
|
||
/** | ||
* TransformTo. | ||
*/ | ||
class TransformTo implements ExtensionInterface | ||
{ | ||
/** | ||
* Transforms the data to given class via setter and getter methods. | ||
* | ||
* @param Mapping $mapping The mapping object | ||
* @param string|object $object The class name or an object | ||
* | ||
* @return Mapping | ||
*/ | ||
public function transformTo(Mapping $mapping, $object) | ||
{ | ||
$mapping->transform(new Callback(function ($data) use ($object, $mapping) { | ||
if (is_string($object)) { | ||
$object = new $object; | ||
} | ||
|
||
foreach ($mapping->getChildren() as $name => $child) { | ||
if (array_key_exists($name, $data) && null !== $data[$name]) { | ||
$this->setValue($object, $name, $data[$name]); | ||
} | ||
} | ||
|
||
return $object; | ||
}, function ($data) use ($object, $mapping) { | ||
if (!$data instanceof $object) { | ||
return; | ||
} | ||
|
||
$result = []; | ||
foreach ($mapping->getChildren() as $name => $child) { | ||
$result[$name] = $this->getValue($data, $name); | ||
} | ||
|
||
return $result; | ||
}, false)); | ||
|
||
return $mapping; | ||
} | ||
|
||
/** | ||
* Sets the given value into the object. | ||
* | ||
* @param object $object The object | ||
* @param string $name The property name | ||
* @param mixed $value The value | ||
*/ | ||
private function setValue($object, $name, $value) | ||
{ | ||
if (isset($object->$name)) { | ||
$object->$name = $value; | ||
} | ||
|
||
$setter = 'set' . ucfirst($name); | ||
if (method_exists($object, $setter)) { | ||
return $object->$setter($value); | ||
} | ||
} | ||
|
||
/** | ||
* Gets a value from given object. | ||
* | ||
* @param object $object The object | ||
* @param string $name The property name | ||
* | ||
* @return mixed | ||
*/ | ||
private function getValue($object, $name) | ||
{ | ||
if (isset($object->$name)) { | ||
return $object->$name; | ||
} | ||
|
||
$getter = 'get' . ucfirst($name); | ||
if (method_exists($object, $getter)) { | ||
return $object->$getter(); | ||
} | ||
} | ||
} |
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,94 @@ | ||
<?php | ||
|
||
namespace Mapped\Tests\Extension; | ||
|
||
use Mapped\Factory; | ||
use Mapped\Tests\Fixtures\User; | ||
use Mapped\Tests\Fixtures\Book; | ||
|
||
class TransformToTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testA() | ||
{ | ||
$factory = new Factory([new \Mapped\Extension\TransformTo]); | ||
|
||
$mapping = $factory->mapping([ | ||
'title' => $factory->string(), | ||
'author' => $factory->string(), | ||
])->transformTo('Mapped\Tests\Fixtures\Book'); | ||
|
||
$book = $mapping->apply([ | ||
'title' => 'LOTR', | ||
'author' => 'J.R.R. Tolkien', | ||
]); | ||
|
||
$this->assertInstanceOf('Mapped\Tests\Fixtures\Book', $book); | ||
$data = $mapping->unapply($book); | ||
|
||
$this->assertEquals([ | ||
'title' => 'LOTR', | ||
'author' => 'J.R.R. Tolkien', | ||
], $data); | ||
} | ||
|
||
public function testB() | ||
{ | ||
$factory = new Factory([new \Mapped\Extension\TransformTo]); | ||
|
||
$mapping = $factory->mapping([ | ||
'title' => $factory->string(), | ||
'author' => $factory->string()->optional(), | ||
])->transformTo('Mapped\Tests\Fixtures\Book'); | ||
|
||
$book = $mapping->apply([ | ||
'title' => 'LOTR', | ||
]); | ||
|
||
$this->assertSame('N/A', $book->getAuthor()); | ||
} | ||
|
||
public function testC() | ||
{ | ||
$factory = new Factory([new \Mapped\Extension\TransformTo]); | ||
|
||
$book = new Book; | ||
$mapping = $factory->mapping([ | ||
'title' => $factory->string(), | ||
'author' => $factory->string(), | ||
])->transformTo($book); | ||
|
||
$mapping->apply([ | ||
'title' => 'a', | ||
'author' => 'b', | ||
]); | ||
|
||
$this->assertSame('a', $book->getTitle()); | ||
$this->assertSame('b', $book->getAuthor()); | ||
} | ||
|
||
public function testD() | ||
{ | ||
$factory = new Factory([new \Mapped\Extension\TransformTo]); | ||
$user = new User('a', 'b'); | ||
|
||
$mapping = $factory->mapping([ | ||
'username' => $factory->string(), | ||
'password' => $factory->string(), | ||
])->transformTo($user); | ||
|
||
$user = $mapping->apply([ | ||
'username' => 'dennis', | ||
'password' => 'passwd', | ||
]); | ||
|
||
$this->assertSame('dennis', $user->username); | ||
$this->assertSame('passwd', $user->password); | ||
|
||
$data = $mapping->unapply($user); | ||
|
||
$this->assertEquals([ | ||
'username' => 'dennis', | ||
'password' => 'passwd', | ||
], $data); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace Mapped\Tests\Fixtures; | ||
|
||
class Book | ||
{ | ||
private $title; | ||
private $author; | ||
|
||
public function __construct() | ||
{ | ||
$this->author = 'N/A'; | ||
} | ||
|
||
public function setTitle($title) | ||
{ | ||
$this->title = $title; | ||
} | ||
|
||
public function getTitle() | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function setAuthor($author) | ||
{ | ||
$this->author = $author; | ||
} | ||
|
||
public function getAuthor() | ||
{ | ||
return $this->author; | ||
} | ||
} |