-
Notifications
You must be signed in to change notification settings - Fork 125
/
TestNormalizer.php
58 lines (56 loc) · 2.46 KB
/
TestNormalizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace Jane\JsonSchema\Tests\Expected\Normalizer;
use Jane\JsonSchemaRuntime\Reference;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class TestNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
{
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
public function supportsDenormalization($data, $type, $format = null)
{
return $type === 'Jane\\JsonSchema\\Tests\\Expected\\Model\\Test';
}
public function supportsNormalization($data, $format = null)
{
return $data instanceof \Jane\JsonSchema\Tests\Expected\Model\Test;
}
public function denormalize($data, $class, $format = null, array $context = array())
{
if (!is_object($data)) {
throw new InvalidArgumentException(sprintf('Given $data is not an object (%s given). We need an object in order to continue denormalize method.', gettype($data)));
}
if (isset($data->{'$ref'})) {
return new Reference($data->{'$ref'}, $context['document-origin']);
}
if (isset($data->{'$recursiveRef'})) {
return new Reference($data->{'$recursiveRef'}, $context['document-origin']);
}
$object = new \Jane\JsonSchema\Tests\Expected\Model\Test();
if (property_exists($data, 'foo')) {
$values = array();
foreach ($data->{'foo'} as $value) {
$values[] = $this->denormalizer->denormalize($value, 'Jane\\JsonSchema\\Tests\\Expected\\Model\\TestFooItem', 'json', $context);
}
$object->setFoo($values);
}
return $object;
}
public function normalize($object, $format = null, array $context = array())
{
$data = new \stdClass();
if (null !== $object->getFoo()) {
$values = array();
foreach ($object->getFoo() as $value) {
$values[] = $this->normalizer->normalize($value, 'json', $context);
}
$data->{'foo'} = $values;
}
return $data;
}
}