datetime strategy, some changes in order to make subtype parsing possible
This commit is contained in:
parent
1119759d1d
commit
f08978c567
3 changed files with 58 additions and 6 deletions
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue