Completed
Pull Request — master (#15)
by
unknown
03:34
created

NormalizerContext::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use Chubbyphp\Serialization\Policy\GroupPolicy;
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
     * @param ServerRequestInterface|null $request
32
     * @param array                       $attributes
33
     */
34 2
    public function __construct(array $groups = [], ServerRequestInterface $request = null, array $attributes = [])
35
    {
36 2
        if ([] !== $groups) {
37 1
            @trigger_error(sprintf('groups are deprecated, use "%s" instead', GroupPolicy::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
38
        }
39
40 2
        $this->groups = $groups;
0 ignored issues
show
Deprecated Code introduced by
The property Chubbyphp\Serialization\...malizerContext::$groups has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

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

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

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