Completed
Pull Request — develop (#10)
by
unknown
23:29 queued 08:32
created

Parser.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
use Chrisyue\PhpM3u8\Loader\LoaderInterface;
15
use Chrisyue\PhpM3u8\M3u8\M3u8;
16
use Chrisyue\PhpM3u8\M3u8\MediaSegment;
17
use Chrisyue\PhpM3u8\M3u8\Playlist;
18
19
class Parser
20
{
21
    private $loader;
22
23 1
    public function setLoader(LoaderInterface $loader)
24
    {
25 1
        $this->loader = $loader;
26
27 1
        return $this;
28
    }
29
30 2
    public function parseFromUri($uri)
31
    {
32 2
        if (null === $this->loader) {
33 1
            throw new \BadMethodCallException('You should set an m3u8 loader first');
34
        }
35
36 1
        return $this->parse($this->loader->load($uri));
37
    }
38
39 2
    public function parse($content)
40
    {
41 2
        $data = $this->content2Data($content);
42
43 2
        if (empty($data['version'])) {
44
            $data['version'] = 3;
45
        }
46
47 2
        if (empty($data['mediaSequence'])) {
48
            $data['mediaSequence'] = 0;
49
        }
50
51 2
        $playlist = new Playlist();
52 2
        foreach ($data['playlist'] as $index => $row) {
53 2
            $mediaSegment = new MediaSegment(
54 2
                $row['uri'],
55 2
                $row['duration'],
56 2
                $data['mediaSequence'] + $index,
57 2
                !empty($row['isDiscontinuity']),
58 2
                empty($row['title']) ? null : $row['title'],
59 2
                empty($row['byteRangeLength']) ? null : $row['byteRangeLength'],
60 2
                empty($row['byteRangeOffset']) ? null : $row['byteRangeOffset']
61 2
            );
62
            $playlist->add($mediaSegment);
63 2
        }
64
65
        return new M3u8($playlist, $data['version'], $data['targetDuration']);
66 2
    }
67
68 2
    private function content2Data($content)
69
    {
70 2
        $data = array();
71
72 2
        $mediaSequence = 0;
73 2
74 2
        $lines = explode("\n", $content);
75
        foreach ($lines as $line) {
76 2
            $line = trim($line);
77 2
78 2
            if (preg_match('/^#EXT-X-VERSION:(\d+)/', $line, $matches)) {
79
                $data['version'] = $matches[1];
80
                continue;
81 2
            }
82 2
83 2
            if (preg_match('/^#EXT-X-TARGETDURATION:(\d+)/', $line, $matches)) {
84
                $data['targetDuration'] = +$matches[1];
85
                continue;
86 2
            }
87 2
88 2
            if (preg_match('/^#EXT-X-MEDIA-SEQUENCE:(\d+)/', $line, $matches)) {
89
                $data['mediaSequence'] = +$matches[1];
90
                continue;
91 2
            }
92
93
            if (preg_match('/^#EXT-X-DISCONTINUITY/', $line)) {
94
                $data['playlist'][$mediaSequence]['isDiscontinuity'] = true;
95
                continue;
96 2
            }
97 2
98 View Code Duplication
            if (preg_match('/^#EXTINF:(.+),(.*)$/', $line, $matches)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99 2
                $data['playlist'][$mediaSequence]['duration'] = $matches[1];
100 2
101 2
                if (isset($matches[2])) {
102
                    $data['playlist'][$mediaSequence]['title'] = $matches[2];
103 2
                }
104
105
                continue;
106 2
            }
107 2
108 2 View Code Duplication
            if (preg_match('/^#EXT-X-BYTERANGE:(\d+)(@(\d+))?$/', $line, $matches)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109 2
                $data['playlist'][$mediaSequence]['byteRangeLength'] = (int) $matches[1];
110 2
                if (isset($matches[3])) {
111
                    $data['playlist'][$mediaSequence]['byteRangeOffset'] = (int) $matches[3];
112 2
                }
113
                continue;
114
            }
115
116
            if (preg_match('/^[^#]+/', $line, $matches)) {
117
                $data['playlist'][$mediaSequence]['uri'] = $matches[0];
118
                ++$mediaSequence;
119
            }
120
        }
121
122
        return $data;
123
    }
124
}
125