Skip to content

Commit

Permalink
handle incomplete datetime strings timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
danigargar committed Jun 18, 2024
1 parent 7c39099 commit c2e9efd
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Doctrine/Orm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ protected function stringToUtc($value)
* @param string $field
* @return ?bool
*/
private function isDateTime(ResourceMetadata $metadata, string $field)
protected function isDateTime(ResourceMetadata $metadata, string $field)
{
$filterFields = $metadata->getAttribute(
'filterFields'
Expand Down
89 changes: 88 additions & 1 deletion Doctrine/Orm/Filter/SearchFilterStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@
namespace Ivoz\Api\Doctrine\Orm\Filter;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use Doctrine\DBAL\Driver\PDO\Exception;
use Doctrine\ORM\QueryBuilder;
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use Ivoz\Core\Domain\Model\Helper\DateTimeHelper;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;

/**
* @inheritdoc
Expand All @@ -14,7 +25,10 @@ class SearchFilterStart extends SearchFilter

public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = [])
{

$contextCopy = (new \ArrayObject($context))->getArrayCopy();
$contextFilters = $contextCopy['filters'];

foreach ($contextCopy['filters'] as $field => $filters) {

if (strpos($field, '_') === 0) {
Expand Down Expand Up @@ -46,6 +60,12 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
}
}

$metadata = $this->resourceMetadataFactory->create($resourceClass);
$contextCopy['filters'] = $this->dateFiltersToUtc(
$metadata,
$contextFilters
);

return parent::apply($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $contextCopy);
}

Expand All @@ -68,10 +88,77 @@ protected function normalizeValues(array $values, string $property): ?array
}
}

if (is_array($values[self::STRATEGY_START])) {
$isStrategyStartArray = isset($values[self::STRATEGY_START]) && is_array($values[self::STRATEGY_START]);
if ($isStrategyStartArray) {
return array_values($values[self::STRATEGY_START]);
}

return array_values($values);
}

protected function dateFiltersToUtc(ResourceMetadata $metadata, array $filters)
{
foreach ($filters as $field => $criteria) {
if (!$this->isDateTime($metadata, $field)) {
continue;
}

if (is_array($criteria)) {

foreach ($criteria as $filter => $value) {

if (is_array($value)) {
$filters[$field][$filter] = array_map(
[$this, 'partialStringToUtc'],
$value
);
} else {
$filters[$field][$filter] = $this->partialStringToUtc($value);
}
}

continue;
}

$filters[$field] = $this->partialStringToUtc($criteria);
}

return $filters;
}

private function partialStringToUtc(string $value)
{
$match = [];
$res = preg_match('/(\d{4}-\d{2}-\d{2})(?: ([0-9]{0,2})(?::([0-9]{0,2})(?::([0-9]{0,2}))?)?)/', $value, $match);

if (!$res) {
return $value;
}

$date = $match[1];

$match = array_slice($match, 2);
$timeParts = array_pad(
$match,
3,
""
);

$timeParts = array_map(
function (string $value) {
return str_pad($value, 2, "0", STR_PAD_RIGHT);
},
$timeParts
);

$utcDataTime = $this->stringToUtc(
sprintf('%s %s:%s:%s', $date, ...$timeParts)
);

return substr(
$utcDataTime,
0,
strlen($value)
);
}
}

0 comments on commit c2e9efd

Please sign in to comment.