NormalizerContext::getAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 1
cts 1
cp 1
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\GroupPolicy;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
final class NormalizerContext implements NormalizerContextInterface
11
{
12
    /**
13
     * @deprecated
14
     *
15
     * @var string[]
16
     */
17
    private $groups = [];
18
19
    /**
20
     * @var ServerRequestInterface|null
21
     */
22
    private $request;
23
24
    /**
25
     * @var array
26
     */
27
    private $attributes;
28
29
    /**
30
     * @param string[] $groups
31
     */
32
    public function __construct(array $groups = [], ServerRequestInterface $request = null, array $attributes = [])
33
    {
34 3
        if ([] !== $groups) {
35
            @trigger_error(sprintf('groups are deprecated, use "%s" instead', GroupPolicy::class), E_USER_DEPRECATED);
36 3
        }
37 2
38
        $this->groups = $groups;
0 ignored issues
show
Deprecated Code introduced by
The property Chubbyphp\Serialization\...malizerContext::$groups has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

38
        /** @scrutinizer ignore-deprecated */ $this->groups = $groups;
Loading history...
39
        $this->request = $request;
40 3
        $this->attributes = $attributes;
41 3
    }
42 3
43 3
    /**
44
     * @deprecated
45
     *
46
     * @return string[]
47
     */
48
    public function getGroups(): array
49
    {
50 2
        return $this->groups;
0 ignored issues
show
Deprecated Code introduced by
The property Chubbyphp\Serialization\...malizerContext::$groups has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

50
        return /** @scrutinizer ignore-deprecated */ $this->groups;
Loading history...
51
    }
52 2
53
    /**
54
     * @return ServerRequestInterface|null
55
     */
56
    public function getRequest()
57
    {
58 2
        return $this->request;
59
    }
60 2
61
    public function getAttributes(): array
62
    {
63
        return $this->attributes;
64
    }
65
66 3
    /**
67
     * @param mixed $default
68 3
     *
69
     * @return mixed
70
     */
71
    public function getAttribute(string $name, $default = null)
72
    {
73
        if (isset($this->attributes[$name])) {
74
            return $this->attributes[$name];
75
        }
76
77 2
        return $default;
78
    }
79 2
80 1
    /**
81
     * @param mixed $value
82
     */
83 1
    public function withAttribute(string $name, $value): NormalizerContextInterface
84
    {
85
        $context = clone $this;
86
        $context->attributes[$name] = $value;
87
88
        return $context;
89
    }
90
}
91