Completed
Pull Request — develop (#27)
by Chris
02:45
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
/*
4
 * This file is part of the PhpM3u8 package.
5
 *
6
 * (c) Chrisyue <https://chrisyue.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Chrisyue\PhpM3u8\Parser;
13
14
use Chrisyue\PhpM3u8\Config;
15
16
class AttributeListParser
17
{
18
    private $valueParsers;
19
20
    public function __construct(Config $valueParsers)
21
    {
22
        $this->valueParsers = $valueParsers;
23
    }
24
25
    public function parse($value, array $types)
26
    {
27
        preg_match_all('/(?<=^|,)[A-Z0-9-]+=("?).+?\1(?=,|$)/', $value, $matches);
28
29
        $result = new \ArrayObject();
30
        foreach ($matches[0] as $attr) {
31
            list($name, $value) = explode('=', $attr);
32
            $result[$name] = $value;
33
34
            if (!isset($types[$name])) {
35
                continue;
36
            }
37
38
            $type = $types[$name];
39
            $parse = $this->valueParsers->get($type);
40
            if (is_callable($parse)) {
41
                $result[$name] = $parse($value);
42
            }
43
        }
44
45
        return $result;
46
    }
47
}
48