Dictionary::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fundic\DataStructure;
6
7
use Fundic\DataStructure\Maybe\Maybe;
8
9
final class Dictionary
10
{
11
    private $values = [];
12
13
    private function __construct()
14
    {
15
    }
16
17
    public static function empty() : self
18
    {
19
        return new self();
20
    }
21
22
    public function has(string $id) : bool
23
    {
24
        return array_key_exists($id, $this->values);
25
    }
26
27
    public function get(string $id) : Maybe
28
    {
29
        if (array_key_exists($id, $this->values)) {
30
            return Maybe::just($this->values[$id]);
31
        }
32
33
        return Maybe::nothing();
34
    }
35
36
    public function add(string $id, $value) : self
37
    {
38
        $instance = clone $this;
39
        $instance->values[$id] = $value;
40
41
        return $instance;
42
    }
43
}
44