TestData::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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