Issues (15)

src/DataSource.php (2 issues)

1
<?php
2
/**
3
 * Copyright © 2018 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
 * Class DataSource
12
 * @api
13
 */
14
class DataSource
15
{
16
    /**
17
     * Associative data array by key pair value
18
     *
19
     * @var array
20
     */
21
    private $data;
22
23
    /**
24
     * DataSource constructor
25
     *
26
     * @param iterable $data [optional]
27
     */
28
    public function __construct(iterable $data = [])
0 ignored issues
show
Short array syntax is not allowed
Loading history...
29
    {
30
        $this->setData($data);
31
    }
32
33
    /**
34
     * Retrieve the data
35
     *
36
     * @return array
37
     */
38
    public function getData(): array
39
    {
40
        return $this->data;
41
    }
42
43
    /**
44
     * Set the new data
45
     *
46
     * @param iterable $data
47
     * @return \LogicTree\DataSource
48
     */
49
    public function setData(iterable $data): DataSource
50
    {
51
        $this->data = [];
0 ignored issues
show
Short array syntax is not allowed
Loading history...
52
53
        foreach ($data as $key => $value) {
54
            $this->setValue($key, $value);
55
        }
56
57
        return $this;
58
    }
59
60
    /**
61
     * Add and replace data on duplicate
62
     *
63
     * @param iterable $data
64
     * @return \LogicTree\DataSource
65
     */
66
    public function addData(iterable $data): DataSource
67
    {
68
        foreach ($data as $key => $value) {
69
            $this->setValue($key, $value);
70
        }
71
72
        return $this;
73
    }
74
75
    /**
76
     * Remove the data by keys
77
     *
78
     * @param array $keys
79
     * @return \LogicTree\DataSource
80
     */
81
    public function unsetData(array $keys): DataSource
82
    {
83
        foreach ($keys as $key) {
84
            $this->unsetValue($key);
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * Retrieve the data value
92
     *
93
     * @param string $key
94
     * @return mixed|null
95
     */
96
    public function getValue(string $key)
97
    {
98
        return $this->data[$key] ?? null;
99
    }
100
101
    /**
102
     * Set the data value
103
     *
104
     * @param string $key
105
     * @param mixed $value
106
     * @return \LogicTree\DataSource
107
     */
108
    public function setValue(string $key, $value): DataSource
109
    {
110
        $this->data[$key] = $value;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Remove the data value
117
     *
118
     * @param string $key
119
     * @return \LogicTree\DataSource
120
     */
121
    public function unsetValue(string $key): DataSource
122
    {
123
        unset($this->data[$key]);
124
125
        return $this;
126
    }
127
}
128