Passed
Branch master (4b23d6)
by Tim
04:40
created

TestData::addInput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace SimpleSAML\Module\Monitor;
4
5
final class TestData
6
{
7
    /** @var array */
8
    private $testData = [];
9
10
11
    /**
12
     * @param array $input
13
     */
14
    public function __construct(array $input = [])
15
    {
16
        $this->setInput($input);
17
    }
18
19
20
    /**
21
     * @param mixed|null $input
22
     * @param string|null $key
23
     *
24
     * @return void
25
     */
26
    public function setInput($input, string $key = null): void
27
    {
28
        if (is_null($key)) {
29
            assert(is_array($input));
30
            foreach ($input as $key => $value) {
31
                $this->addInput($key, $value);
32
            }
33
        } elseif (array_key_exists($key, $this->testData)) {
34
            $this->testData[$key] = $input;
35
        } else {
36
            $this->addInput($key, $input);
37
        }
38
    }
39
40
41
    /**
42
     * @param string $key
43
     * @param mixed|null $value
44
     *
45
     * @return void
46
     */
47
    public function addInput(string $key, $value = null): void
48
    {
49
        if (isset($this->testData[$key])) {
50
            assert(is_array($this->testData[$key]));
51
            $this->testData[$key] = array_merge($this->testData[$key], $value);
52
        } else {
53
            $this->testData[$key] = $value;
54
        }
55
    }
56
57
58
    /**
59
     * @return array
60
     */
61
    public function getInput(): array
62
    {
63
        return $this->testData;
64
    }
65
66
67
    /**
68
     * @param string $item
69
     *
70
     * @return mixed|null
71
     */
72
    public function getInputItem(string $item)
73
    {
74
        return array_key_exists($item, $this->testData) ? $this->testData[$item] : null;
75
    }
76
}
77