-
Notifications
You must be signed in to change notification settings - Fork 125
/
FooNormalizer.php
66 lines (64 loc) · 2.51 KB
/
FooNormalizer.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
<?php
namespace Jane\JsonSchema\Tests\Expected\Normalizer;
use Jane\Component\JsonSchemaRuntime\Reference;
use Jane\JsonSchema\Tests\Expected\Runtime\Normalizer\CheckArray;
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 FooNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
{
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
use CheckArray;
public function supportsDenormalization($data, $type, $format = null) : bool
{
return $type === 'Jane\\JsonSchema\\Tests\\Expected\\Model\\Foo';
}
public function supportsNormalization($data, $format = null) : bool
{
return $data instanceof \Jane\JsonSchema\Tests\Expected\Model\Foo;
}
/**
* @return mixed
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
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\Foo();
$validator = new \Jane\JsonSchema\Tests\Expected\Validator\FooValidator();
if (!($data['skip_validation'] ?? false)) {
$validator->validate($data);
}
if (null === $data || false === \is_array($data)) {
return $object;
}
if (\array_key_exists('foo', $data)) {
$object->setFoo($data['foo']);
}
return $object;
}
/**
* @return array|string|int|float|bool|\ArrayObject|null
*/
public function normalize($object, $format = null, array $context = array())
{
$data = array();
if (null !== $object->getFoo()) {
$data['foo'] = $object->getFoo();
}
$validator = new \Jane\JsonSchema\Tests\Expected\Validator\FooValidator();
if (!($data['skip_validation'] ?? false)) {
$validator->validate($data);
}
return $data;
}
}