CategoriesNormalizer::normalize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\Question\Serializers;
4
5
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
6
use VSV\GVQ_API\Question\Models\Categories;
7
use VSV\GVQ_API\Question\Models\Category;
8
9
class CategoriesNormalizer implements NormalizerInterface
10
{
11
    /**
12
     * @var CategoryNormalizer
13
     */
14
    private $categoryNormalizer;
15
16
    /**
17
     * @param CategoryNormalizer $categoryNormalizer
18
     */
19
    public function __construct(CategoryNormalizer $categoryNormalizer)
20
    {
21
        $this->categoryNormalizer = $categoryNormalizer;
22
    }
23
24
    /**
25
     * @inheritdoc
26
     * @param Categories $categories
27
     */
28
    public function normalize($categories, $format = null, array $context = []): array
29
    {
30
        return array_map(
31
            function (Category $category) use ($format, $context) {
32
                return $this->categoryNormalizer->normalize($category, $format, $context);
33
            },
34
            $categories->toArray()
35
        );
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function supportsNormalization($data, $format = null): bool
42
    {
43
        return ($data instanceof Categories) && ($format === 'json');
44
    }
45
}
46