Completed
Push — develop ( bd286b...3e7014 )
by Chris
01:03 queued 59s
created

KeyTags   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 38.1%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 95
ccs 16
cts 42
cp 0.381
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A rewind() 0 4 1
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
A valid() 0 4 1
A offsetSet() 0 14 4
A offsetGet() 0 4 1
A offsetExists() 0 4 1
A offsetUnset() 0 4 1
A add() 0 4 1
A readLines() 0 13 3
A dump() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the PhpM3u8 package.
5
 *
6
 * (c) Chrisyue <http://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;
13
14
class KeyTags implements DumpableInterface, \Iterator, \ArrayAccess
15
{
16
    private $tags = [];
17
    private $position;
18
19
    public function rewind()
20
    {
21
        $this->position = 0;
22
    }
23
24
    public function current()
25
    {
26
        return $this->tags[$this->position];
27
    }
28
29
    public function key()
30
    {
31
        return $this->position;
32
    }
33
34
    public function next()
35
    {
36
        ++$this->position;
37
    }
38
39
    /**
40
     * @return bool
41
     */
42
    public function valid()
43
    {
44
        return isset($this->tags[$this->position]);
45
    }
46
47
    public function offsetSet($offset, $value)
48
    {
49
        if (!is_object($value) || !$value instanceof Tag\KeyTag) {
50
            throw new \InvalidArgumentException('Expected $value of class Chrisyue\PhpM3u8\Tag\KeyTag');
51
        }
52
53
        if (is_null($offset)) {
54
            $this->add($value);
55
56
            return;
57
        }
58
59
        $this->tags[$offset] = $value;
60
    }
61
62
    /**
63
     * @return Chrisyue\PhpM3u8\Tag\KeyTag
64
     */
65 1
    public function offsetGet($offset)
66
    {
67 1
        return $this->tags[$offset];
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function offsetExists($offset)
74
    {
75
        return isset($this->tags[$offset]);
76
    }
77
78
    public function offsetUnset($offset)
79
    {
80
        unset($this->tags[$offset]);
81
    }
82
83 1
    public function add(Tag\KeyTag $tag)
84
    {
85 1
        $this->tags[] = $tag;
86 1
    }
87
88 1
    public function readLines(array &$lines)
89
    {
90 1
        while (true) {
91 1
            $tag = new Tag\KeyTag();
92 1
            $tag->readLines($lines);
93
94 1
            if (null === $tag->getMethod()) {
95 1
                return;
96
            }
97
98 1
            $this->tags[] = $tag;
99 1
        }
100
    }
101
102
    public function dump()
103
    {
104 1
        return implode("\n", array_map(function (Tag\KeyTag $tag) {
105 1
            return $tag->dump();
106 1
        }, $this->tags));
107
    }
108
}
109