@@ -15,15 +15,15 @@ |
||
| 15 | 15 | |
| 16 | 16 | class StreamParser |
| 17 | 17 | { |
| 18 | - public static function xml(String $source){ |
|
| 18 | + public static function xml(String $source) { |
|
| 19 | 19 | return (new XMLParser())->from($source); |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | - public static function json(String $source){ |
|
| 22 | + public static function json(String $source) { |
|
| 23 | 23 | return (new JSONParser())->from($source); |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | - public static function csv(String $source){ |
|
| 26 | + public static function csv(String $source) { |
|
| 27 | 27 | return (new CSVParser())->from($source); |
| 28 | 28 | } |
| 29 | 29 | } |
| 30 | 30 | \ No newline at end of file |
@@ -19,8 +19,8 @@ |
||
| 19 | 19 | |
| 20 | 20 | public function __construct() |
| 21 | 21 | { |
| 22 | - Collection::macro('recursive', function () { |
|
| 23 | - return $this->map(function ($value) { |
|
| 22 | + Collection::macro('recursive', function() { |
|
| 23 | + return $this->map(function($value) { |
|
| 24 | 24 | if (is_array($value) || is_object($value)) { |
| 25 | 25 | return (new Collection($value))->recursive(); |
| 26 | 26 | } |
@@ -17,12 +17,12 @@ discard block |
||
| 17 | 17 | { |
| 18 | 18 | protected $reader, $source, $headers, $currentLine; |
| 19 | 19 | |
| 20 | - public static $delimiters = [",", ";"]; |
|
| 20 | + public static $delimiters = [ ",", ";" ]; |
|
| 21 | 21 | |
| 22 | 22 | public function __construct() |
| 23 | 23 | { |
| 24 | - Collection::macro('recursive', function () { |
|
| 25 | - return $this->map(function ($value) { |
|
| 24 | + Collection::macro('recursive', function() { |
|
| 25 | + return $this->map(function($value) { |
|
| 26 | 26 | if (is_array($value) || is_object($value)) { |
| 27 | 27 | return (new Collection($value))->recursive(); |
| 28 | 28 | } |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | public function each(callable $function) |
| 42 | 42 | { |
| 43 | 43 | $this->start(); |
| 44 | - while($this->read()){ |
|
| 44 | + while ($this->read()) { |
|
| 45 | 45 | $function($this->getCurrentLineAsCollection()); |
| 46 | 46 | } |
| 47 | 47 | $this->close(); |
@@ -57,8 +57,8 @@ discard block |
||
| 57 | 57 | return $this; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - private function close(){ |
|
| 61 | - if( ! fclose($this->reader)){ |
|
| 60 | + private function close() { |
|
| 61 | + if ( ! fclose($this->reader)) { |
|
| 62 | 62 | throw new IncompleteParseException(); |
| 63 | 63 | } |
| 64 | 64 | } |
@@ -76,20 +76,20 @@ discard block |
||
| 76 | 76 | return $headers->intersectByKeys($this->currentLine)->combine($values->recursive()); |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - private function formatCurrentLineValues(Collection $collection){ |
|
| 79 | + private function formatCurrentLineValues(Collection $collection) { |
|
| 80 | 80 | $this->explodeCollectionValues($collection); |
| 81 | 81 | return $collection; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - private function explodeCollectionValues(Collection $collection){ |
|
| 85 | - $collection->transform(function($value){ |
|
| 84 | + private function explodeCollectionValues(Collection $collection) { |
|
| 85 | + $collection->transform(function($value) { |
|
| 86 | 86 | (new Collection(static::$delimiters))->each(function($delimiter) use (&$value){ |
| 87 | - if( ! is_array($value) && strpos($value, $delimiter) !== false){ |
|
| 87 | + if ( ! is_array($value) && strpos($value, $delimiter) !== false) { |
|
| 88 | 88 | $value = explode($delimiter, $value); |
| 89 | 89 | } |
| 90 | 90 | }); |
| 91 | - if(is_array($value)){ |
|
| 92 | - return (new Collection($value))->reject(function($value){ |
|
| 91 | + if (is_array($value)) { |
|
| 92 | + return (new Collection($value))->reject(function($value) { |
|
| 93 | 93 | return empty($value); |
| 94 | 94 | }); |
| 95 | 95 | } else { |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | |
| 18 | 18 | class XMLParser implements StreamParserInterface |
| 19 | 19 | { |
| 20 | - protected $reader,$source; |
|
| 20 | + protected $reader, $source; |
|
| 21 | 21 | |
| 22 | 22 | protected $skipFirstElement = true; |
| 23 | 23 | |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | return $this; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - public function withoutSkippingFirstElement(){ |
|
| 31 | + public function withoutSkippingFirstElement() { |
|
| 32 | 32 | $this->skipFirstElement = false; |
| 33 | 33 | |
| 34 | 34 | return $this; |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | public function each(callable $function) |
| 38 | 38 | { |
| 39 | 39 | $this->start(); |
| 40 | - while($this->reader->read()){ |
|
| 40 | + while ($this->reader->read()) { |
|
| 41 | 41 | $this->searchElement($function); |
| 42 | 42 | } |
| 43 | 43 | $this->stop(); |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | |
| 46 | 46 | private function searchElement(callable $function) |
| 47 | 47 | { |
| 48 | - if($this->isElement() && ! $this->shouldBeSkipped()){ |
|
| 48 | + if ($this->isElement() && ! $this->shouldBeSkipped()) { |
|
| 49 | 49 | $function($this->extractElement($this->reader->name)); |
| 50 | 50 | } |
| 51 | 51 | } |
@@ -54,19 +54,19 @@ discard block |
||
| 54 | 54 | { |
| 55 | 55 | $elementCollection = (new Collection())->merge($this->getCurrentElementAttributes()); |
| 56 | 56 | |
| 57 | - while($this->reader->read()){ |
|
| 58 | - if($this->isEndElement($elementName)){ |
|
| 57 | + while ($this->reader->read()) { |
|
| 58 | + if ($this->isEndElement($elementName)) { |
|
| 59 | 59 | break; |
| 60 | 60 | } |
| 61 | - if($this->isValue()){ |
|
| 62 | - if($elementCollection->isEmpty()){ |
|
| 61 | + if ($this->isValue()) { |
|
| 62 | + if ($elementCollection->isEmpty()) { |
|
| 63 | 63 | return trim($this->reader->value); |
| 64 | 64 | } else { |
| 65 | 65 | return $elementCollection->put($elementName, trim($this->reader->value)); |
| 66 | 66 | } |
| 67 | 67 | } |
| 68 | - if($this->isElement()){ |
|
| 69 | - if($couldBeAnElementsList){ |
|
| 68 | + if ($this->isElement()) { |
|
| 69 | + if ($couldBeAnElementsList) { |
|
| 70 | 70 | $foundElementName = $this->reader->name; |
| 71 | 71 | $elementCollection->push(new Collection($this->extractElement($foundElementName))); |
| 72 | 72 | } else { |
@@ -79,10 +79,10 @@ discard block |
||
| 79 | 79 | return $elementCollection; |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - private function getCurrentElementAttributes(){ |
|
| 82 | + private function getCurrentElementAttributes() { |
|
| 83 | 83 | $attributes = new Collection(); |
| 84 | - if($this->reader->hasAttributes) { |
|
| 85 | - while($this->reader->moveToNextAttribute()) { |
|
| 84 | + if ($this->reader->hasAttributes) { |
|
| 85 | + while ($this->reader->moveToNextAttribute()) { |
|
| 86 | 86 | $attributes->put($this->reader->name, $this->reader->value); |
| 87 | 87 | } |
| 88 | 88 | } |
@@ -99,13 +99,13 @@ discard block |
||
| 99 | 99 | |
| 100 | 100 | private function stop() |
| 101 | 101 | { |
| 102 | - if( ! $this->reader->close()){ |
|
| 102 | + if ( ! $this->reader->close()) { |
|
| 103 | 103 | throw new IncompleteParseException(); |
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - private function shouldBeSkipped(){ |
|
| 108 | - if($this->skipFirstElement){ |
|
| 107 | + private function shouldBeSkipped() { |
|
| 108 | + if ($this->skipFirstElement) { |
|
| 109 | 109 | $this->skipFirstElement = false; |
| 110 | 110 | return true; |
| 111 | 111 | } |
@@ -113,23 +113,23 @@ discard block |
||
| 113 | 113 | return false; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - private function isElement(String $elementName = null){ |
|
| 117 | - if($elementName){ |
|
| 116 | + private function isElement(String $elementName = null) { |
|
| 117 | + if ($elementName) { |
|
| 118 | 118 | return $this->reader->nodeType == XMLReader::ELEMENT && $this->reader->name === $elementName; |
| 119 | 119 | } else { |
| 120 | 120 | return $this->reader->nodeType == XMLReader::ELEMENT; |
| 121 | 121 | } |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - private function isEndElement(String $elementName = null){ |
|
| 125 | - if($elementName){ |
|
| 124 | + private function isEndElement(String $elementName = null) { |
|
| 125 | + if ($elementName) { |
|
| 126 | 126 | return $this->reader->nodeType == XMLReader::END_ELEMENT && $this->reader->name === $elementName; |
| 127 | 127 | } else { |
| 128 | 128 | return $this->reader->nodeType == XMLReader::END_ELEMENT; |
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - private function isValue(){ |
|
| 132 | + private function isValue() { |
|
| 133 | 133 | return $this->reader->nodeType == XMLReader::TEXT || $this->reader->nodeType === XMLReader::CDATA; |
| 134 | 134 | } |
| 135 | 135 | } |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | throw $e; |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - if( ! fclose($stream)){ |
|
| 46 | + if ( ! fclose($stream)) { |
|
| 47 | 47 | throw new IncompleteParseException(); |
| 48 | 48 | } |
| 49 | 49 | } |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | { |
| 59 | 59 | $stream = @fopen($filePath, 'r'); |
| 60 | 60 | if (false === $stream) { |
| 61 | - throw new \Exception('Unable to open file for read: ' . $filePath); |
|
| 61 | + throw new \Exception('Unable to open file for read: '.$filePath); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | return $stream; |