Metadata   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A kv() 0 3 1
A fromArray() 0 3 1
A get() 0 7 2
A all() 0 3 1
A has() 0 3 1
A jsonSerialize() 0 3 1
A __construct() 0 3 1
A merge() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Common\Domain\Model;
6
7
use JsonSerializable;
8
use OutOfBoundsException;
9
10
final class Metadata implements JsonSerializable
11
{
12
    private $values;
13
14
    public static function fromArray(array $values): self
15
    {
16
        return new self($values);
17
    }
18
19
    public static function kv(string $key, $value): self
20
    {
21
        return new self([$key => $value]);
22
    }
23
24
    public static function create(): self
25
    {
26
        return new self([]);
27
    }
28
29
    public function has(string $key): bool
30
    {
31
        return isset($this->values[$key]);
32
    }
33
34
    /**
35
     * @throws OutOfBoundsException
36
     *
37
     * @return mixed
38
     */
39
    public function get(string $key)
40
    {
41
        if (!isset($this->values[$key])) {
42
            throw new OutOfBoundsException(sprintf('Value by key "%s" not found.', $key));
43
        }
44
45
        return $this->values[$key];
46
    }
47
48
    public function all(): array
49
    {
50
        return $this->values;
51
    }
52
53
    public function merge(Metadata $metadata): self
54
    {
55
        $values = array_merge($this->values, $metadata->values);
56
57
        return new self($values);
58
    }
59
60
    public function jsonSerialize(): array
61
    {
62
        return $this->values;
63
    }
64
65
    private function __construct(array $values)
66
    {
67
        $this->values = $values;
68
    }
69
}
70