Skip to content

Commit

Permalink
[new] Support for higher serializer versions (2.x, 3.x)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbojdo committed Mar 23, 2020
1 parent 0eb44ca commit cc743ca
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 160 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Add ***"webit/doctrine-jms-json": "^2.0.0"*** to the require section of your ***
```json
{
"require": {
"webit/doctrine-jms-json": "^2.0.0"
"webit/doctrine-jms-json": "^3.0.0"
}
}
```
Expand Down Expand Up @@ -166,3 +166,7 @@ This breaks backwards compatibility with version **1.x**. Version **1.x** cannot
Provides backwards compatibility with version **1.x** on conversion to PHP Value (reading from the database).
Please note, the data read from the format of version **1.x** will be converted automatically to the new format during flush process of the entity.
## Version 3.0.0
Provides support for higher JMS Serializer versions (2.x, 3.x). This version required PHP >= 7.2.
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
}
],
"require": {
"php": ">=5.3.2",
"php": ">=7.2",
"ext-json": "*",
"jms/serializer": "^1.0",
"doctrine/dbal": "^2.2",
"doctrine/collections": "^1.2.1"
"jms/serializer": "^2.0.0|^3.0.0",
"doctrine/dbal": "^2.10.0",
"doctrine/collections": "^1.5.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
"phpunit/phpunit": "^8.0.0"
},
"autoload": {
"psr-4": {
Expand All @@ -34,7 +34,8 @@
"extra": {
"branch-alias": {
"dev-1.x": "1.x-dev",
"dev-master": "2.x-dev"
"dev-2.x": "2.x-dev",
"dev-master": "3.x-dev"
}
}
}
7 changes: 0 additions & 7 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/autoload.php"
>

Expand All @@ -16,10 +15,4 @@
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<blacklist>
<directory>./vendor</directory>
</blacklist>
</filter>
</phpunit>
5 changes: 2 additions & 3 deletions src/DBAL/Exception/JmsJsonTypeInitializationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Webit\DoctrineJmsJson\DBAL\Exception;

class JmsJsonTypeInitializationException extends \RuntimeException
final class JmsJsonTypeInitializationException extends \RuntimeException
{

}
}
55 changes: 16 additions & 39 deletions src/DBAL/JmsJsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@

namespace Webit\DoctrineJmsJson\DBAL;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use JMS\Serializer\Serializer;
use Webit\DoctrineJmsJson\DBAL\Exception\JmsJsonTypeInitializationException;
use Webit\DoctrineJmsJson\Serializer\Type\SerializerTypeResolver;

