DataSource::setData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © Thomas Klein, All rights reserved.
4
 * See LICENSE bundled with this library for license details.
5
 */
6
declare(strict_types=1);
7
8
namespace LogicTree;
9
10
/**
11
 * @api
12
 */
13
class DataSource
14
{
15
    /**
16
     * Associative data array by key pair value
17
     *
18
     * @var array
19
     */
20
    private $data;
21
22
    public function __construct(iterable $data = [])
23
    {
24
        $this->setData($data);
25
    }
26
27
    public function getData(): array
28
    {
29
        return $this->data;
30
    }
31
32
    public function setData(iterable $data): DataSource
33
    {
34
        $this->data = [];
35
36
        foreach ($data as $key => $value) {
37
            $this->setValue($key, $value);
38
        }
39
40
        return $this;
41
    }
42
43
    public function addData(iterable $data): DataSource
44
    {
45
        foreach ($data as $key => $value) {
46
            $this->setValue($key, $value);
47
        }
48
49
        return $this;
50
    }
51
52
    public function getValue(string $key)
53
    {
54
        return $this->data[$key] ?? null;
55
    }
56
57
    public function setValue(string $key, $value): DataSource
58
    {
59
        $this->data[$key] = $value;
60
61
        return $this;
62
    }
63
64
    public function unsetValue(string $key): DataSource
65
    {
66
        unset($this->data[$key]);
67
68
        return $this;
69
    }
70
71
    public function unsetValues(array $keys): DataSource
72
    {
73
        foreach ($keys as $key) {
74
            $this->unsetValue($key);
75
        }
76
77
        return $this;
78
    }
79
}
80