Permalink
Browse files

added TransformerInterface

  • Loading branch information...
1 parent 01dc06d commit 513f3d53967e9f27da874a55916f0e0d634498c7 @ubermuda committed Jul 30, 2012
View
@@ -38,14 +38,19 @@ $data = ['title' => 'Fondation'];
// That instance is just a prototype and will be cloned for each mapping so your $mapper
// is actually re-usable ad nauseam
$mapper = new Maple\Mapper(new Model\Book());
-$book = $mapper->map($data);
+
+// Mappers actually implement the TransformerInterface interface
+// So they can freely be used wherever a Transformer is expected
+$book = $mapper->transform($data);
echo $book->title; // 'Fondation'
// Has inflection by default
// Maple comes with a simple underscore to camelCase inflector
$data = ['published_at' => 1957];
$mapper = new Maple\Mapper(new Model\Book());
+
+// All built-in transformers implement the __invoke magic method
$book = $mapper($data);
echo $book->publishedAt; // 1957
@@ -15,11 +15,12 @@
use Traversable;
use Maple\Inflector\Inflector;
+use Maple\Transformer\TransformerInterface;
/**
* @package Maple
*/
-class Mapper
+class Mapper implements TransformerInterface
{
/**
* @var object
@@ -64,7 +65,7 @@ public function setInflector(callable $inflector = null)
* @param array|Traversable $data
* @return object
*/
- public function map($data)
+ public function transform($data)
{
$class = get_class($this->prototype);
@@ -108,6 +109,6 @@ public function map($data)
*/
public function __invoke($data)
{
- return $this->map($data);
+ return $this->transform($data);
}
}
@@ -14,7 +14,7 @@
/**
* @package Maple
*/
-class Alias
+class Alias implements TransformerInterface
{
/**
* @var string
@@ -14,7 +14,7 @@
/**
* @package Maple
*/
-class Coalesce
+class Coalesce implements TransformerInterface
{
/**
* @var array
@@ -16,7 +16,7 @@
/**
* @package Maple
*/
-class Group
+class Group implements TransformerInterface
{
/**
* @var string
@@ -14,7 +14,7 @@
/**
* @package Maple
*/
-class Pipe
+class Pipe implements TransformerInterface
{
/**
* @var array
@@ -6,16 +6,17 @@
use Maple\Transformer\Coalesce;
use Maple\Transformer\Alias;
use Maple\Transformer\Group;
+use Maple\Mapper;
-class Relation
+class Relation implements TransformerInterface
{
private $name;
private $prefix;
private $mapper;
- public function __construct($name, $prefix, $mapper)
+ public function __construct($name, $prefix, Mapper $mapper)
{
$this->name = $name;
$this->prefix = $prefix;
@@ -0,0 +1,30 @@
+<?php
+
+/**
+ * This file is part of the Maple package
+ *
+ * (c) Geoffrey Bachelet <geoffrey.bachelet@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Maple\Transformer;
+
+/**
+ * @package Maple
+ */
+interface TransformerInterface
+{
+ /**
+ * @param array $data
+ * @return mixed
+ */
+ public function transform($data);
+
+ /**
+ * @param array $data
+ * @return mixed
+ */
+ public function __invoke($data);
+}
@@ -65,7 +65,7 @@ public function testMapToProperties()
$book->title = $data['title'];
$book->excerpt = $data['excerpt'];
- $this->assertEquals($book, $mapper->map($data));
+ $this->assertEquals($book, $mapper->transform($data));
}
public function testMapToSetter()
@@ -147,7 +147,7 @@ public function testUsesGroupTransformer()
'author' => new Group('author__'),
]);
- $this->assertEquals($book, $mapper->map($data));
+ $this->assertEquals($book, $mapper->transform($data));
}
public function testAcceptsTraversableObject()

0 comments on commit 513f3d5

Please sign in to comment.