class JmsJsonType extends Type
final class JmsJsonType extends Type
{
const NAME = 'jms_json';
public const NAME = 'jms_json';

/**
* @var Serializer
*/
private const TYPE_KEY = '_jms_type';
private const DATA_KEY = 'data';
private const LEGACY_SEPARATOR = '::';

/** @var Serializer */
private static $serializer;

/**
* @var SerializerTypeResolver
*/
/** @var SerializerTypeResolver */
private static $typeResolver;

/**
Expand Down Expand Up @@ -62,10 +61,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
return null;
}

$dbValue = array(
'_jms_type' => $this->typeResolver()->resolveType($value),
'data' => $value
);
$dbValue = [
self::TYPE_KEY => $this->typeResolver()->resolveType($value),
self::DATA_KEY => $value
];

return $this->serializer()->serialize($dbValue, 'json');
}
Expand All @@ -91,11 +90,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
$phpValue = array_shift($phpValue);
}

if (! $this->isCollection($type)) {
return $phpValue;
}

return $this->fixCollection($phpValue);
return $phpValue;
}

/**
Expand Down Expand Up @@ -142,36 +137,18 @@ private function typeResolver()
return self::$typeResolver;
}

/**
* @param string $type
* @return bool
*/
private function isCollection($type)
{
return substr($type, -10) == 'Collection' || strpos($type, 'Collection<');
}

/**
* @param mixed $phpValue
* @return ArrayCollection
*/
private function fixCollection($phpValue)
{
return new ArrayCollection((array) $phpValue);
}

private function resolveJmsTypeAndData($value)
{
$arData = @json_decode($value, true);
if (json_last_error() === JSON_ERROR_NONE) {
return array(
isset($arData['_jms_type']) ? $arData['_jms_type'] : null,
isset($arData['data']) ? $arData['data'] : null
isset($arData[self::TYPE_KEY]) ? $arData[self::TYPE_KEY] : null,
isset($arData[self::DATA_KEY]) ? $arData[self::DATA_KEY] : null
);
}

// try backward compatibility
@list($type, $data) = explode('::', $value, 2);
@list($type, $data) = explode(self::LEGACY_SEPARATOR, $value, 2);
if ($data) {
$data = @json_decode($data, true);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Serializer/Type/DefaultSerializerTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DefaultSerializerTypeResolver implements SerializerTypeResolver
* @param mixed $value
* @return string
*/
public function resolveType($value)
public function resolveType($value): string
{

if (is_scalar($value)) {
Expand Down
5 changes: 2 additions & 3 deletions src/Serializer/Type/Exception/TypeNotResolvedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Webit\DoctrineJmsJson\Serializer\Type\Exception;

class TypeNotResolvedException extends \OutOfBoundsException
final class TypeNotResolvedException extends \OutOfBoundsException
{

}
}
102 changes: 52 additions & 50 deletions tests/DBAL/JmsJsonTypeFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,39 @@
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Webit\DoctrineJmsJson\DBAL\Exception\JmsJsonTypeInitializationException;
use Webit\DoctrineJmsJson\DBAL\JmsJsonType;
use Webit\DoctrineJmsJson\Serializer\Type\DefaultSerializerTypeResolver;
use Webit\DoctrineJmsJson\Serializer\Type\SerializerTypeResolver;

class JmsJsonTypeFunctionalTest extends \PHPUnit_Framework_TestCase
class JmsJsonTypeFunctionalTest extends TestCase
{
/** @var AbstractPlatform|ObjectProphecy */
private $platform;

/** @var JmsJsonType */
private $type;

/** @var SerializerInterface */
/** @var Serializer */
private $serializer;

/** @var SerializerTypeResolver */
private $typeResolver;

protected function setUp()
protected function setUp(): void
{
$this->platform = $this->prophesize('Doctrine\DBAL\Platforms\AbstractPlatform');
$this->platform = $this->prophesize(AbstractPlatform::class);

$this->serializer = $this->buildSerializer();
$this->typeResolver = new DefaultSerializerTypeResolver();

try {
Type::addType(JmsJsonType::NAME, 'Webit\DoctrineJmsJson\DBAL\JmsJsonType');
Type::addType(JmsJsonType::NAME, JmsJsonType::class);
} catch (DBALException $e) {
}

Expand Down Expand Up @@ -81,73 +83,73 @@ public function values()
{
$date = date_create('2019-11-02 12:33:21');

return array(
array(null, null),
array(1, json_encode(array('_jms_type' => 'integer', 'data' => 1))),
array(1.25, json_encode(array('_jms_type' => 'double', 'data' => 1.25))),
array(true, json_encode(array('_jms_type' => 'boolean', 'data' => true))),
array(false, json_encode(array('_jms_type' => 'boolean', 'data' => false))),
array('abcd', json_encode(array('_jms_type' => 'string', 'data' => 'abcd'))),
array(array('k1' => 'v1'), json_encode(array('_jms_type' => 'array<string,string>', 'data' => array('k1' => 'v1')))),
array(array('v1', 'v2'), json_encode(array('_jms_type' => 'array<integer,string>', 'data' => array('v1', 'v2')))),
array($date, json_encode(array('_jms_type' => 'DateTime', 'data' => $date->format(\DateTime::ISO8601)))),
array(
new ArrayCollection(array('v1', 'v2')),
json_encode(array('_jms_type' => 'Doctrine\Common\Collections\ArrayCollection<integer,string>', 'data' => array("v1", "v2")))
),
array(
new ArrayCollection(array('k1' => 'v1', 'k2' => 'v2')),
json_encode(array('_jms_type' => 'Doctrine\Common\Collections\ArrayCollection<string,string>', 'data' => array("k1" => "v1", "k2" => "v2")))
),
array(
return [
[null, null],
[1, json_encode(['_jms_type' => 'integer', 'data' => 1])],
[1.25, json_encode(['_jms_type' => 'double', 'data' => 1.25])],
[true, json_encode(['_jms_type' => 'boolean', 'data' => true])],
[false, json_encode(['_jms_type' => 'boolean', 'data' => false])],
['abcd', json_encode(['_jms_type' => 'string', 'data' => 'abcd'])],
[['k1' => 'v1'], json_encode(['_jms_type' => 'array<string,string>', 'data' => ['k1' => 'v1']])],
[['v1', 'v2'], json_encode(['_jms_type' => 'array<integer,string>', 'data' => ['v1', 'v2']])],
[$date, json_encode(['_jms_type' => 'DateTime', 'data' => $date->format(\DateTime::ATOM)])],
[
new ArrayCollection(['v1', 'v2']),
json_encode(['_jms_type' => 'Doctrine\Common\Collections\ArrayCollection<integer,string>', 'data' => ["v1", "v2"]])
],
[
new ArrayCollection(['k1' => 'v1', 'k2' => 'v2']),
json_encode(['_jms_type' => 'Doctrine\Common\Collections\ArrayCollection<string,string>', 'data' => ["k1" => "v1", "k2" => "v2"]])
],
[
new ArrayCollection(
array(
[
new Dummy('item1', $date),
new Dummy('item2', $date)
)
]
),
json_encode(
array(
[
'_jms_type' => 'Doctrine\Common\Collections\ArrayCollection<integer,Webit\DoctrineJmsJson\Tests\DBAL\Dummy>',
'data' => array(
array('name' => 'item1', 'date' => $date->format(DATE_ISO8601)),
array('name' => 'item2', 'date' => $date->format(DATE_ISO8601)),
)
)
'data' => [
['name' => 'item1', 'date' => $date->format(\DateTime::ATOM)],
['name' => 'item2', 'date' => $date->format(\DateTime::ATOM)],
]
]
)
),
array(
],
[
new DummyAggregate(
123,
'myName',
new ArrayCollection(
array(
[
new Dummy('item1', $date),
new Dummy('item2', $date)
)
]
)
),
json_encode(
array(
[
'_jms_type' => 'Webit\DoctrineJmsJson\Tests\DBAL\DummyAggregate',
'data' => array(
'data' => [
'id' => 123,
'name' => 'myName',
'items' => array(
array(
'items' => [
[
'name' => 'item1',
'date' => $date->format(DATE_ISO8601)
),
array(
'date' => $date->format(\DateTime::ATOM)
],
[
'name' => 'item2',
'date' => $date->format(DATE_ISO8601)
)
)
)
)
'date' => $date->format(\DateTime::ATOM)
]
]
]
]
)
)
);
]
];
}

/**
Expand Down
Loading

0 comments on commit cc743ca

Please sign in to comment.