Completed
Push — array-output ( a2191f )
by Craig
01:47
created

PadArray::result()   D

Complexity

Conditions 9
Paths 26

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 4.909
c 0
b 0
f 0
cc 9
eloc 24
nc 26
nop 0
1
<?php
2
3
namespace League\CLImate\TerminalObject\Basic;
4
5
class PadArray extends BasicTerminalObject
6
{
7
    /**
8
     * The data to convert to JSON
9
     *
10
     * @var mixed $data
11
     */
12
    protected $data;
13
14
    public function __construct($data)
15
    {
16
        $this->data = $data;
17
    }
18
19
    /**
20
     * Return the data as JSON
21
     *
22
     * @return string
23
     */
24
    public function result()
25
    {
26
        # Default to at least 5 spaces of padding
27
        $max = 5;
28
29
        # Convert each element in the array to a string
30
        foreach ($this->data as &$value) {
31
            $type = strtolower(gettype($value));
32
            switch ($type) {
33
                case 'boolean':
34
                    $value = $value ? '[true]' : '[false]';
35
                    break;
36
                case 'array':
37
                case 'object':
38
                case 'resource':
39
                    $value = '[' . $type . ']';
40
                    break;
41
                default:
42
                    $value = (string) $value;
43
                    break;
44
            }
45
46
            $len = strlen($value);
47
            if ($len > $max) {
48
                $max = $len;
49
            }
50
        }
51
        unset($value);
52
53
        $padding = new Padding($max);
54
        $padding->char('-');
55
56
        foreach ($this->data as $key => $value) {
57
            $padding->label($key)->result($value);
58
        }
59
    }
60
}
61