Skip to content

Commit

Permalink
feature/transformer: Add transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
cambell-prince committed Aug 13, 2020
1 parent 10d8236 commit db6bbe4
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 7 deletions.
21 changes: 18 additions & 3 deletions src/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class DataMapper
/** @var string Name of the table */
public $table;

/** @var array */
public $transformers = [];

public static function create(\PDO $pdo, $table, $map)
{
$mapper = new DataMapper($pdo, $table, $map);
Expand Down Expand Up @@ -127,7 +130,11 @@ public function write(&$c)
if ($c->$property === null) {
$value = 'NULL';
} else {
$value = $this->pdo->quote($c->$property);
if (array_key_exists($field, $this->transformers)) {
$value = $this->pdo->quote($this->transformers[$field]->txModelToDatabase($c->$property));
} else {
$value = $this->pdo->quote($c->$property);
}
}
$values .= $value;
}
Expand All @@ -151,7 +158,11 @@ public function write(&$c)
if ($c->$property === null) {
$value = 'NULL';
} else {
$value = $this->pdo->quote($c->$property);
if (array_key_exists($field, $this->transformers)) {
$value = $this->pdo->quote($this->transformers[$field]->txModelToDatabase($c->$property));
} else {
$value = $this->pdo->quote($c->$property);
}
}
// TODO Move this to bound value CP 2020-06
$set .= "$field=$value";
Expand Down Expand Up @@ -241,7 +252,11 @@ public function readArray(&$c, $data, $exclude = array())
continue;
}
if (!in_array($property, $exclude) && array_key_exists($field, $data)) {
$c->$property = $data[$field];
if (array_key_exists($field, $this->transformers)) {
$c->$property = $this->transformers[$field]->txDatabaseToModel($data[$field]);
} else {
$c->$property = $data[$field];
}
}
}
return true;
Expand Down
29 changes: 29 additions & 0 deletions src/Transformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Anorm;

class Transformer
{

/** @var callable */
private $databaseToModel;

/** @var callable */
private $modelToDatabase;

public function __construct(callable $databaseToModel, callable $modelToDatabase)
{
$this->databaseToModel = $databaseToModel;
$this->modelToDatabase = $modelToDatabase;
}

public function txDatabaseToModel($value)
{
return ($this->databaseToModel)($value);
}

public function txModelToDatabase($value)
{
return ($this->modelToDatabase)($value);
}

}
1 change: 0 additions & 1 deletion test/anorm/Anorm_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function testPdo_NotConnected_Fails()

/**
* @expectedException \PDOException
* @expectedExceptionMessage SQLSTATE[HY000] [1045] Access denied for user 'bogus'@'localhost' (using password: NO)
*/
public function testConnction_Bogus_Fails()
{
Expand Down
6 changes: 3 additions & 3 deletions test/anorm/DataMapper_Find_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testFindOne_OK()
$model = DataMapper::find(SomeTableModel::class, $this->pdo)
->where("`name`=:name", [':name' => 'Name 1'])
->one();
$this->assertEquals('Name 1', $model->name);
$this->assertEquals('name 1', $model->name);
}

public function testFindOneOrThrow_OK()
Expand All @@ -59,7 +59,7 @@ public function testFindOneOrThrow_OK()
$model = DataMapper::find(SomeTableModel::class, $this->pdo)
->where("`name`=:name", [':name' => 'Name 1'])
->oneOrThrow();
$this->assertEquals('Name 1', $model->name);
$this->assertEquals('name 1', $model->name);
}

public function testFindSome_OK()
Expand All @@ -70,7 +70,7 @@ public function testFindSome_OK()
->some();
$i = 0;
foreach ($generator as $model) {
$this->assertEquals("Name $i", $model->name);
$this->assertEquals("name $i", $model->name);
++$i;
}
$this->assertEquals(3, $i);
Expand Down
5 changes: 5 additions & 0 deletions test/anorm/SomeTableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
use Anorm\Anorm;
use Anorm\DataMapper;
use Anorm\Model;
use Anorm\Transformer;

class SomeTableModel extends Model {
public function __construct()
{
$pdo = Anorm::pdo();
parent::__construct($pdo, DataMapper::createByClass($pdo, $this));
$this->_mapper->modelPrimaryKey = 'someId';
$this->_mapper->transformers['name'] = new Transformer(
function($value) { return strtolower($value); },
function($value) { return strtoupper($value); }
);
$this->dtc = null;
}

Expand Down
24 changes: 24 additions & 0 deletions test/anorm/Transformer_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

require_once(__DIR__ . '/../../vendor/autoload.php');

use PHPUnit\Framework\TestCase;

use Anorm\Transformer;
use Anorm\Test\TestEnvironment;

class TransformerTest extends TestCase
{
public function testNullTransform_Ok()
{
$o = new Transformer(
function($value) { return $value; },
function($value) { return $value; }
);
$result = $o->txDatabaseToModel('test');
$this->assertEquals('test', $result);
$result = $o->txModelToDatabase('test');
$this->assertEquals('test', $result);
}

}

0 comments on commit db6bbe4

Please sign in to comment.