Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #34 from archdevil666pl/master
Browse files Browse the repository at this point in the history
Added database mappers
  • Loading branch information
arius86 committed Dec 29, 2014
2 parents 3ebf366 + a52f745 commit 12ccf46
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 110 deletions.
28 changes: 15 additions & 13 deletions src/Db/Decorator/Helper/MappingHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

/**
* Class MappingHelperTrait
* Strictly coupled with \Vegas\Db\MappingResolverTrait
* Manages mappings specified manually in models & calls the mapping resolving tasks.
* @package Vegas\Db\Decorator
*/
trait MappingHelperTrait
Expand All @@ -22,14 +24,14 @@ trait MappingHelperTrait
* Defines field mappings in format
*
* <code>
* array(
* [
* 'field_1' => 'mapper_1',
* 'field_2' => array('mapper_1', 'mapper_2')
* )
* 'field_2' => ['mapper_1', 'mapper_2']
* ]
* </code>
* @var array
*/
protected $mappings = array();
protected $mappings = [];

/**
* Returns array of attributes with mapped values
Expand All @@ -39,7 +41,7 @@ trait MappingHelperTrait
public function toMappedArray()
{
$values = $this->toArray();
$mappedValues = array();
$mappedValues = [];
foreach ($values as $key => $value) {
$mappedValues[$key] = $this->readMapped($key);
}
Expand All @@ -50,7 +52,7 @@ public function toMappedArray()
/**
* Returns mapped value of attribute
*
* @param $name
* @param string $name
* @return mixed
*/
public function readMapped($name)
Expand All @@ -66,27 +68,27 @@ public function readMapped($name)
}

/**
* @param $name
* @param string $name
* @return mixed
*/
abstract public function hasMapping($name);

/**
* @param $name
* @param $mapping
* @param string $name
* @param mixed $mappings
* @return mixed
*/
abstract public function addMapping($name, $mapping);
abstract public function addMapping($name, $mappings);

/**
* @param $name
* @param string $name
* @return mixed
*/
abstract public function readAttribute($name);

/**
* @param $name
* @param $value
* @param string $name
* @param mixed $value
* @return mixed
*/
abstract public function resolveMapping($name, $value);
Expand Down
46 changes: 46 additions & 0 deletions src/Db/Mapping/Blob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* This file is part of Vegas package
*
* @author Radosław Fąfara <radek@amsterdam-standard.pl>
* @copyright Amsterdam Standard Sp. Z o.o.
* @homepage http://vegas-cmf.github.io
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vegas\Db\Mapping;

use Vegas\Db\MappingInterface;

/**
* Class Blob
*
* Simple mapper for binary fields
*
* @package Vegas\Db\Mapping
*/
class Blob implements MappingInterface
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'blob';
}

/**
* {@inheritdoc}
*/
public function resolve(& $value)
{
if (is_string($value) && strlen($value) > 0) {
$decoded = base64_decode($value, true);
$decoded !== false && $value = $decoded;
}

return $value;
}
}
45 changes: 45 additions & 0 deletions src/Db/Mapping/Camelize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of Vegas package
*
* @author Radosław Fąfara <radek@amsterdam-standard.pl>
* @copyright Amsterdam Standard Sp. Z o.o.
* @homepage http://vegas-cmf.github.io
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vegas\Db\Mapping;

use Vegas\Db\MappingInterface;

/**
* Class Camelize
*
* Simple mapper for converting text to camelize style
*
* @package Vegas\Db\Mapping
*/
class Camelize implements MappingInterface
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'camelize';
}

/**
* {@inheritdoc}
*/
public function resolve(& $value)
{
if (is_string($value) && strlen($value) > 0) {
$value = \Phalcon\Text::camelize($value);
}

return $value;
}
}
45 changes: 45 additions & 0 deletions src/Db/Mapping/Decimal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of Vegas package
*
* @author Radosław Fąfara <radek@amsterdam-standard.pl>
* @copyright Amsterdam Standard Sp. Z o.o.
* @homepage http://vegas-cmf.github.io
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vegas\Db\Mapping;

use Vegas\Db\MappingInterface;

/**
* Class Decimal
*
* Simple mapper for number fields
*
* @package Vegas\Db\Mapping
*/
class Decimal implements MappingInterface
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'decimal';
}

/**
* {@inheritdoc}
*/
public function resolve(& $value)
{
if (is_numeric($value) && strlen($value) > 0) {
$value = number_format($value, 2, '.', '');
}

return $value;
}
}
45 changes: 45 additions & 0 deletions src/Db/Mapping/Lowercase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of Vegas package
*
* @author Radosław Fąfara <radek@amsterdam-standard.pl>
* @copyright Amsterdam Standard Sp. Z o.o.
* @homepage http://vegas-cmf.github.io
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vegas\Db\Mapping;

use Vegas\Db\MappingInterface;

/**
* Class Lowercase
*
* Simple mapper for converting text to lowercase style
*
* @package Vegas\Db\Mapping
*/
class Lowercase implements MappingInterface
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'lowercase';
}

/**
* {@inheritdoc}
*/
public function resolve(& $value)
{
if (is_string($value) && strlen($value) > 0) {
$value = \Phalcon\Text::lower($value);
}

return $value;
}
}
48 changes: 48 additions & 0 deletions src/Db/Mapping/Serialize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* This file is part of Vegas package
*
* @author Radosław Fąfara <radek@amsterdam-standard.pl>
* @copyright Amsterdam Standard Sp. Z o.o.
* @homepage http://vegas-cmf.github.io
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vegas\Db\Mapping;

use Vegas\Db\MappingInterface;

/**
* Class Serialize
*
* Simple mapper for PHP serialized fields
*
* @package Vegas\Db\Mapping
*/
class Serialize implements MappingInterface
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'serialize';
}

/**
* {@inheritdoc}
*/
public function resolve(& $value)
{
if (is_string($value) && strlen($value) > 0) {
$unserialized = unserialize($value);
if ($value !== serialize(false) && $unserialized !== false) {
$value = $unserialized;
}
}

return $value;
}
}
45 changes: 45 additions & 0 deletions src/Db/Mapping/Uppercase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of Vegas package
*
* @author Radosław Fąfara <radek@amsterdam-standard.pl>
* @copyright Amsterdam Standard Sp. Z o.o.
* @homepage http://vegas-cmf.github.io
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vegas\Db\Mapping;

use Vegas\Db\MappingInterface;

/**
* Class Uppercase
*
* Simple mapper for converting text to uppercase style
*
* @package Vegas\Db\Mapping
*/
class Uppercase implements MappingInterface
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'uppercase';
}

/**
* {@inheritdoc}
*/
public function resolve(& $value)
{
if (is_string($value) && strlen($value) > 0) {
$value = \Phalcon\Text::upper($value);
}

return $value;
}
}

0 comments on commit 12ccf46

Please sign in to comment.