Skip to content

Commit

Permalink
Extract type cast behaviour
Browse files Browse the repository at this point in the history
Signed-off-by: Zacharias Luiten <[email protected]>
  • Loading branch information
zluiten committed Oct 27, 2020
1 parent 8af0bcb commit 6fbf99f
Show file tree
Hide file tree
Showing 13 changed files with 935 additions and 145 deletions.
37 changes: 11 additions & 26 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,26 @@ env:
matrix:
fast_finish: true
include:
- php: 5.6
env:
- DEPS=lowest
- MONGO_DRIVER=mongo
- php: 5.6
env:
- DEPS=latest
- MONGO_DRIVER=mongo
- php: 7
env:
- DEPS=lowest
- php: 7
env:
- DEPS=latest
- php: 7.1
env:
- DEPS=lowest
- php: 7.1
env:
- DEPS=latest
- CS_CHECK=true
- TEST_COVERAGE=true
- php: 7.2
env:
- DEPS=lowest
- php: 7.2
env:
- DEPS=latest
# Disabled because some lowest dependencies are not compatible with PHP 7.3
# - php: 7.3
# env:
# - DEPS=lowest
# Disabled because some lowest dependencies are not compatible with PHP 7.3
# - php: 7.3
# env:
# - DEPS=lowest
- php: 7.3
env:
- DEPS=latest
# Disabled because some lowest dependencies are not compatible with PHP 7.4
# - php: 7.4
# env:
# - DEPS=lowest
- php: 7.4
env:
- DEPS=latest

before_install:
- if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}
},
"require": {
"php": "^5.6 || ^7.0",
"php": "^7.2",
"doctrine/doctrine-module": "^1.2 || ^2.1.8",
"laminas-api-tools/api-tools-api-problem": "^1.2.2",
"laminas-api-tools/api-tools-doctrine": "^2.1",
Expand Down
2 changes: 2 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
Filter\Service\ODMFilterManager::class => Filter\Service\ODMFilterManagerFactory::class,
OrderBy\Service\ORMOrderByManager::class => OrderBy\Service\ORMOrderByManagerFactory::class,
OrderBy\Service\ODMOrderByManager::class => OrderBy\Service\ODMOrderByManagerFactory::class,
Filter\ORM\TypeCaster::class => \Laminas\ServiceManager\Factory\InvokableFactory::class,
Filter\ODM\TypeCaster::class => \Laminas\ServiceManager\Factory\InvokableFactory::class,
],
],
];
97 changes: 37 additions & 60 deletions src/Filter/ODM/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,51 @@

namespace Laminas\ApiTools\Doctrine\QueryBuilder\Filter\ODM;

use DateTime;
use Laminas\ApiTools\Doctrine\QueryBuilder\Filter\FilterInterface;
use Laminas\ApiTools\Doctrine\QueryBuilder\Filter\TypeCastInterface;

abstract class AbstractFilter implements FilterInterface
{
abstract public function filter($queryBuilder, $metadata, $option);

protected function typeCastField($metadata, $field, $value, $format = null, $doNotTypecastDatetime = false)
/**
* @var TypeCastInterface
*/
protected $typeCaster;

public function __construct(array $params = [])
{
$this->setTypeCaster($params['type_caster'] ?? new TypeCaster());
}

/**
* @param TypeCastInterface $typeCaster
* @return $this
*/
public function setTypeCaster(TypeCastInterface $typeCaster)
{
if (! isset($metadata->fieldMappings[$field])) {
return $value;
}
$this->typeCaster = $typeCaster;
return $this;
}

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 TypeCastInterface
*/
protected function getTypeCaster()
{
return $this->typeCaster;
}

return $value;
/**
* @param object $metadata
* @param string $field
* @param string|int|float $value
* @param string|null $format
* @param bool $doNotTypecastDatetime
* @return mixed
*/
protected function typeCastField($metadata, string $field, $value, string $format = null, bool $doNotTypecastDatetime = false)
{
return $this->getTypeCaster()->typeCastField($metadata, $field, $value, $format, $doNotTypecastDatetime);
}
}
91 changes: 91 additions & 0 deletions src/Filter/ODM/TypeCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* @see https://github.com/laminas-api-tools/api-tools-doctrine-querybuilder for the canonical source repository
* @copyright https://github.com/laminas-api-tools/api-tools-doctrine-querybuilder/blob/master/COPYRIGHT.md
* @license https://github.com/laminas-api-tools/api-tools-doctrine-querybuilder/blob/master/LICENSE.md New BSD License
*/

namespace Laminas\ApiTools\Doctrine\QueryBuilder\Filter\ODM;

use DateTime;
use DateTimeImmutable;
use Laminas\ApiTools\Doctrine\QueryBuilder\Filter\TypeCastInterface;

class TypeCaster implements TypeCastInterface
{
/**
* {@inheritDoc}
*/
public function typeCastField($metadata, string $field, $value, string $format = null, bool $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 'date_immutable':
if ($value && ! $doNotTypecastDatetime) {
if (! $format) {
$format = 'Y-m-d H:i:s';
}
$value = DateTimeImmutable::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;
}
}
Loading

0 comments on commit 6fbf99f

Please sign in to comment.