Passed
Branch master (f41941)
by compolom
01:27
created

Param::escape()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Compolomus\IniObject;
6
7
class Param
8
{
9
    private $name;
10
11
    private $value;
12
13
    /**
14
     * Param constructor.
15
     *
16
     * @param string $name
17
     * @param mixed $value
18
     */
19 12
    public function __construct(string $name, $value)
20
    {
21 12
        $this->name = $name;
22 12
        $this->value = $this->escape($value);
23 12
    }
24
25
    /**
26
     * @return string
27
     */
28 2
    public function getName(): string
29
    {
30 2
        return $this->name;
31
    }
32
33
    /**
34
     * @param string $value
35
     * @return mixed
36
     */
37 12
    private function escape($value = '')
38
    {
39 12
        return empty($value) ? false : $value;
40
    }
41
42
    /**
43
     * @return mixed
44
     */
45 5
    public function getValue()
46
    {
47 5
        return $this->value ?? null;
48
    }
49
50 1
    public function __toString(): string
51
    {
52 1
        $return = '';
53
54 1
        if (is_array($this->getValue())) {
55 1
            foreach ($this->getValue() as $key => $value) {
56 1
                $return .= $this->getName() . '[' . $key . '] = ' . $this->escape($value) . PHP_EOL;
57
            }
58
        } else {
59 1
            $return = $this->getName() . ' = ' . $this->value . PHP_EOL;
60
        }
61
62 1
        return $return;
63
    }
64
65 1
    public function toArray(): array
66
    {
67 1
        return [$this->getName() => $this->value];
68
    }
69
}
70