LazyCollectionNormalizer::denormalize()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
c 1
b 0
f 1
nc 2
nop 4
dl 0
loc 12
rs 10
1
<?php
2
3
4
namespace W2w\Laravel\Apie\Plugins\Illuminate\Normalizers;
5
6
use Illuminate\Support\LazyCollection;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\LazyCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 LazyCollectionNormalizer 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 LazyCollection::make(function () use (&$data, &$format, &$context) {
24
                foreach ($data as $key => $value) {
25
                    yield $key => $this->serializer->denormalize($value, $context['collection_resource'], $format, $context);
26
                }
27
            });
28
        }
29
        return LazyCollection::make(function () use (&$data) {
30
            yield from $data;
31
        });
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    public function supportsDenormalization($data, string $type, string $format = null)
38
    {
39
        return $type === LazyCollection::class;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function normalize($object, string $format = null, array $context = [])
46
    {
47
        $result = [];
48
        foreach ($object as $value) {
49
            $result[] = $this->serializer->normalize($value, $format, $context);
50
        }
51
        return $result;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function supportsNormalization($data, string $format = null)
58
    {
59
        return $data instanceof LazyCollection;
60
    }
61
}
62