VariableBag::replace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * League.Uri (https://uri.thephpleague.com)
5
 *
6
 * (c) Ignace Nyamagana Butera <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace League\Uri\UriTemplate;
15
16
use League\Uri\Exceptions\TemplateCanNotBeExpanded;
17
use function gettype;
18
use function is_array;
19
use function is_bool;
20
use function is_object;
21
use function is_scalar;
22
use function method_exists;
23
use function sprintf;
24
25
final class VariableBag
26
{
27
    /**
28
     * @var array<string,string|array<string>>
29
     */
30
    private $variables = [];
31
32
    /**
33
     * @param iterable<string,mixed> $variables
34
     */
35 18
    public function __construct(iterable $variables = [])
36
    {
37 18
        foreach ($variables as $name => $value) {
38 12
            $this->assign($name, $value);
39
        }
40 14
    }
41
42 2
    public static function __set_state(array $properties): self
43
    {
44 2
        return new self($properties['variables']);
45
    }
46
47
    /**
48
     * @return array<string,string|array<string>>
49
     */
50 6
    public function all(): array
51
    {
52 6
        return $this->variables;
53
    }
54
55
    /**
56
     * Fetches the variable value if none found returns null.
57
     *
58
     * @return null|string|array<string>
59
     */
60 14
    public function fetch(string $name)
61
    {
62 14
        return $this->variables[$name] ?? null;
63
    }
64
65
    /**
66
     * @param string|array<string> $value
67
     */
68 24
    public function assign(string $name, $value): void
69
    {
70 24
        $this->variables[$name] = $this->normalizeValue($value, $name, true);
71 20
    }
72
73
    /**
74
     * @param mixed $value the value to be expanded
75
     *
76
     * @throws TemplateCanNotBeExpanded if the value contains nested list
77
     *
78
     * @return string|array<string>
79
     */
80 24
    private function normalizeValue($value, string $name, bool $isNestedListAllowed)
81
    {
82 24
        if (is_bool($value)) {
83 4
            return true === $value ? '1' : '0';
84
        }
85
86 22
        if (null === $value || is_scalar($value) || (is_object($value) && method_exists($value, '__toString'))) {
87 18
            return (string) $value;
88
        }
89
90 10
        if (!is_array($value)) {
91 2
            throw new \TypeError(sprintf('The variable '.$name.' must be NULL, a scalar or a stringable object `%s` given', gettype($value)));
92
        }
93
94 8
        if (!$isNestedListAllowed) {
95 2
            throw TemplateCanNotBeExpanded::dueToNestedListOfValue($name);
96
        }
97
98 8
        foreach ($value as &$var) {
99 8
            $var = self::normalizeValue($var, $name, false);
0 ignored issues
show
Bug Best Practice introduced by
The method League\Uri\UriTemplate\V...leBag::normalizeValue() is not static, but was called statically. ( Ignorable by Annotation )

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

99
            /** @scrutinizer ignore-call */ 
100
            $var = self::normalizeValue($var, $name, false);
Loading history...
100
        }
101 6
        unset($var);
102
103 6
        return $value;
104
    }
105
106
    /**
107
     * Replaces elements from passed variables into the current instance.
108
     */
109 4
    public function replace(VariableBag $variables): self
110
    {
111 4
        $instance = clone $this;
112 4
        $instance->variables += $variables->variables;
113
114 4
        return $instance;
115
    }
116
}
117