Passed
Push — main ( e3d1bb...64bec1 )
by
unknown
31:25 queued 12s
created

ArrayFormatter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 30
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 3 1
A format() 0 13 5
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 2
            $data = (array) $data;
30
        }
31
32 2
        $array = [];
33
34
        foreach ($data as $key => $value) {
35 2
            $array[$key] = is_object($value) || is_array($value) ? $this->format($value) : $value;
36
        }
37
38 2
        return $array;
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function parse(string $data): array
45
    {
46 2
        return [$data];
47
    }
48
}
49