|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: sergio.rodenas |
|
5
|
|
|
* Date: 14/5/18 |
|
6
|
|
|
* Time: 11:02 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Rodenastyle\StreamParser\Parsers; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Rodenastyle\StreamParser\Exceptions\StopParseException; |
|
13
|
|
|
use Rodenastyle\StreamParser\Services\JsonCollectionParser as Parser; |
|
14
|
|
|
use Rodenastyle\StreamParser\StreamParserInterface; |
|
15
|
|
|
use Tightenco\Collect\Support\Collection; |
|
16
|
|
|
|
|
17
|
|
|
class JSONParser implements StreamParserInterface |
|
18
|
|
|
{ |
|
19
|
|
|
protected $reader, $source; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
Collection::macro('recursive', function () { |
|
24
|
5 |
|
return $this->map(function ($value) { |
|
|
|
|
|
|
25
|
5 |
|
if (is_array($value) || is_object($value)) { |
|
26
|
5 |
|
return (new Collection($value))->recursive(); |
|
27
|
|
|
} |
|
28
|
5 |
|
return $value; |
|
29
|
5 |
|
}); |
|
30
|
5 |
|
}); |
|
31
|
5 |
|
} |
|
32
|
|
|
|
|
33
|
5 |
|
public function from(String $source): StreamParserInterface |
|
34
|
|
|
{ |
|
35
|
5 |
|
$this->source = $source; |
|
36
|
|
|
|
|
37
|
5 |
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
public function each(callable $function) |
|
41
|
|
|
{ |
|
42
|
5 |
|
$this->start(); |
|
43
|
|
|
try { |
|
44
|
5 |
|
$this->reader->parse($this->source, function(array $item) use ($function){ |
|
45
|
5 |
|
if($function((new Collection($item))->recursive()) === false) { |
|
46
|
1 |
|
throw new StopParseException(); |
|
47
|
|
|
} |
|
48
|
5 |
|
}); |
|
49
|
1 |
|
} catch (StopParseException $e) { |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
5 |
|
} |
|
52
|
|
|
|
|
53
|
5 |
|
public function chunk($count, callable $function) |
|
54
|
|
|
{ |
|
55
|
5 |
|
if($count <= 0) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
5 |
|
$this->start(); |
|
60
|
|
|
try { |
|
61
|
5 |
|
$chunk = new Collection(); |
|
62
|
|
|
|
|
63
|
5 |
|
$this->reader->parse($this->source, function(array $item) use ($function, &$chunk, &$count){ |
|
64
|
5 |
|
$chunk->push((new Collection($item))->recursive()); |
|
65
|
|
|
|
|
66
|
5 |
|
if($chunk->count() >= $count) { |
|
67
|
5 |
|
$stop = $function($chunk) === false; |
|
68
|
|
|
|
|
69
|
5 |
|
$chunk = new Collection(); |
|
70
|
|
|
|
|
71
|
5 |
|
if($stop) { |
|
72
|
1 |
|
throw new StopParseException(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
5 |
|
}); |
|
76
|
|
|
|
|
77
|
4 |
|
if($chunk->count() > 0) { |
|
78
|
4 |
|
$function($chunk); |
|
79
|
|
|
} |
|
80
|
1 |
|
} catch (StopParseException $e) { |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
5 |
|
} |
|
83
|
|
|
|
|
84
|
5 |
|
private function start() |
|
85
|
|
|
{ |
|
86
|
5 |
|
$this->reader = new Parser(); |
|
87
|
|
|
|
|
88
|
5 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.