@@ -13,87 +13,87 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ChunkExtension implements \Rodenastyle\StreamParser\StreamParserInterface |
| 15 | 15 | { |
| 16 | - const DEFAULT_CHUNK_SIZE = 5000; |
|
| 16 | + const DEFAULT_CHUNK_SIZE = 5000; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var \Rodenastyle\StreamParser\StreamParserInterface |
|
| 20 | - */ |
|
| 21 | - private $parser; |
|
| 22 | - private $iterationCount = 0; |
|
| 23 | - private $chunkSize; |
|
| 24 | - /** @var callable $onChunkCompleteCallback */ |
|
| 25 | - private $onChunkCompleteCallback; |
|
| 18 | + /** |
|
| 19 | + * @var \Rodenastyle\StreamParser\StreamParserInterface |
|
| 20 | + */ |
|
| 21 | + private $parser; |
|
| 22 | + private $iterationCount = 0; |
|
| 23 | + private $chunkSize; |
|
| 24 | + /** @var callable $onChunkCompleteCallback */ |
|
| 25 | + private $onChunkCompleteCallback; |
|
| 26 | 26 | |
| 27 | - public function __construct(\Rodenastyle\StreamParser\StreamParserInterface $parser, $chunkSize = self::DEFAULT_CHUNK_SIZE) |
|
| 28 | - { |
|
| 29 | - $this->parser = $parser; |
|
| 30 | - $this->chunkSize = $chunkSize; |
|
| 31 | - } |
|
| 27 | + public function __construct(\Rodenastyle\StreamParser\StreamParserInterface $parser, $chunkSize = self::DEFAULT_CHUNK_SIZE) |
|
| 28 | + { |
|
| 29 | + $this->parser = $parser; |
|
| 30 | + $this->chunkSize = $chunkSize; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - public function from(String $source): \Rodenastyle\StreamParser\StreamParserInterface |
|
| 34 | - { |
|
| 35 | - return $this->parser->from($source); |
|
| 36 | - } |
|
| 33 | + public function from(String $source): \Rodenastyle\StreamParser\StreamParserInterface |
|
| 34 | + { |
|
| 35 | + return $this->parser->from($source); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - public function eachChunk(callable $function): StreamParserInterface |
|
| 39 | - { |
|
| 40 | - $this->onChunkCompleteCallback = $function; |
|
| 41 | - return $this; |
|
| 42 | - } |
|
| 38 | + public function eachChunk(callable $function): StreamParserInterface |
|
| 39 | + { |
|
| 40 | + $this->onChunkCompleteCallback = $function; |
|
| 41 | + return $this; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - public function each(callable $function) |
|
| 45 | - { |
|
| 46 | - if ($this->onChunkCompleteCallback === null) { |
|
| 47 | - throw new UndefinedCallbackException("No callback is set on chunk complete"); |
|
| 48 | - } |
|
| 49 | - // decorate function to apply both its behavior and the chunk process |
|
| 50 | - $function = function ($collection) use ($function) { |
|
| 51 | - $this->iterationCount++; |
|
| 52 | - $function($collection); |
|
| 44 | + public function each(callable $function) |
|
| 45 | + { |
|
| 46 | + if ($this->onChunkCompleteCallback === null) { |
|
| 47 | + throw new UndefinedCallbackException("No callback is set on chunk complete"); |
|
| 48 | + } |
|
| 49 | + // decorate function to apply both its behavior and the chunk process |
|
| 50 | + $function = function ($collection) use ($function) { |
|
| 51 | + $this->iterationCount++; |
|
| 52 | + $function($collection); |
|
| 53 | 53 | |
| 54 | - if ($this->iterationCount % $this->chunkSize === 0) { |
|
| 55 | - $this->executeChunkCompleteCallback(); |
|
| 56 | - } |
|
| 57 | - }; |
|
| 58 | - $this->parser->each($function); |
|
| 54 | + if ($this->iterationCount % $this->chunkSize === 0) { |
|
| 55 | + $this->executeChunkCompleteCallback(); |
|
| 56 | + } |
|
| 57 | + }; |
|
| 58 | + $this->parser->each($function); |
|
| 59 | 59 | |
| 60 | - // make sure no entry is left out |
|
| 61 | - $this->processLastEntries(); |
|
| 62 | - return $this; |
|
| 63 | - } |
|
| 60 | + // make sure no entry is left out |
|
| 61 | + $this->processLastEntries(); |
|
| 62 | + return $this; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * @param $this |
|
| 67 | - */ |
|
| 68 | - protected function executeChunkCompleteCallback() |
|
| 69 | - { |
|
| 70 | - $this->iterationCount = 0; |
|
| 71 | - ($this->onChunkCompleteCallback)(); |
|
| 72 | - } |
|
| 65 | + /** |
|
| 66 | + * @param $this |
|
| 67 | + */ |
|
| 68 | + protected function executeChunkCompleteCallback() |
|
| 69 | + { |
|
| 70 | + $this->iterationCount = 0; |
|
| 71 | + ($this->onChunkCompleteCallback)(); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - protected function processLastEntries() |
|
| 75 | - { |
|
| 76 | - if ($this->iterationCount % $this->chunkSize !== 0) { |
|
| 77 | - $this->executeChunkCompleteCallback(); |
|
| 78 | - } |
|
| 79 | - } |
|
| 74 | + protected function processLastEntries() |
|
| 75 | + { |
|
| 76 | + if ($this->iterationCount % $this->chunkSize !== 0) { |
|
| 77 | + $this->executeChunkCompleteCallback(); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * @param int $chunkSize |
|
| 84 | - * @return ChunkExtension |
|
| 85 | - */ |
|
| 86 | - public function setChunkSize(int $chunkSize): ChunkExtension |
|
| 87 | - { |
|
| 88 | - $this->chunkSize = $chunkSize; |
|
| 89 | - return $this; |
|
| 90 | - } |
|
| 82 | + /** |
|
| 83 | + * @param int $chunkSize |
|
| 84 | + * @return ChunkExtension |
|
| 85 | + */ |
|
| 86 | + public function setChunkSize(int $chunkSize): ChunkExtension |
|
| 87 | + { |
|
| 88 | + $this->chunkSize = $chunkSize; |
|
| 89 | + return $this; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * @return \Rodenastyle\StreamParser\StreamParserInterface |
|
| 94 | - */ |
|
| 95 | - public function getParser(): \Rodenastyle\StreamParser\StreamParserInterface |
|
| 96 | - { |
|
| 97 | - return $this->parser; |
|
| 98 | - } |
|
| 92 | + /** |
|
| 93 | + * @return \Rodenastyle\StreamParser\StreamParserInterface |
|
| 94 | + */ |
|
| 95 | + public function getParser(): \Rodenastyle\StreamParser\StreamParserInterface |
|
| 96 | + { |
|
| 97 | + return $this->parser; |
|
| 98 | + } |
|
| 99 | 99 | } |
| 100 | 100 | \ No newline at end of file |
@@ -44,8 +44,8 @@ |
||
| 44 | 44 | return (new CSVParser())->from($source); |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - private function chunk(StreamParserInterface $parser) |
|
| 48 | - { |
|
| 49 | - return new ChunkExtension($parser); |
|
| 47 | + private function chunk(StreamParserInterface $parser) |
|
| 48 | + { |
|
| 49 | + return new ChunkExtension($parser); |
|
| 50 | 50 | } |
| 51 | 51 | } |
| 52 | 52 | \ No newline at end of file |