@@ -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 |
@@ -43,7 +43,7 @@ |
||
| 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 | } |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | class XMLParser implements StreamParserInterface |
| 20 | 20 | { |
| 21 | - protected $reader,$source; |
|
| 21 | + protected $reader, $source; |
|
| 22 | 22 | |
| 23 | 23 | protected $skipFirstElement = true; |
| 24 | 24 | |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | return $this; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - public function withoutSkippingFirstElement(){ |
|
| 32 | + public function withoutSkippingFirstElement() { |
|
| 33 | 33 | $this->skipFirstElement = false; |
| 34 | 34 | |
| 35 | 35 | return $this; |
@@ -39,8 +39,8 @@ discard block |
||
| 39 | 39 | { |
| 40 | 40 | $this->start(); |
| 41 | 41 | try { |
| 42 | - while($this->reader->read()){ |
|
| 43 | - if($this->searchElement($function) === false) { |
|
| 42 | + while ($this->reader->read()) { |
|
| 43 | + if ($this->searchElement($function) === false) { |
|
| 44 | 44 | break; |
| 45 | 45 | } |
| 46 | 46 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | |
| 53 | 53 | public function chunk($count, callable $function) |
| 54 | 54 | { |
| 55 | - if($count <= 0) { |
|
| 55 | + if ($count <= 0) { |
|
| 56 | 56 | return; |
| 57 | 57 | } |
| 58 | 58 | |
@@ -60,23 +60,23 @@ discard block |
||
| 60 | 60 | try { |
| 61 | 61 | $chunk = new Collection(); |
| 62 | 62 | |
| 63 | - while($this->reader->read()){ |
|
| 63 | + while ($this->reader->read()) { |
|
| 64 | 64 | $this->searchElement(function($item) use (&$chunk) { |
| 65 | 65 | $chunk->push($item); |
| 66 | 66 | }); |
| 67 | 67 | |
| 68 | - if($chunk->count() >= $count) { |
|
| 68 | + if ($chunk->count() >= $count) { |
|
| 69 | 69 | $stop = $function($chunk) === false; |
| 70 | 70 | |
| 71 | 71 | $chunk = new Collection(); |
| 72 | 72 | |
| 73 | - if($stop) { |
|
| 73 | + if ($stop) { |
|
| 74 | 74 | break; |
| 75 | 75 | } |
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - if($chunk->count() > 0) { |
|
| 79 | + if ($chunk->count() > 0) { |
|
| 80 | 80 | $function($chunk); |
| 81 | 81 | } |
| 82 | 82 | } catch (StopParseException $e) { |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | |
| 88 | 88 | private function searchElement(callable $function) |
| 89 | 89 | { |
| 90 | - if($this->isElement() && ! $this->shouldBeSkipped()){ |
|
| 90 | + if ($this->isElement() && ! $this->shouldBeSkipped()) { |
|
| 91 | 91 | return $function($this->extractElement($this->reader->name)); |
| 92 | 92 | } |
| 93 | 93 | } |
@@ -96,19 +96,19 @@ discard block |
||
| 96 | 96 | { |
| 97 | 97 | $elementCollection = (new Collection())->merge($this->getCurrentElementAttributes()); |
| 98 | 98 | |
| 99 | - while($this->reader->read()){ |
|
| 100 | - if($this->isEndElement($elementName)){ |
|
| 99 | + while ($this->reader->read()) { |
|
| 100 | + if ($this->isEndElement($elementName)) { |
|
| 101 | 101 | break; |
| 102 | 102 | } |
| 103 | - if($this->isValue()){ |
|
| 104 | - if($elementCollection->isEmpty()){ |
|
| 103 | + if ($this->isValue()) { |
|
| 104 | + if ($elementCollection->isEmpty()) { |
|
| 105 | 105 | return trim($this->reader->value); |
| 106 | 106 | } else { |
| 107 | 107 | return $elementCollection->put($elementName, trim($this->reader->value)); |
| 108 | 108 | } |
| 109 | 109 | } |
| 110 | - if($this->isElement()){ |
|
| 111 | - if($couldBeAnElementsList){ |
|
| 110 | + if ($this->isElement()) { |
|
| 111 | + if ($couldBeAnElementsList) { |
|
| 112 | 112 | $foundElementName = $this->reader->name; |
| 113 | 113 | $elementCollection->push(new Collection($this->extractElement($foundElementName))); |
| 114 | 114 | } else { |
@@ -121,10 +121,10 @@ discard block |
||
| 121 | 121 | return $elementCollection; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - private function getCurrentElementAttributes(){ |
|
| 124 | + private function getCurrentElementAttributes() { |
|
| 125 | 125 | $attributes = new Collection(); |
| 126 | - if($this->reader->hasAttributes) { |
|
| 127 | - while($this->reader->moveToNextAttribute()) { |
|
| 126 | + if ($this->reader->hasAttributes) { |
|
| 127 | + while ($this->reader->moveToNextAttribute()) { |
|
| 128 | 128 | $attributes->put($this->reader->name, $this->reader->value); |
| 129 | 129 | } |
| 130 | 130 | } |
@@ -141,13 +141,13 @@ discard block |
||
| 141 | 141 | |
| 142 | 142 | private function stop() |
| 143 | 143 | { |
| 144 | - if( ! $this->reader->close()){ |
|
| 144 | + if ( ! $this->reader->close()) { |
|
| 145 | 145 | throw new IncompleteParseException(); |
| 146 | 146 | } |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | - private function shouldBeSkipped(){ |
|
| 150 | - if($this->skipFirstElement){ |
|
| 149 | + private function shouldBeSkipped() { |
|
| 150 | + if ($this->skipFirstElement) { |
|
| 151 | 151 | $this->skipFirstElement = false; |
| 152 | 152 | return true; |
| 153 | 153 | } |
@@ -155,23 +155,23 @@ discard block |
||
| 155 | 155 | return false; |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - private function isElement(String $elementName = null){ |
|
| 159 | - if($elementName){ |
|
| 158 | + private function isElement(String $elementName = null) { |
|
| 159 | + if ($elementName) { |
|
| 160 | 160 | return $this->reader->nodeType == XMLReader::ELEMENT && $this->reader->name === $elementName; |
| 161 | 161 | } else { |
| 162 | 162 | return $this->reader->nodeType == XMLReader::ELEMENT; |
| 163 | 163 | } |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - private function isEndElement(String $elementName = null){ |
|
| 167 | - if($elementName){ |
|
| 166 | + private function isEndElement(String $elementName = null) { |
|
| 167 | + if ($elementName) { |
|
| 168 | 168 | return $this->reader->nodeType == XMLReader::END_ELEMENT && $this->reader->name === $elementName; |
| 169 | 169 | } else { |
| 170 | 170 | return $this->reader->nodeType == XMLReader::END_ELEMENT; |
| 171 | 171 | } |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - private function isValue(){ |
|
| 174 | + private function isValue() { |
|
| 175 | 175 | return $this->reader->nodeType == XMLReader::TEXT || $this->reader->nodeType === XMLReader::CDATA; |
| 176 | 176 | } |
| 177 | 177 | } |
@@ -18,12 +18,12 @@ discard block |
||
| 18 | 18 | { |
| 19 | 19 | protected $reader, $source, $headers, $currentLine; |
| 20 | 20 | |
| 21 | - public static $delimiters = [",", ";"]; |
|
| 21 | + public static $delimiters = [ ",", ";" ]; |
|
| 22 | 22 | |
| 23 | 23 | public function __construct() |
| 24 | 24 | { |
| 25 | - Collection::macro('recursive', function () { |
|
| 26 | - return $this->map(function ($value) { |
|
| 25 | + Collection::macro('recursive', function() { |
|
| 26 | + return $this->map(function($value) { |
|
| 27 | 27 | if (is_array($value) || is_object($value)) { |
| 28 | 28 | return (new Collection($value))->recursive(); |
| 29 | 29 | } |
@@ -43,8 +43,8 @@ discard block |
||
| 43 | 43 | { |
| 44 | 44 | $this->start(); |
| 45 | 45 | try { |
| 46 | - while($this->read()){ |
|
| 47 | - if($function($this->getCurrentLineAsCollection()) === false) { |
|
| 46 | + while ($this->read()) { |
|
| 47 | + if ($function($this->getCurrentLineAsCollection()) === false) { |
|
| 48 | 48 | break; |
| 49 | 49 | } |
| 50 | 50 | } |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | |
| 57 | 57 | public function chunk($count, callable $function) |
| 58 | 58 | { |
| 59 | - if($count <= 0) { |
|
| 59 | + if ($count <= 0) { |
|
| 60 | 60 | return; |
| 61 | 61 | } |
| 62 | 62 | |
@@ -64,21 +64,21 @@ discard block |
||
| 64 | 64 | try { |
| 65 | 65 | $chunk = new Collection(); |
| 66 | 66 | |
| 67 | - while($this->read()){ |
|
| 67 | + while ($this->read()) { |
|
| 68 | 68 | $chunk->push($this->getCurrentLineAsCollection()); |
| 69 | 69 | |
| 70 | - if($chunk->count() >= $count) { |
|
| 70 | + if ($chunk->count() >= $count) { |
|
| 71 | 71 | $stop = $function($chunk) === false; |
| 72 | 72 | |
| 73 | 73 | $chunk = new Collection(); |
| 74 | 74 | |
| 75 | - if($stop) { |
|
| 75 | + if ($stop) { |
|
| 76 | 76 | break; |
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - if($chunk->count() > 0) { |
|
| 81 | + if ($chunk->count() > 0) { |
|
| 82 | 82 | $function($chunk); |
| 83 | 83 | } |
| 84 | 84 | } catch (StopParseException $e) { |
@@ -97,8 +97,8 @@ discard block |
||
| 97 | 97 | return $this; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - private function close(){ |
|
| 101 | - if( ! fclose($this->reader)){ |
|
| 100 | + private function close() { |
|
| 101 | + if ( ! fclose($this->reader)) { |
|
| 102 | 102 | throw new IncompleteParseException(); |
| 103 | 103 | } |
| 104 | 104 | } |
@@ -116,20 +116,20 @@ discard block |
||
| 116 | 116 | return $headers->intersectByKeys($this->currentLine)->combine($values->recursive()); |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - private function formatCurrentLineValues(Collection $collection){ |
|
| 119 | + private function formatCurrentLineValues(Collection $collection) { |
|
| 120 | 120 | $this->explodeCollectionValues($collection); |
| 121 | 121 | return $collection; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - private function explodeCollectionValues(Collection $collection){ |
|
| 125 | - $collection->transform(function($value){ |
|
| 124 | + private function explodeCollectionValues(Collection $collection) { |
|
| 125 | + $collection->transform(function($value) { |
|
| 126 | 126 | (new Collection(static::$delimiters))->each(function($delimiter) use (&$value){ |
| 127 | - if( ! is_array($value) && strpos($value, $delimiter) !== false){ |
|
| 127 | + if ( ! is_array($value) && strpos($value, $delimiter) !== false) { |
|
| 128 | 128 | $value = explode($delimiter, $value); |
| 129 | 129 | } |
| 130 | 130 | }); |
| 131 | - if(is_array($value)){ |
|
| 132 | - return (new Collection($value))->reject(function($value){ |
|
| 131 | + if (is_array($value)) { |
|
| 132 | + return (new Collection($value))->reject(function($value) { |
|
| 133 | 133 | return empty($value); |
| 134 | 134 | }); |
| 135 | 135 | } else { |
@@ -20,8 +20,8 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | public function __construct() |
| 22 | 22 | { |
| 23 | - Collection::macro('recursive', function () { |
|
| 24 | - return $this->map(function ($value) { |
|
| 23 | + Collection::macro('recursive', function() { |
|
| 24 | + return $this->map(function($value) { |
|
| 25 | 25 | if (is_array($value) || is_object($value)) { |
| 26 | 26 | return (new Collection($value))->recursive(); |
| 27 | 27 | } |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | $this->start(); |
| 43 | 43 | try { |
| 44 | 44 | $this->reader->parse($this->source, function(array $item) use ($function){ |
| 45 | - if($function((new Collection($item))->recursive()) === false) { |
|
| 45 | + if ($function((new Collection($item))->recursive()) === false) { |
|
| 46 | 46 | throw new StopParseException(); |
| 47 | 47 | } |
| 48 | 48 | }); |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | |
| 53 | 53 | public function chunk($count, callable $function) |
| 54 | 54 | { |
| 55 | - if($count <= 0) { |
|
| 55 | + if ($count <= 0) { |
|
| 56 | 56 | return; |
| 57 | 57 | } |
| 58 | 58 | |
@@ -63,18 +63,18 @@ discard block |
||
| 63 | 63 | $this->reader->parse($this->source, function(array $item) use ($function, &$chunk, &$count){ |
| 64 | 64 | $chunk->push((new Collection($item))->recursive()); |
| 65 | 65 | |
| 66 | - if($chunk->count() >= $count) { |
|
| 66 | + if ($chunk->count() >= $count) { |
|
| 67 | 67 | $stop = $function($chunk) === false; |
| 68 | 68 | |
| 69 | 69 | $chunk = new Collection(); |
| 70 | 70 | |
| 71 | - if($stop) { |
|
| 71 | + if ($stop) { |
|
| 72 | 72 | throw new StopParseException(); |
| 73 | 73 | } |
| 74 | 74 | } |
| 75 | 75 | }); |
| 76 | 76 | |
| 77 | - if($chunk->count() > 0) { |
|
| 77 | + if ($chunk->count() > 0) { |
|
| 78 | 78 | $function($chunk); |
| 79 | 79 | } |
| 80 | 80 | } catch (StopParseException $e) { |