Completed
Pull Request — master (#9)
by Jérémy
06:26 queued 02:39
created

MetadataBag   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 1
b 0
f 0
dl 0
loc 38
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 12 2
A get() 0 3 1
A __construct() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Thunder micro CLI framework.
7
 * (c) Jérémy Marodon <[email protected]>
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace RxThunder\Core\Model;
13
14
final class MetadataBag
15
{
16
    /** @var array<string, bool|int|float|string>  */
17
    private array $parameters = [];
18
19
    /**
20
     * @param array<mixed, mixed> $parameters
21
     */
22
    public function __construct(array $parameters)
23
    {
24
        foreach ($parameters as $key => $value) {
25
            $this->add((string) $key, $value);
26
        }
27
    }
28
29
    /**
30
     * @param bool|int|float|string $value
31
     */
32
    public function add(string $key, $value): self
33
    {
34
        if (!is_scalar($value)) {
0 ignored issues
show
introduced by
The condition is_scalar($value) is always true.
Loading history...
35
            throw new \InvalidArgumentException(sprintf(
36
                'MetadataBag expect scalar value, %s given',
37
                gettype($value)
38
            ));
39
        }
40
41
        $this->parameters[$key] = $value;
42
43
        return $this;
44
    }
45
46
    /**
47
     * @return bool|int|float|string
48
     */
49
    public function get(string $key)
50
    {
51
        return $this->parameters[$key] ?? null;
52
    }
53
}
54