Skip to content

Commit

Permalink
Merge pull request #3 from byjg/2.0.1
Browse files Browse the repository at this point in the history
Added "preserveCase" option to Mapper for avoid name conflict.
  • Loading branch information
byjg committed Nov 21, 2017
2 parents 4e3a775 + 20f341e commit 597293f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
40 changes: 35 additions & 5 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Mapper
private $keygenFunction = null;
private $fieldMap = [];
private $fieldAlias = [];
private $preserveCasename = false;

/**
* Mapper constructor.
Expand All @@ -29,19 +30,38 @@ class Mapper
* @param string $table
* @param string $primaryKey
* @param \Closure $keygenFunction
* @param bool $preserveCasename
* @throws \Exception
*/
public function __construct($entity, $table, $primaryKey, \Closure $keygenFunction = null)
public function __construct($entity, $table, $primaryKey, \Closure $keygenFunction = null, $preserveCasename = false)
{
if (!class_exists($entity)) {
throw new \Exception("Entity '$entity' does not exists");
}
$this->entity = $entity;
$this->table = $table;
$this->primaryKey = $primaryKey;
$this->preserveCasename = $preserveCasename;
$this->primaryKey = $this->prepareField($primaryKey);
$this->keygenFunction = $keygenFunction;
}

public function prepareField($field)
{
if (!$this->preserveCasename) {
if (!is_array($field)) {
return strtolower($field);
}

$result = [];
foreach ($field as $key => $value) {
$result[strtolower($key)] = $value;
}
return $result;
}

return $field;
}

/**
* @param string $property
* @param string $fieldName
Expand All @@ -63,8 +83,8 @@ public function addFieldMap($property, $fieldName, $updateMask = false, \Closure
throw new \InvalidArgumentException('UpdateMask must be a \Closure or NULL');
}

$this->fieldMap[$property] = [
self::FIELDMAP_FIELD => $fieldName,
$this->fieldMap[$this->prepareField($property)] = [
self::FIELDMAP_FIELD => $this->prepareField($fieldName),
self::FIELDMAP_UPDATEMASK => $updateMask,
self::FIELDMAP_SELECTMASK => $selectMask
];
Expand All @@ -78,7 +98,7 @@ public function addFieldMap($property, $fieldName, $updateMask = false, \Closure
*/
public function addFieldAlias($fieldName, $alias)
{
$this->fieldAlias[$fieldName] = $alias;
$this->fieldAlias[$this->prepareField($fieldName)] = $this->prepareField($alias);
}

/**
Expand Down Expand Up @@ -106,6 +126,14 @@ public function getPrimaryKey()
return $this->primaryKey;
}

/**
* @return bool
*/
public function isPreserveCasename()
{
return $this->preserveCasename;
}

/**
* @param string|null $property
* @param string|null $key
Expand All @@ -117,6 +145,8 @@ public function getFieldMap($property = null, $key = null)
return $this->fieldMap;
}

$property = $this->prepareField($property);

if (!isset($this->fieldMap[$property])) {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public function save($instance)
{
// Get all fields
$array = BinderObject::toArrayFrom($instance, true);
$array = $this->getMapper()->prepareField($array);

// Mapping the data
foreach ((array)$this->getMapper()->getFieldMap() as $property => $fieldmap) {
Expand Down
10 changes: 5 additions & 5 deletions tests/Model/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Users
{
protected $id;
protected $Id;
protected $name;
protected $createdate;

Expand All @@ -13,15 +13,15 @@ class Users
*/
public function getId()
{
return $this->id;
return $this->Id;
}

/**
* @param mixed $id
* @param mixed $Id
*/
public function setId($id)
public function setId($Id)
{
$this->id = $id;
$this->Id = $Id;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/RepositoryAliasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public function setUp()
$this->dbDriver->execute("insert into customers (customer_name, customer_age) values ('John Doe', 40)");
$this->dbDriver->execute("insert into customers (customer_name, customer_age) values ('Jane Doe', 37)");
$this->customerMapper = new Mapper(Customer::class, 'customers', 'id');
$this->customerMapper->addFieldMap('customername', 'customer_name');
$this->customerMapper->addFieldMap('age', 'customer_age');
$this->customerMapper->addFieldMap('customerName', 'customer_Name');
$this->customerMapper->addFieldMap('Age', 'customer_Age');

$this->customerMapper->addFieldAlias('customer_age', 'custage');
$this->customerMapper->addFieldAlias('custoMer_age', 'custAge');

$this->repository = new Repository($this->dbDriver, $this->customerMapper);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function setUp()
$this->dbDriver->execute("insert into users (name, createdate) values ('John Doe', '2017-01-02')");
$this->dbDriver->execute("insert into users (name, createdate) values ('Jane Doe', '2017-01-04')");
$this->dbDriver->execute("insert into users (name, createdate) values ('JG', '1974-01-26')");
$this->userMapper = new Mapper(Users::class, 'users', 'id');
$this->userMapper = new Mapper(Users::class, 'users', 'Id');


$this->dbDriver->execute('create table info (
Expand Down

0 comments on commit 597293f

Please sign in to comment.