Passed
Branch master (6f7d84)
by compolom
105:47 queued 103:53
created

Param::__toString()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 5
cts 7
cp 0.7143
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3.2098
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 7
    public function __construct(string $name, $value)
20
    {
21 7
        $this->name = $name;
22 7
        $this->value = $this->escape($value);
23 7
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function getName(): string
29
    {
30
        return $this->name;
31
    }
32
33
    /**
34
     * @param string $value
35
     * @return mixed
36
     */
37 7
    private function escape($value = '')
38
    {
39 7
        return empty($value) ? false : $value;
40
    }
41
42
//    /**
43
//     * @param $value
44
//     * @return mixed
45
//     */
46
//    private function bool($value)
47
//    {
48
//        $values = [
49
//            'true',
50
//            'false',
51
//            'null',
52
//            'on',
53
//            'off',
54
//            'yes',
55
//            'no'
56
//        ];
57
//
58
//        return (in_array($value, $values, true) || is_numeric($value) || is_array($value) ? $value : '"' . $value . '"');
59
//    }
60
61
    /**
62
     * @return mixed
63
     */
64
    public function getValue()
65
    {
66
        return $this->value;
67
    }
68
69 2
    public function __toString(): string
70
    {
71 2
        $return = '';
72
73 2
        if (is_array($this->value)) {
74
            foreach ($this->value as $key => $value) {
75
                $return .= $this->name . '[' . $key . '] = ' . $this->escape($value) . PHP_EOL;
76
            }
77
        } else {
78 2
            $return = $this->name . ' = ' . $this->value . PHP_EOL;
79
        }
80
81 2
        return $return;
82
    }
83
}
84