Passed
Push — master ( 3c3c2e...b529be )
by Tim
02:22
created

TestData::addInput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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