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

PadArray   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 56
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D result() 0 36 9
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