This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
forked from API-Skeletons/zf-doctrine-querybuilder
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract type cast behaviour and add support for the immutable datetim…
…e field types
- Loading branch information
Showing
9 changed files
with
219 additions
and
110 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/** | ||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause | ||
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com) | ||
*/ | ||
|
||
namespace ZF\Doctrine\QueryBuilder\Filter\ODM; | ||
|
||
use DateTime; | ||
use ZF\Doctrine\QueryBuilder\Filter\TypeCast\TypeCastInterface; | ||
|
||
class TypeCaster implements TypeCastInterface | ||
{ | ||
public function typeCastField($metadata, $field, $value, $format, $doNotTypecastDatetime = false) | ||
{ | ||
if (! isset($metadata->fieldMappings[$field])) { | ||
return $value; | ||
} | ||
|
||
switch ($metadata->fieldMappings[$field]['type']) { | ||
case 'int': | ||
settype($value, 'integer'); | ||
break; | ||
case 'boolean': | ||
settype($value, 'boolean'); | ||
break; | ||
case 'float': | ||
settype($value, 'float'); | ||
break; | ||
case 'string': | ||
settype($value, 'string'); | ||
break; | ||
case 'bin_data_custom': | ||
break; | ||
case 'bin_data_func': | ||
break; | ||
case 'bin_data_md5': | ||
break; | ||
case 'bin_data': | ||
break; | ||
case 'bin_data_uuid': | ||
break; | ||
case 'collection': | ||
break; | ||
case 'custom_id': | ||
break; | ||
case 'date': | ||
if ($value && ! $doNotTypecastDatetime) { | ||
if (! $format) { | ||
$format = 'Y-m-d H:i:s'; | ||
} | ||
$value = DateTime::createFromFormat($format, $value); | ||
} | ||
break; | ||
case 'file': | ||
break; | ||
case 'hash': | ||
break; | ||
case 'id': | ||
break; | ||
case 'increment': | ||
break; | ||
case 'key': | ||
break; | ||
case 'object_id': | ||
break; | ||
case 'raw_type': | ||
break; | ||
case 'timestamp': | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause | ||
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com) | ||
*/ | ||
|
||
namespace ZF\Doctrine\QueryBuilder\Filter\ORM; | ||
|
||
use DateTime; | ||
use ZF\Doctrine\QueryBuilder\Filter\TypeCast\TypeCastInterface; | ||
|
||
class TypeCaster implements TypeCastInterface | ||
{ | ||
public function typeCastField($metadata, $field, $value, $format, $doNotTypecastDatetime = false) | ||
{ | ||
if (! isset($metadata->fieldMappings[$field])) { | ||
return $value; | ||
} | ||
|
||
switch ($metadata->fieldMappings[$field]['type']) { | ||
case 'string': | ||
settype($value, 'string'); | ||
break; | ||
case 'integer': | ||
case 'smallint': | ||
#case 'bigint': // Don't try to manipulate bigints? | ||
settype($value, 'integer'); | ||
break; | ||
case 'boolean': | ||
settype($value, 'boolean'); | ||
break; | ||
case 'decimal': | ||
case 'float': | ||
settype($value, 'float'); | ||
break; | ||
case 'date': | ||
// For dates set time to midnight | ||
if ($value && ! $doNotTypecastDatetime) { | ||
if (! $format) { | ||
$format = 'Y-m-d'; | ||
} | ||
$value = DateTime::createFromFormat($format, $value); | ||
$value = DateTime::createFromFormat('Y-m-d H:i:s', $value->format('Y-m-d') . ' 00:00:00'); | ||
} | ||
break; | ||
case 'time': | ||
if ($value && ! $doNotTypecastDatetime) { | ||
if (! $format) { | ||
$format = 'H:i:s'; | ||
} | ||
$value = DateTime::createFromFormat($format, $value); | ||
} | ||
break; | ||
case 'datetime': | ||
if ($value && ! $doNotTypecastDatetime) { | ||
if (! $format) { | ||
$format = 'Y-m-d H:i:s'; | ||
} | ||
$value = DateTime::createFromFormat($format, $value); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return $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.