-
Notifications
You must be signed in to change notification settings - Fork 125
/
DocumentNormalizer.php
68 lines (66 loc) · 2.95 KB
/
DocumentNormalizer.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
59
60
61
62
63
64
65
66
67
68
<?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 DocumentNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
{
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
public function supportsDenormalization($data, $type, $format = null)
{
return $type === 'Jane\\JsonSchema\\Tests\\Expected\\Model\\Document';
}
public function supportsNormalization($data, $format = null)
{
return $data instanceof \Jane\JsonSchema\Tests\Expected\Model\Document;
}
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\Document();
if (property_exists($data, 'attributes')) {
$value = $data->{'attributes'};
if (is_array($data->{'attributes'})) {
$values = array();
foreach ($data->{'attributes'} as $value_1) {
$values[] = $this->denormalizer->denormalize($value_1, 'Jane\\JsonSchema\\Tests\\Expected\\Model\\Attributes', 'json', $context);
}
$value = $values;
} elseif (is_null($data->{'attributes'})) {
$value = $data->{'attributes'};
}
$object->setAttributes($value);
}
return $object;
}
public function normalize($object, $format = null, array $context = array())
{
$data = new \stdClass();
$value = $object->getAttributes();
if (is_array($object->getAttributes())) {
$values = array();
foreach ($object->getAttributes() as $value_1) {
$values[] = $this->normalizer->normalize($value_1, 'json', $context);
}
$value = $values;
} elseif (is_null($object->getAttributes())) {
$value = $object->getAttributes();
}
$data->{'attributes'} = $value;
return $data;
}
}