CollectionNormalizer::denormalize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 4
dl 0
loc 7
rs 10
1
<?php
2
3
4
namespace W2w\Laravel\Apie\Plugins\Illuminate\Normalizers;
5
6
use Illuminate\Support\Collection;
7
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
8
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9
use Symfony\Component\Serializer\SerializerAwareInterface;
10
use Symfony\Component\Serializer\SerializerAwareTrait;
11
12
class CollectionNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
13
{
14
    use SerializerAwareTrait;
15
16
    /**
17
     * {@inheritDoc}
18
     */
19
    public function denormalize($data, string $type, string $format = null, array $context = [])
20
    {
21
        if (array_key_exists('collection_resource', $context)) {
22
            unset($context['object_to_populate']);
23
            return new Collection($this->serializer->denormalize($data, $context['collection_resource'] . '[]', $format, $context));
24
        }
25
        return new Collection($data);
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function supportsDenormalization($data, string $type, string $format = null)
32
    {
33
        return $type === Collection::class;
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function normalize($object, string $format = null, array $context = [])
40
    {
41
        $result = [];
42
        foreach ($object as $value) {
43
            $result[] = $this->serializer->normalize($value, $format, $context);
44
        }
45
        return $result;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function supportsNormalization($data, string $format = null)
52
    {
53
        return $data instanceof Collection;
54
    }
55
}
56