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

ArrayFormatter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 17 5
A parse() 0 3 1
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