From 9338a1ccc3c005d9eefed9ab32b52767272547ad Mon Sep 17 00:00:00 2001 From: Gemorroj Date: Sun, 21 May 2017 17:28:06 +0300 Subject: [PATCH] minor refactoring --- src/M3uParser/Data.php | 21 ---------------- src/M3uParser/M3uParser.php | 5 +++- src/M3uParser/TagAttributesTrait.php | 37 +++++++++++++++++----------- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/M3uParser/Data.php b/src/M3uParser/Data.php index dedb363..6e07780 100644 --- a/src/M3uParser/Data.php +++ b/src/M3uParser/Data.php @@ -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 [] - * 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])); - } - } } diff --git a/src/M3uParser/M3uParser.php b/src/M3uParser/M3uParser.php index a01144b..2d32c93 100644 --- a/src/M3uParser/M3uParser.php +++ b/src/M3uParser/M3uParser.php @@ -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; } diff --git a/src/M3uParser/TagAttributesTrait.php b/src/M3uParser/TagAttributesTrait.php index 156ee52..59b2fbf 100644 --- a/src/M3uParser/TagAttributesTrait.php +++ b/src/M3uParser/TagAttributesTrait.php @@ -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; } /** @@ -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; + } }