Passed
Push — main ( 633b92...66245a )
by Dimitri
12:27
created

ArrayFormatter::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Formatter;
13
14
/**
15
 * Formateur de données en tableau
16
 */
17
class ArrayFormatter implements FormatterInterface
18
{
19
    /**
20
     * Prend les données fournies et les formate.
21
     *
22
     * @param mixed $data
23
     *
24
     * @return array Données formatées sous forme de tableau ; sinon, un tableau vide
25
     */
26
    public function format($data)
27
    {
28
        if (! is_array($data)) {
29
            $data = (array) $data;
30
        }
31
32
        $array = [];
33
34
        foreach ($data as $key => $value) {
35
            if (is_object($value) || is_array($value)) {
36
                $array[$key] = $this->format($value);
37
            } else {
38
                $array[$key] = $value;
39
            }
40
        }
41
42
        return $array;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function parse(string $data): array
49
    {
50
        return [$data];
51
    }
52
}
53