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

AttributeList   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 25
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 12 2
A reverse() 0 9 2
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