This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from chrisandchris/feature/auto-camel-case
added type caster
- Loading branch information
Showing
16 changed files
with
371 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Changelog for v2.3.0 | ||
|
||
This file is the changelog for changes from version 2.2.4 to 2.3.0 | ||
|
||
## Additions | ||
* added typecast function to cast fields when mapping, using "field_name::int" when building query (currently only in MySQL!) | ||
|
||
## Changes | ||
* to support typecast, the ParserInterface was extended with `::getMappingInfo()` |
18 changes: 18 additions & 0 deletions
18
src/ChrisAndChris/Common/RowMapperBundle/Exceptions/Mapping/UnknownCastTypeException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace ChrisAndChris\Common\RowMapperBundle\Exceptions\Mapping; | ||
|
||
/** | ||
* | ||
* | ||
* @name UnknownCastTypeException | ||
* @version 1.0.0 | ||
* @since 1.0.0 | ||
* @package RowMapperBundle | ||
* @author ChrisAndChris | ||
* @link https://github.com/chrisandchris | ||
*/ | ||
class UnknownCastTypeException extends MappingException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/ChrisAndChris/Common/RowMapperBundle/Services/Mapper/TypeCaster.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace ChrisAndChris\Common\RowMapperBundle\Services\Mapper; | ||
|
||
use ChrisAndChris\Common\RowMapperBundle\Exceptions\Mapping\UnknownCastTypeException; | ||
|
||
/** | ||
* Casts arbitrary values to specified values | ||
* | ||
* @name TypeCaster | ||
* @version 1.0.0 | ||
* @since 1.0.0 | ||
* @package RowMapperBundle | ||
* @author ChrisAndChris | ||
* @link https://github.com/chrisandchris | ||
*/ | ||
class TypeCaster | ||
{ | ||
|
||
/** @var array */ | ||
public $casts; | ||
|
||
/** | ||
* TypeCaster constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->casts = [ | ||
'int' => [$this, 'castInt'], | ||
'json' => [$this, 'castJson'], | ||
]; | ||
} | ||
|
||
public function cast($targetType, $value) | ||
{ | ||
if (!isset($this->casts[$targetType])) { | ||
throw new UnknownCastTypeException(sprintf( | ||
'Cannot cast to unknown type %s', | ||
$targetType | ||
)); | ||
} | ||
|
||
return call_user_func($this->casts[$targetType], $value); | ||
} | ||
|
||
/** | ||
* @param $value | ||
* @return int | ||
*/ | ||
public function castInt($value) | ||
{ | ||
return (int)$value; | ||
} | ||
|
||
public function castJson($value) | ||
{ | ||
return json_decode($value, true); | ||
} | ||
|
||
public function castDate($value) | ||
{ | ||
if ($value instanceof \DateTime) { | ||
return $value; | ||
} | ||
|
||
return new \DateTime($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.