Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
378 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
/phpunit.xml | ||
/tests/Fixtures/cache | ||
/tests/Fixtures/logs | ||
/var |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Dunglas\DoctrineJsonOdm; | ||
|
||
use Symfony\Component\Serializer\Serializer as BaseSerializer; | ||
|
||
final class Serializer extends BaseSerializer | ||
{ | ||
private const KEY_TYPE = '#type'; | ||
private const KEY_SCALAR = '#scalar'; | ||
|
||
public function normalize($data, $format = null, array $context = []) | ||
{ | ||
$normalizedData = parent::normalize($data, $format, $context); | ||
|
||
if (is_object($data)) { | ||
$typeData = [self::KEY_TYPE => get_class($data)]; | ||
$valueData = is_scalar($normalizedData) ? [self::KEY_SCALAR => $normalizedData] : $normalizedData; | ||
$normalizedData = array_merge($typeData, $valueData); | ||
} | ||
|
||
return $normalizedData; | ||
} | ||
|
||
public function denormalize($data, $class, $format = null, array $context = []) | ||
{ | ||
if (is_array($data) && (isset($data[self::KEY_TYPE]))) { | ||
$type = $data[self::KEY_TYPE]; | ||
unset($data[self::KEY_TYPE]); | ||
|
||
$data = $data[self::KEY_SCALAR] ?? $data; | ||
$data = $this->denormalize($data, $type, $format, $context); | ||
|
||
return parent::denormalize($data, $type, $format, $context); | ||
} | ||
|
||
if (is_iterable($data)) { | ||
$class = ($class === '') ? 'stdClass' : $class; | ||
|
||
return parent::denormalize($data, $class.'[]', $format, $context); | ||
} | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Dunglas\DoctrineJsonOdm\Tests; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
abstract class AbstractKernelTestCase extends KernelTestCase | ||
{ | ||
/** | ||
* @var Application | ||
*/ | ||
protected $application; | ||
|
||
protected function setUp() | ||
{ | ||
$this->bootKernel(); | ||
|
||
$this->application = new Application(self::$kernel); | ||
$this->application->setAutoExit(false); | ||
} | ||
} |
Oops, something went wrong.