Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed May 21, 2017
1 parent a3873bc commit 9338a1c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
21 changes: 0 additions & 21 deletions src/M3uParser/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,4 @@
class Data extends \ArrayIterator
{
use TagAttributesTrait;

/**
* @see http://l189-238-14.cn.ru/api-doc/m3u-extending.html
* @param string $lineStr
*/
public function makeAttributes($lineStr)
{
/*
* format:
* #EXTM3U [<attributes-list>]
* example:
* #EXTM3U url-tvg="http://www.teleguide.info/download/new3/jtv.zip" m3uautoload=1 deinterlace=8 cache=500
*/
$tmp = \substr($lineStr, 7);
$split = \explode(',', $tmp, 2);
$splitAttributes = \explode(' ', $split[0], 2);

if (isset($splitAttributes[1]) && \trim($splitAttributes[1])) {
$this->initAttributes(\trim($splitAttributes[1]));
}
}
}
5 changes: 4 additions & 1 deletion src/M3uParser/M3uParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public function parse($str)
}

if (static::isExtM3u($lineStr)) {
$data->makeAttributes($lineStr);
$tmp = \trim(\substr($lineStr, 7));
if ($tmp) {
$data->initAttributes($tmp);
}
continue;
}

Expand Down
37 changes: 22 additions & 15 deletions src/M3uParser/TagAttributesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,35 @@ trait TagAttributesTrait
*
* @param string $attrString
*/
protected function initAttributes($attrString)
public function initAttributes($attrString)
{
$this->attributes = \array_merge(
$this->parseQuotedAttributes($attrString),
$this->parseNotQuotedAttributes($attrString)
);
$this->parseQuotedAttributes($attrString);
$this->parseNotQuotedAttributes($attrString);
}

/**
* @param string $attrString
* @return array
*/
private function parseQuotedAttributes($attrString)
{
\preg_match_all('/([a-zA-Z0-9\-]+)="([^"]*)"/', $attrString, $matches, \PREG_SET_ORDER);

$result = array();
foreach ($matches as $match) {
$result[$match[1]] = $match[2];
$this->setAttribute($match[1], $match[2]);
}

return $result;
}


/**
* @param string $attrString
* @return array
*/
private function parseNotQuotedAttributes($attrString)
{
\preg_match_all('/([a-zA-Z0-9\-]+)=([^ "]+)/', $attrString, $matches, \PREG_SET_ORDER);

$result = array();
foreach ($matches as $match) {
$result[$match[1]] = $match[2];
$this->setAttribute($match[1], $match[2]);
}

return $result;
}

/**
Expand All @@ -69,4 +59,21 @@ public function getAttribute($name)
{
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
}

/**
* @param array $attributes
*/
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
}

/**
* @param string $name
* @param string $value
*/
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
}
}

0 comments on commit 9338a1c

Please sign in to comment.