Conditions | 9 |
Paths | 12 |
Total Lines | 43 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
16 | public function serialize(array $input): string |
||
17 | { |
||
18 | $builder = new TomlBuilder(); |
||
19 | |||
20 | $tables = []; |
||
21 | |||
22 | // TOML doesn't support root data array so we add dummy `data` key. |
||
23 | if (is_numeric(array_key_first($input))) { |
||
24 | |||
25 | foreach ($input as $item) { |
||
26 | |||
27 | $builder->addArrayOfTable('data'); |
||
28 | |||
29 | foreach ($item as $key => $value) { |
||
30 | $builder->addValue($key, $value); |
||
31 | } |
||
32 | } |
||
33 | |||
34 | return $builder->getTomlString(); |
||
35 | } |
||
36 | |||
37 | foreach ($input as $key => $value) { |
||
38 | |||
39 | // Associative arrays map to TOML tables and shouldn't be mixed between regular values. |
||
40 | if (is_array($value) && !is_numeric(array_key_first($value))) { |
||
41 | $tables[$key] = $value; |
||
42 | continue; |
||
43 | } |
||
44 | |||
45 | $builder->addValue((string)$key, $value); |
||
46 | } |
||
47 | |||
48 | foreach ($tables as $name => $items) { |
||
49 | |||
50 | $builder->addTable($name); |
||
51 | |||
52 | foreach ($items as $itemKey => $itemValue) { |
||
53 | $builder->addValue((string)$itemKey, $itemValue); |
||
54 | } |
||
55 | |||
56 | } |
||
57 | |||
58 | return $builder->getTomlString(); |
||
59 | } |
||
69 |