CallbackLinkNormalizer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
use Chubbyphp\Serialization\Policy\PolicyInterface;
8
use Chubbyphp\Serialization\SerializerLogicException;
9
use Psr\Link\LinkInterface;
10
11
final class CallbackLinkNormalizer implements LinkNormalizerInterface
12
{
13
    /**
14
     * @var callable
15
     */
16
    private $callback;
17
18
    public function __construct(callable $callback)
19
    {
20
        $this->callback = $callback;
21 3
    }
22
23 3
    /**
24 3
     * @param object $object
25
     *
26
     * @throws SerializerLogicException
27
     *
28
     * @return array|null
29
     */
30
    public function normalizeLink(string $path, $object, NormalizerContextInterface $context)
31
    {
32
        $callback = $this->callback;
33
34
        $link = $callback($path, $object, $context);
35 3
36
        if (null === $link) {
37 3
            @trigger_error(
38
                sprintf(
39 3
                    'If a link should not be serialized under certain conditions, use a "%s" instead',
40
                    PolicyInterface::class
41 3
                ),
42 1
                E_USER_DEPRECATED
43 1
            );
44 1
45 1
            return null;
46
        }
47 1
48
        if (!$link instanceof LinkInterface) {
49
            $type = is_object($link) ? get_class($link) : gettype($link);
50 1
51
            throw SerializerLogicException::createInvalidLinkTypeReturned($path, $type);
52
        }
53 2
54 1
        return (new LinkNormalizer($link))->normalizeLink($path, $object, $context);
55
    }
56
}
57