Completed
Push — master ( f84168...88ef34 )
by Greg
01:46
created

PropertyParser::parse()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 10
nc 4
nop 1
crap 5
1
<?php
2
namespace Consolidation\OutputFormatters\Transformations;
3
4
/**
5
 * Transform a string of properties into a PHP associative array.
6
 *
7
 * Input:
8
 *
9
 *   one: red
10
 *   two: white
11
 *   three: blue
12
 *
13
 * Output:
14
 *
15
 *   [
16
 *      'one' => 'red',
17
 *      'two' => 'white',
18
 *      'three' => 'blue',
19
 *   ]
20
 */
21
class PropertyParser
22
{
23 7
    public static function parse($data)
24
    {
25 7
        if (!is_string($data)) {
26 7
            return $data;
27
        }
28 1
        $result = [];
29 1
        $lines = explode("\n", $data);
30 1
        foreach ($lines as $line) {
31 1
            list($key, $value) = explode(':', trim($line), 2) + ['', ''];
32 1
            if (!empty($key) && !empty($value)) {
33 1
                $result[$key] = trim($value);
34 1
            }
35
        }
36
        return $result;
37
    }
38
}
39