Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added TransformerInterface

  • Loading branch information...
commit 513f3d53967e9f27da874a55916f0e0d634498c7 1 parent 01dc06d
Geoffrey Bachelet authored
7 README.md
View
@@ -38,7 +38,10 @@ $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'
@@ -46,6 +49,8 @@ echo $book->title; // 'Fondation'
// 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
7 src/Maple/Mapper.php
View
@@ -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);
}
}
2  src/Maple/Transformer/Alias.php
View
@@ -14,7 +14,7 @@
/**
* @package Maple
*/
-class Alias
+class Alias implements TransformerInterface
{
/**
* @var string
2  src/Maple/Transformer/Coalesce.php
View
@@ -14,7 +14,7 @@
/**
* @package Maple
*/
-class Coalesce
+class Coalesce implements TransformerInterface
{
/**
* @var array
2  src/Maple/Transformer/Group.php
View
@@ -16,7 +16,7 @@
/**
* @package Maple
*/
-class Group
+class Group implements TransformerInterface
{
/**
* @var string
2  src/Maple/Transformer/Pipe.php
View
@@ -14,7 +14,7 @@
/**
* @package Maple
*/
-class Pipe
+class Pipe implements TransformerInterface
{
/**
* @var array
5 src/Maple/Transformer/Relation.php
View
@@ -6,8 +6,9 @@
use Maple\Transformer\Coalesce;
use Maple\Transformer\Alias;
use Maple\Transformer\Group;
+use Maple\Mapper;
-class Relation
+class Relation implements TransformerInterface
{
private $name;
@@ -15,7 +16,7 @@ class Relation
private $mapper;
- public function __construct($name, $prefix, $mapper)
+ public function __construct($name, $prefix, Mapper $mapper)
{
$this->name = $name;
$this->prefix = $prefix;
30 src/Maple/Transformer/TransformerInterface.php
View
@@ -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);
+}
4 src/Tests/Maple/MapperTest.php
View
@@ -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()
Please sign in to comment.
Something went wrong with that request. Please try again.