NormalizationLinkMappingBuilder::createCallback()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Mapping;
6
7
use Chubbyphp\Serialization\Normalizer\CallbackLinkNormalizer;
8
use Chubbyphp\Serialization\Normalizer\LinkNormalizer;
9
use Chubbyphp\Serialization\Normalizer\LinkNormalizerInterface;
10
use Chubbyphp\Serialization\Policy\NullPolicy;
11
use Chubbyphp\Serialization\Policy\PolicyInterface;
12
use Psr\Link\LinkInterface;
13
14
final class NormalizationLinkMappingBuilder implements NormalizationLinkMappingBuilderInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $name;
20
21
    /**
22
     * @var array
23
     */
24
    private $groups = [];
25
26
    /**
27
     * @var LinkNormalizerInterface
28
     */
29
    private $linkNormalizer;
30
31
    /**
32
     * @var PolicyInterface|null
33
     */
34
    private $policy;
35
36
    private function __construct(string $name)
37
    {
38
        $this->name = $name;
39 4
    }
40
41 4
    public static function create(
42 4
        string $name,
43
        LinkNormalizerInterface $linkNormalizer
44
    ): NormalizationLinkMappingBuilderInterface {
45
        $self = new self($name);
46
        $self->linkNormalizer = $linkNormalizer;
47
48
        return $self;
49
    }
50 2
51
    public static function createCallback(
52
        string $name,
53
        callable $callback
54 2
    ): NormalizationLinkMappingBuilderInterface {
55 2
        $self = new self($name);
56
        $self->linkNormalizer = new CallbackLinkNormalizer($callback);
57 2
58
        return $self;
59
    }
60
61
    public static function createLink(
62
        string $name,
63
        LinkInterface $link
64
    ): NormalizationLinkMappingBuilderInterface {
65
        $self = new self($name);
66 1
        $self->linkNormalizer = new LinkNormalizer($link);
67
68
        return $self;
69
    }
70 1
71 1
    /**
72
     * @deprecated
73 1
     */
74
    public function setGroups(array $groups): NormalizationLinkMappingBuilderInterface
75
    {
76
        $this->groups = $groups;
77
78
        return $this;
79
    }
80
81
    public function setPolicy(PolicyInterface $policy): NormalizationLinkMappingBuilderInterface
82 1
    {
83
        $this->policy = $policy;
84
85
        return $this;
86 1
    }
87 1
88
    public function getMapping(): NormalizationLinkMappingInterface
89 1
    {
90
        return new NormalizationLinkMapping(
91
            $this->name,
92
            $this->groups,
93
            $this->linkNormalizer,
94
            $this->policy ?? new NullPolicy()
95
        );
96
    }
97
}
98