Completed
Pull Request — develop (#27)
by Chris
20:10 queued 05:10
created

AttributeList::transform()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Transformer;
4
5
/**
6
 * @Annotation
7
 */
8
class AttributeList implements TransformerInterface
9
{
10
    public function transform($origin)
11
    {
12
        preg_match_all('/(?<=^|,)[A-Z0-9-]+=("?).+?\1(?=,|$)/', $origin, $matches);
13
14
        $attributes = [];
15
        foreach ($matches[0] as $attr) {
16
            list($key, $value) = explode('=', $attr);
17
            $attributes[$key] = trim($value);
18
        }
19
20
        return $attributes;
21
    }
22
23
    public function reverse($attributes)
24
    {
25
        $keyVals = [];
26
        foreach ($attributes as $name => $attribute) {
27
            $keyVals[] = sprintf('%s=%s', $name, $attribute);
28
        }
29
30
        return implode(',', $keyVals);
31
    }
32
}
33