Completed
Pull Request — develop (#27)
by Chris
05:29
created

AttributeListParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 22 4
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Parser;
4
5
use Chrisyue\PhpM3u8\Line\Line;
6
use Chrisyue\PhpM3u8\Config;
7
8
class AttributeListParser
9
{
10
    private $valueParsers;
11
12
    public function __construct(Config $valueParsers)
13
    {
14
        $this->valueParsers = $valueParsers;
15
    }
16
17
    public function parse($value, array $types)
18
    {
19
        preg_match_all('/(?<=^|,)[A-Z0-9-]+=("?).+?\1(?=,|$)/', $value, $matches);
20
21
        $result = new \ArrayObject();
22
        foreach ($matches[0] as $attr) {
23
            list($name, $value) = explode('=', $attr);
24
            $result[$name] = $value;
25
26
            if (!isset($types[$name])) {
27
                continue;
28
            }
29
30
            $type = $types[$name];
31
            $parse = $this->valueParsers->get($type);
32
            if (is_callable($parse)) {
33
                $result[$name] = $parse($value);
34
            }
35
        }
36
37
        return $result;
38
    }
39
}
40