1
0
Fork 0
mirror of synced 2025-04-20 01:21:01 +00:00

datetime strategy, some changes in order to make subtype parsing possible

This commit is contained in:
Pavel 2020-07-28 22:04:12 +03:00
parent 1119759d1d
commit f08978c567
3 changed files with 58 additions and 6 deletions

View file

@ -0,0 +1,35 @@
<?php
/**
* PHP version 7.1
*
* @category Integration
* @package Intaro\RetailCrm\Component\Json\Strategy\Deserialize
* @author retailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://retailcrm.ru/docs
*/
namespace Intaro\RetailCrm\Component\Json\Strategy\Deserialize;
/**
* Class DateTimeStrategy
*
* @package Intaro\RetailCrm\Component\Json\Strategy\Deserialize
*/
class DateTimeStrategy implements DeserializeStrategyInterface
{
use InnerTypeTrait;
/**
* @inheritDoc
*/
public function deserialize(string $type, $value)
{
if (!empty($value)) {
return \DateTime::createFromFormat($this->innerType, $value);
}
return null;
}
}

View file

@ -46,6 +46,10 @@ class StrategyFactory
return new Serialize\SimpleTypeStrategy();
}
if (static::isDateTime($dataType)) {
return (new Serialize\DateTimeStrategy())->setInnerType(\DateTime::RFC3339);
}
// TODO: DateTime<format> strategy and array<valueType>, array<keyType, valueType> strategies
return new Serialize\EntityStrategy();
@ -58,14 +62,30 @@ class StrategyFactory
*/
public static function deserializeStrategyByType(string $dataType): DeserializeStrategyInterface
{
print_r($dataType);
if (in_array($dataType, static::$simpleTypes)) {
return new Deserialize\SimpleTypeStrategy();
}
if (static::isDateTime($dataType)) {
return (new Deserialize\DateTimeStrategy())->setInnerType(\DateTime::RFC3339);
}
// TODO: DateTime<format> strategy and array<valueType>, array<keyType, valueType> strategies
return new Deserialize\EntityStrategy();
}
/**
* Returns true if provided type is DateTime
*
* @param string $dataType
*
* @return bool
*/
private static function isDateTime(string $dataType): bool
{
return strlen($dataType) > 1
&& (\DateTime::class === $dataType
|| ('\\' === $dataType[0] && \DateTime::class === substr($dataType, 1)));
}
}

View file

@ -12,9 +12,6 @@
*/
namespace Intaro\RetailCrm\Model;
use Intaro\RetailCrm\Component\Json\Mapping\Accessor;
use Intaro\RetailCrm\Component\Json\Mapping\Name;
/**
* Class Customer
* TODO: Create necessary models for retailCRM entities