@@ -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; |
@@ -21,8 +21,8 @@ discard block |
||
21 | 21 | |
22 | 22 | private 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 | } |
@@ -31,15 +31,15 @@ discard block |
||
31 | 31 | }); |
32 | 32 | } |
33 | 33 | |
34 | - private function xml(String $source){ |
|
34 | + private function xml(String $source) { |
|
35 | 35 | return (new XMLParser())->from($source); |
36 | 36 | } |
37 | 37 | |
38 | - private function json(String $source){ |
|
38 | + private function json(String $source) { |
|
39 | 39 | return (new JSONParser())->from($source); |
40 | 40 | } |
41 | 41 | |
42 | - private function csv(String $source){ |
|
42 | + private function csv(String $source) { |
|
43 | 43 | return (new CSVParser())->from($source); |
44 | 44 | } |
45 | 45 | } |
46 | 46 | \ No newline at end of file |
@@ -66,9 +66,9 @@ |
||
66 | 66 | } |
67 | 67 | if($this->isValue()) { |
68 | 68 | if($elementCollection->isEmpty()) { |
69 | - if (!is_null($foundInEl)) { |
|
70 | - return $elementCollection->put($elementName, trim($this->reader->value)); |
|
71 | - } |
|
69 | + if (!is_null($foundInEl)) { |
|
70 | + return $elementCollection->put($elementName, trim($this->reader->value)); |
|
71 | + } |
|
72 | 72 | |
73 | 73 | return trim($this->reader->value); |
74 | 74 | } 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 | protected $separateParameters = false; |
@@ -29,13 +29,13 @@ discard block |
||
29 | 29 | return $this; |
30 | 30 | } |
31 | 31 | |
32 | - public function withSeparatedParametersList(){ |
|
32 | + public function withSeparatedParametersList() { |
|
33 | 33 | $this->separateParameters = true; |
34 | 34 | |
35 | 35 | return $this; |
36 | 36 | } |
37 | 37 | |
38 | - public function withoutSkippingFirstElement(){ |
|
38 | + public function withoutSkippingFirstElement() { |
|
39 | 39 | $this->skipFirstElement = false; |
40 | 40 | |
41 | 41 | return $this; |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | public function each(callable $function) |
45 | 45 | { |
46 | 46 | $this->start(); |
47 | - while($this->reader->read()){ |
|
47 | + while ($this->reader->read()) { |
|
48 | 48 | $this->searchElement($function); |
49 | 49 | } |
50 | 50 | $this->stop(); |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | |
53 | 53 | private function searchElement(callable $function) |
54 | 54 | { |
55 | - if($this->isElement() && ! $this->shouldBeSkipped()){ |
|
55 | + if ($this->isElement() && ! $this->shouldBeSkipped()) { |
|
56 | 56 | $function($this->extractElement($this->reader->name, false, $this->reader->depth), $this->reader->name); |
57 | 57 | } |
58 | 58 | } |
@@ -64,23 +64,23 @@ discard block |
||
64 | 64 | $elementCollection = new Collection(); |
65 | 65 | |
66 | 66 | $elementParameters = $this->getCurrentElementAttributes(); |
67 | - if($this->separateParameters){ |
|
67 | + if ($this->separateParameters) { |
|
68 | 68 | $elementCollection->put('__params', $elementParameters); |
69 | 69 | } else { |
70 | 70 | $elementCollection = $elementCollection->merge($elementParameters); |
71 | 71 | } |
72 | 72 | |
73 | - if($emptyElement) { |
|
73 | + if ($emptyElement) { |
|
74 | 74 | return $elementCollection; |
75 | 75 | } |
76 | 76 | |
77 | - while($this->reader->read()) { |
|
78 | - if($this->isEndElement($elementName) && $this->reader->depth === $parentDepth) { |
|
77 | + while ($this->reader->read()) { |
|
78 | + if ($this->isEndElement($elementName) && $this->reader->depth === $parentDepth) { |
|
79 | 79 | break; |
80 | 80 | } |
81 | - if($this->isValue()) { |
|
82 | - if($elementCollection->isEmpty()) { |
|
83 | - if (!is_null($foundInEl)) { |
|
81 | + if ($this->isValue()) { |
|
82 | + if ($elementCollection->isEmpty()) { |
|
83 | + if ( ! is_null($foundInEl)) { |
|
84 | 84 | return $elementCollection->put($elementName, trim($this->reader->value)); |
85 | 85 | } |
86 | 86 | |
@@ -89,8 +89,8 @@ discard block |
||
89 | 89 | return $elementCollection->put($elementName, trim($this->reader->value)); |
90 | 90 | } |
91 | 91 | } |
92 | - if($this->isElement()) { |
|
93 | - if($couldBeAnElementsList) { |
|
92 | + if ($this->isElement()) { |
|
93 | + if ($couldBeAnElementsList) { |
|
94 | 94 | $foundElementName = $this->reader->name; |
95 | 95 | $elementCollection->push(new Collection($this->extractElement($foundElementName, false, $this->reader->depth, $elementName))); |
96 | 96 | } else { |
@@ -103,10 +103,10 @@ discard block |
||
103 | 103 | return $elementCollection; |
104 | 104 | } |
105 | 105 | |
106 | - private function getCurrentElementAttributes(){ |
|
106 | + private function getCurrentElementAttributes() { |
|
107 | 107 | $attributes = new Collection(); |
108 | - if($this->reader->hasAttributes) { |
|
109 | - while($this->reader->moveToNextAttribute()) { |
|
108 | + if ($this->reader->hasAttributes) { |
|
109 | + while ($this->reader->moveToNextAttribute()) { |
|
110 | 110 | $attributes->put($this->reader->name, $this->reader->value); |
111 | 111 | } |
112 | 112 | } |
@@ -123,13 +123,13 @@ discard block |
||
123 | 123 | |
124 | 124 | private function stop() |
125 | 125 | { |
126 | - if( ! $this->reader->close()){ |
|
126 | + if ( ! $this->reader->close()) { |
|
127 | 127 | throw new IncompleteParseException(); |
128 | 128 | } |
129 | 129 | } |
130 | 130 | |
131 | - private function shouldBeSkipped(){ |
|
132 | - if($this->skipFirstElement){ |
|
131 | + private function shouldBeSkipped() { |
|
132 | + if ($this->skipFirstElement) { |
|
133 | 133 | $this->skipFirstElement = false; |
134 | 134 | return true; |
135 | 135 | } |
@@ -137,28 +137,28 @@ discard block |
||
137 | 137 | return false; |
138 | 138 | } |
139 | 139 | |
140 | - private function isElement(String $elementName = null){ |
|
141 | - if($elementName){ |
|
140 | + private function isElement(String $elementName = null) { |
|
141 | + if ($elementName) { |
|
142 | 142 | return $this->reader->nodeType == XMLReader::ELEMENT && $this->reader->name === $elementName; |
143 | 143 | } else { |
144 | 144 | return $this->reader->nodeType == XMLReader::ELEMENT; |
145 | 145 | } |
146 | 146 | } |
147 | 147 | |
148 | - private function isEndElement(String $elementName = null){ |
|
149 | - if($elementName){ |
|
148 | + private function isEndElement(String $elementName = null) { |
|
149 | + if ($elementName) { |
|
150 | 150 | return $this->reader->nodeType == XMLReader::END_ELEMENT && $this->reader->name === $elementName; |
151 | 151 | } else { |
152 | 152 | return $this->reader->nodeType == XMLReader::END_ELEMENT; |
153 | 153 | } |
154 | 154 | } |
155 | 155 | |
156 | - private function isValue(){ |
|
156 | + private function isValue() { |
|
157 | 157 | return $this->reader->nodeType == XMLReader::TEXT || $this->reader->nodeType === XMLReader::CDATA; |
158 | 158 | } |
159 | 159 | |
160 | - private function isEmptyElement(String $elementName = null){ |
|
161 | - if($elementName) { |
|
160 | + private function isEmptyElement(String $elementName = null) { |
|
161 | + if ($elementName) { |
|
162 | 162 | return $this->reader->isEmptyElement && $this->reader->name === $elementName; |
163 | 163 | } else { |
164 | 164 | return false; |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | { |
18 | 18 | protected $reader, $source, $headers, $currentLine; |
19 | 19 | |
20 | - public static $delimiters = [",", ";"]; |
|
20 | + public static $delimiters = [ ",", ";" ]; |
|
21 | 21 | public static $fieldDelimiter = ","; |
22 | 22 | public static $skipsEmptyLines = true; |
23 | 23 | |
@@ -31,8 +31,8 @@ discard block |
||
31 | 31 | public function each(callable $function) |
32 | 32 | { |
33 | 33 | $this->start(); |
34 | - while($this->read()){ |
|
35 | - if($this->currentLineIsValid()){ |
|
34 | + while ($this->read()) { |
|
35 | + if ($this->currentLineIsValid()) { |
|
36 | 36 | $function($this->getCurrentLineAsCollection()); |
37 | 37 | } |
38 | 38 | } |
@@ -49,8 +49,8 @@ discard block |
||
49 | 49 | return $this; |
50 | 50 | } |
51 | 51 | |
52 | - private function close(){ |
|
53 | - if( ! fclose($this->reader)){ |
|
52 | + private function close() { |
|
53 | + if ( ! fclose($this->reader)) { |
|
54 | 54 | throw new IncompleteParseException(); |
55 | 55 | } |
56 | 56 | } |
@@ -63,20 +63,20 @@ discard block |
||
63 | 63 | } |
64 | 64 | |
65 | 65 | private function currentLineIsValid(): bool{ |
66 | - if(static::$skipsEmptyLines){ |
|
66 | + if (static::$skipsEmptyLines) { |
|
67 | 67 | return ! $this->currentLineIsEmpty(); |
68 | 68 | } |
69 | 69 | |
70 | 70 | return true; |
71 | 71 | } |
72 | 72 | |
73 | - private function currentLineIsEmpty(){ |
|
74 | - return $this->currentLine->reject(function($value){ |
|
73 | + private function currentLineIsEmpty() { |
|
74 | + return $this->currentLine->reject(function($value) { |
|
75 | 75 | return $this->isEmptyValue($value); |
76 | 76 | })->isEmpty(); |
77 | 77 | } |
78 | 78 | |
79 | - private function isEmptyValue($value){ |
|
79 | + private function isEmptyValue($value) { |
|
80 | 80 | return $value === "" || $value === null; |
81 | 81 | } |
82 | 82 | |
@@ -87,25 +87,25 @@ discard block |
||
87 | 87 | |
88 | 88 | return $headers->intersectByKeys($this->currentLine) |
89 | 89 | ->combine($values->recursive()) |
90 | - ->reject(function($value){ |
|
90 | + ->reject(function($value) { |
|
91 | 91 | return $this->isEmptyValue($value); |
92 | 92 | }); |
93 | 93 | } |
94 | 94 | |
95 | - private function formatCurrentLineValues(Collection $collection){ |
|
95 | + private function formatCurrentLineValues(Collection $collection) { |
|
96 | 96 | $this->explodeCollectionValues($collection); |
97 | 97 | return $collection; |
98 | 98 | } |
99 | 99 | |
100 | - private function explodeCollectionValues(Collection $collection){ |
|
101 | - $collection->transform(function($value){ |
|
100 | + private function explodeCollectionValues(Collection $collection) { |
|
101 | + $collection->transform(function($value) { |
|
102 | 102 | (new Collection(static::$delimiters))->each(function($delimiter) use (&$value){ |
103 | - if( ! is_array($value) && strpos($value, $delimiter) !== false){ |
|
103 | + if ( ! is_array($value) && strpos($value, $delimiter) !== false) { |
|
104 | 104 | $value = explode($delimiter, $value); |
105 | 105 | } |
106 | 106 | }); |
107 | - if(is_array($value)){ |
|
108 | - return (new Collection($value))->reject(function($value){ |
|
107 | + if (is_array($value)) { |
|
108 | + return (new Collection($value))->reject(function($value) { |
|
109 | 109 | return $this->isEmptyValue($value); |
110 | 110 | }); |
111 | 111 | } else { |