Skip to content

Commit

Permalink
Merge branch 'feature/transform'
Browse files Browse the repository at this point in the history
  • Loading branch information
cambell-prince committed Jun 29, 2021
2 parents 3a5b9e9 + 218a83b commit 864e10a
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Anorm: Another ORM for PHP

[![Build Status](https://travis-ci.org/saygoweb/anorm.svg?branch=master)](https://travis-ci.org/saygoweb/anorm)
[![Build Status](https://travis-ci.com/saygoweb/anorm.svg?branch=master)](https://travis-ci.com/saygoweb/anorm)
[![Coverage Status](https://coveralls.io/repos/github/saygoweb/anorm/badge.svg?branch=master)](https://coveralls.io/github/saygoweb/anorm?branch=master)
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)

Expand Down
2 changes: 1 addition & 1 deletion src/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class DataMapper
/** @var string Name of the table */
public $table;

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

public static function create(\PDO $pdo, $table, $map)
Expand Down
6 changes: 4 additions & 2 deletions src/Transformer.php → src/Transform/FunctionTransform.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace Anorm;
namespace Anorm\Transform;

class Transformer
use Anorm\TransformInterface;

class FunctionTransform implements TransformInterface
{

/** @var callable */
Expand Down
19 changes: 19 additions & 0 deletions src/Transform/JsonArrayTransform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Anorm\Transform;

use Anorm\TransformInterface;

class JsonArrayTransform implements TransformInterface
{

public function txDatabaseToModel($value)
{
return \json_decode($value, true);
}

public function txModelToDatabase($value)
{
return \json_encode($value);
}

}
27 changes: 27 additions & 0 deletions src/Transform/SqlDateTimeTransform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Anorm\Transform;

use Anorm\TransformInterface;
use DateTime;

class SqlDateTimeTransform implements TransformInterface
{
/** @var string */
private $format;

public function __construct($format = 'Y-m-d H:i:s')
{
$this->format = $format;
}

public function txDatabaseToModel($value)
{
return new \DateTime($value);
}

public function txModelToDatabase(/** @var \DateTime */$value)
{
return $value->format($this->format);
}

}
11 changes: 11 additions & 0 deletions src/TransformInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Anorm;

interface TransformInterface
{

public function txDatabaseToModel($value);

public function txModelToDatabase($value);

}
4 changes: 2 additions & 2 deletions test/anorm/ReplaceTableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Anorm\Anorm;
use Anorm\DataMapper;
use Anorm\Model;
use Anorm\Transformer;
use Anorm\Transform\FunctionTransform;

class ReplaceTableModel extends Model {
public function __construct()
Expand All @@ -13,7 +13,7 @@ public function __construct()
parent::__construct($pdo, DataMapper::createByClass($pdo, $this));
$this->_mapper->modelPrimaryKey = 'replaceId';
$this->_mapper->useReplace = true;
$this->_mapper->transformers['name'] = new Transformer(
$this->_mapper->transformers['name'] = new FunctionTransform(
function($value) { return strtolower($value); },
function($value) { return strtoupper($value); }
);
Expand Down
4 changes: 2 additions & 2 deletions test/anorm/SomeTableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
use Anorm\Anorm;
use Anorm\DataMapper;
use Anorm\Model;
use Anorm\Transformer;
use Anorm\Transform\FunctionTransform;

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(
$this->_mapper->transformers['name'] = new FunctionTransform(
function($value) { return strtolower($value); },
function($value) { return strtoupper($value); }
);
Expand Down
28 changes: 26 additions & 2 deletions test/anorm/Transformer_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

use Anorm\Transformer;
use Anorm\Test\TestEnvironment;
use Anorm\Transform\FunctionTransform;
use Anorm\Transform\JsonArrayTransform;
use Anorm\Transform\SqlDateTimeTransform;

class TransformerTest extends TestCase
{
public function testNullTransform_Ok()
public function testNullFunctionTransform_Ok()
{
$o = new Transformer(
$o = new FunctionTransform(
function($value) { return $value; },
function($value) { return $value; }
);
Expand All @@ -21,4 +24,25 @@ function($value) { return $value; }
$this->assertEquals('test', $result);
}

public function testJsonArrayTransform_Ok()
{
$t = new JsonArrayTransform();
$result1 = $t->txDatabaseToModel('{ "key": "value" }');
$this->assertEquals(['key' => 'value'], $result1);
$result2 = $t->txModelToDatabase($result1);
$this->assertEquals('{"key":"value"}', $result2);
}

public function testSqlDateTimeTransform_Ok()
{
$t = new SqlDateTimeTransform();
$testString = '2021-06-29 16:10:11';
$result1 = $t->txDatabaseToModel($testString);
$this->assertInstanceOf('DateTime', $result1);
$this->assertEquals(new DateTime($testString), $result1);
$result2 = $t->txModelToDatabase($result1);
$this->assertEquals($testString, $result2);
}


}

0 comments on commit 864e10a

Please sign in to comment.