Skip to content

Commit

Permalink
fix #12 The functionality of adding tags has been redesigned
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed Feb 26, 2018
1 parent a00a57a commit 3d201e9
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 68 deletions.
46 changes: 13 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ $ composer update gemorroj/m3u-parser
use M3uParser\M3uParser;

$m3uParser = new M3uParser();
$m3uParser->addDefaultTags();
$data = $m3uParser->parseFile('path_to_file.m3u');

print_r($data->getAttributes());
Expand All @@ -49,15 +50,15 @@ Array
)
*/

/** @var \M3uParser\Entry $entry */
/** @var \M3uParser\M3uEntry $entry */
foreach ($data as $entry) {
print_r($entry);
/*
M3uParser\Entry Object
M3uParser\M3uEntry Object
(
[lineDelimiter:protected] =>

[extTags:M3uParser\Entry:private] => Array
[extTags:M3uParser\M3uEntry:private] => Array
(
[0] => M3uParser\Tag\ExtInf Object
(
Expand All @@ -84,7 +85,7 @@ foreach ($data as $entry) {

)

[path:M3uParser\Entry:private] => rtp://@232.2.201.53:5003
[path:M3uParser\M3uEntry:private] => rtp://@232.2.201.53:5003
)
*/

Expand Down Expand Up @@ -118,12 +119,12 @@ foreach ($data as $entry) {

```php
<?php
use M3uParser\Data;
use M3uParser\Entry;
use M3uParser\M3uData;
use M3uParser\M3uEntry;
use M3uParser\Tag\ExtInf;
use M3uParser\Tag\ExtTv;

$entry = new Entry();
$entry = new M3uEntry();
$entry->setPath('test-path');
$entry->addExtTag(
(new ExtInf())
Expand All @@ -139,7 +140,7 @@ $entry->addExtTag(
->setTags(['hd', 'sd'])
);

$data = new Data();
$data = new M3uData();
$data->setAttribute('test-name', 'test-value');
$data->append($entry);

Expand Down Expand Up @@ -237,38 +238,17 @@ example:
}
}

// send implemented tag to M3uParser constructor
$m3uParser = new M3uParser([ExtCustomTag::class]);
$m3uParser = new M3uParser();
// add custom tag
$m3uParser->addTag(ExtCustomTag::class);
$data = $m3uParser->parseFile('path_to_file.m3u');

print_r($data);
/*
M3uParser\Data Object
(
[attributes:M3uParser\Data:private] => Array
(
)
M3uData

[storage:ArrayIterator:private] => Array
(
[0] => M3uParser\Entry Object
(
[lineDelimiter:protected] =>

[extTags:M3uParser\Entry:private] => Array
(
[0] => M3uParser\Tests\ExtCustomTag Object
(
[data:M3uParser\Tests\ExtCustomTag:private] => 123
)

)

[path:M3uParser\Entry:private] => http://nullwave.barricade.lan:8000/club
)

)

)
*/
```
2 changes: 1 addition & 1 deletion src/Data.php → src/M3uData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace M3uParser;

class Data extends \ArrayIterator
class M3uData extends \ArrayIterator
{
use TagAttributesTrait;

Expand Down
2 changes: 1 addition & 1 deletion src/Entry.php → src/M3uEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use M3uParser\Tag\ExtTagInterface;

class Entry
class M3uEntry
{
protected $lineDelimiter = "\n";
/**
Expand Down
39 changes: 14 additions & 25 deletions src/M3uParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,31 @@

namespace M3uParser;

use M3uParser\Tag\ExtInf;
use M3uParser\Tag\ExtTv;

class M3uParser
{
protected $availableTags = [];

/**
* @return Entry
*/
protected function createEntry()
{
return new Entry();
}
use TagsManagerTrait;

/**
* @return Data
* @return M3uEntry
*/
protected function createData()
protected function createM3uEntry()
{
return new Data();
return new M3uEntry();
}

/**
* @param string[] $availableTags
* @return M3uData
*/
public function __construct(array $availableTags = [ExtInf::class, ExtTv::class])
protected function createM3uData()
{
$this->availableTags = $availableTags;
return new M3uData();
}

/**
* Parse m3u file
*
* @param string $file
* @return Data entries
* @return M3uData entries
* @throws Exception
*/
public function parseFile($file)
Expand All @@ -54,13 +43,13 @@ public function parseFile($file)
* Parse m3u string
*
* @param string $str
* @return Data entries
* @return M3uData entries
*/
public function parse($str)
{
$this->removeBom($str);

$data = $this->createData();
$data = $this->createM3uData();
$lines = \explode("\n", $str);

for ($i = 0, $l = \count($lines); $i < $l; ++$i) {
Expand Down Expand Up @@ -88,11 +77,11 @@ public function parse($str)
*
* @param int $lineNumber
* @param string[] $linesStr
* @return Entry
* @return M3uEntry
*/
protected function parseLine(&$lineNumber, array $linesStr)
{
$entry = $this->createEntry();
$entry = $this->createM3uEntry();

for ($l = \count($linesStr); $lineNumber < $l; ++$lineNumber) {
$nextLineStr = $linesStr[$lineNumber];
Expand All @@ -103,7 +92,7 @@ protected function parseLine(&$lineNumber, array $linesStr)
}

$matched = false;
foreach ($this->availableTags as $availableTag) {
foreach ($this->getTags() as $availableTag) {
if ($availableTag::isMatch($nextLineStr)) {
$matched = true;
$entry->addExtTag(new $availableTag($nextLineStr));
Expand Down Expand Up @@ -146,7 +135,7 @@ protected function isExtM3u($lineStr)
protected function isComment($lineStr)
{
$matched = false;
foreach ($this->availableTags as $availableTag) {
foreach ($this->getTags() as $availableTag) {
if ($availableTag::isMatch($lineStr)) {
$matched = true;
break;
Expand Down
64 changes: 64 additions & 0 deletions src/TagsManagerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace M3uParser;


use M3uParser\Tag\ExtInf;
use M3uParser\Tag\ExtTagInterface;
use M3uParser\Tag\ExtTv;

trait TagsManagerTrait
{
private $tags = [];

/**
* Add tag
*
* @param string $tag class name must be implement ExtTagInterface interface
* @return $this
* @throws Exception
*/
public function addTag($tag)
{
if (!\in_array(ExtTagInterface::class, \class_implements($tag), true)) {
throw new Exception(\sprintf('The class %s must be implement interface %s', $tag, ExtTagInterface::class));
}

$this->tags[] = $tag;
return $this;
}

/**
* Add default tags (EXTINF and EXTTV)
*
* @return $this
* @throws Exception
*/
public function addDefaultTags()
{
$this->addTag(ExtInf::class);
$this->addTag(ExtTv::class);
return $this;
}

/**
* Remove all previously defined tags
*
* @return $this
*/
public function clearTags()
{
$this->tags = [];
return $this;
}

/**
* Get all active tags
*
* @return string[]
*/
protected function getTags()
{
return $this->tags;
}
}
10 changes: 5 additions & 5 deletions tests/DataTest.php → tests/M3uDataTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace M3uParser\Tests;

use M3uParser\Data;
use M3uParser\Entry;
use M3uParser\M3uData;
use M3uParser\M3uEntry;
use M3uParser\Tag\ExtInf;
use M3uParser\Tag\ExtTv;

class DataTest extends \PHPUnit_Framework_TestCase
class M3uDataTest extends \PHPUnit_Framework_TestCase
{
public function testToString()
{
Expand All @@ -16,7 +16,7 @@ public function testToString()
$expectedString .= 'test-path';


$entry = new Entry();
$entry = new M3uEntry();
$entry->setPath('test-path');
$entry->addExtTag(
(new ExtInf())
Expand All @@ -32,7 +32,7 @@ public function testToString()
->setTags(['hd', 'sd'])
);

$data = new Data();
$data = new M3uData();
$data->setAttribute('test-name', 'test-value');
$data->append($entry);

Expand Down
14 changes: 11 additions & 3 deletions tests/M3uParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use M3uParser\M3uParser;
use M3uParser\Exception as M3uParserException;
use M3uParser\Data as M3uParserData;
use M3uParser\Entry as M3uParserEntry;
use M3uParser\M3uData as M3uParserData;
use M3uParser\M3uEntry as M3uParserEntry;
use M3uParser\Tag\ExtTagInterface;
use M3uParser\Tag\ExtInf;
use M3uParser\Tag\ExtTv;
Expand All @@ -16,12 +16,14 @@ public function testParseFileFail()
$this->expectException(M3uParserException::class);

$m3uParser = new M3uParser();
$m3uParser->addDefaultTags();
$m3uParser->parseFile('fake_file');
}

public function testParseFileExtInf()
{
$m3uParser = new M3uParser();
$m3uParser->addDefaultTags();
$data = $m3uParser->parseFile(__DIR__ . '/fixtures/extinf.m3u');

self::assertInstanceOf(M3uParserData::class, $data);
Expand Down Expand Up @@ -98,6 +100,7 @@ public function testParseFileExtInf()
public function testParseFileExtM3u()
{
$m3uParser = new M3uParser();
$m3uParser->addDefaultTags();
$data = $m3uParser->parseFile(__DIR__ . '/fixtures/extm3u.m3u');

self::assertEquals([
Expand All @@ -113,6 +116,7 @@ public function testParseFileExtM3u()
public function testParseFileComment()
{
$m3uParser = new M3uParser();
$m3uParser->addDefaultTags();
$data = $m3uParser->parseFile(__DIR__ . '/fixtures/comment.m3u');

/** @var M3uParserEntry $entry */
Expand All @@ -125,6 +129,7 @@ public function testParseFileComment()
public function testParseFileNoTags()
{
$m3uParser = new M3uParser();
$m3uParser->addDefaultTags();
$data = $m3uParser->parseFile(__DIR__ . '/fixtures/notags.m3u');

/** @var M3uParserEntry $entry */
Expand All @@ -138,6 +143,7 @@ public function testParseFileNoTags()
public function testParseFileExtTv()
{
$m3uParser = new M3uParser();
$m3uParser->addDefaultTags();
$data = $m3uParser->parseFile(__DIR__ . '/fixtures/exttv.m3u');

/** @var M3uParserEntry $entry */
Expand All @@ -162,6 +168,7 @@ public function testParseFileExtTv()
public function testParseFileCombinedExtTags()
{
$m3uParser = new M3uParser();
$m3uParser->addDefaultTags();
$data = $m3uParser->parseFile(__DIR__ . '/fixtures/combined.m3u');

/** @var M3uParserEntry $entry */
Expand All @@ -179,7 +186,8 @@ public function testParseFileCombinedExtTags()

public function testParseFileExtCustomTag()
{
$m3uParser = new M3uParser([ExtCustomTag::class]);
$m3uParser = new M3uParser();
$m3uParser->addTag(ExtCustomTag::class);
$data = $m3uParser->parseFile(__DIR__ . '/fixtures/customtag.m3u');

/** @var M3uParserEntry $entry */
Expand Down
Loading

0 comments on commit 3d201e9

Please sign in to comment.