ArrayConverter::recursive()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 20
rs 9.2222
cc 6
nc 10
nop 2
1
<?php
2
3
/*
4
 * This file is part of the SexyField package.
5
 *
6
 * (c) Dion Snoeijen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare (strict_types = 1);
13
14
namespace Tardigrades\Helper;
15
16
class ArrayConverter
17
{
18
    private static $value = '';
19
20
    /**
21
     * This helper converts a (part of) a configuration array to a string
22
     * The config elements will be listed and shown when using a console command
23
     *
24
     * @param array $array
25
     * @param int $level
26
     * @return string
27
     */
28
    public static function recursive(array $array, int $level = 1): string
29
    {
30
        if ($level === 1) {
31
            self::$value = '';
32
        }
33
34
        foreach ($array as $key => $value) {
35
            if (is_array($value)) {
36
                self::$value .= str_repeat('-', $level - 1) .
37
                    (($level - 1 > 0) ? ' ' : '') .
38
                    $key . ':' . PHP_EOL;
39
                self::recursive($value, $level + 1);
40
            } else {
41
                self::$value .= str_repeat('-', $level - 1) .
42
                    (($level - 1 > 0) ? ' ' : '') .
43
                    $key . ':' . $value . PHP_EOL;
44
            }
45
        }
46
47
        return self::$value;
48
    }
49
}
50