|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/data-file |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/data-file |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\DataFile\IO; |
|
15
|
|
|
|
|
16
|
|
|
use CallbackFilterIterator; |
|
17
|
|
|
use Graze\DataFile\Format\Parser\ParserInterface; |
|
18
|
|
|
use Graze\DataFile\Helper\OptionalLoggerTrait; |
|
19
|
|
|
use Iterator; |
|
20
|
|
|
use Psr\Http\Message\StreamInterface; |
|
21
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
22
|
|
|
|
|
23
|
|
|
class StreamReader implements ReaderInterface, LoggerAwareInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use OptionalLoggerTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** @var StreamInterface */ |
|
28
|
|
|
private $stream; |
|
29
|
|
|
/** @var ParserInterface */ |
|
30
|
|
|
private $parser; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param StreamInterface $stream |
|
34
|
|
|
* @param ParserInterface $parser |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(StreamInterface $stream, ParserInterface $parser) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->stream = $stream; |
|
39
|
|
|
$this->parser = $parser; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Create an Iterator based on |
|
44
|
|
|
* |
|
45
|
|
|
* @param callable $callable |
|
|
|
|
|
|
46
|
|
|
* |
|
47
|
|
|
* @return Iterator |
|
48
|
|
|
*/ |
|
49
|
|
|
public function fetch(callable $callable = null) |
|
50
|
|
|
{ |
|
51
|
|
|
$iterator = $this->parser->parse($this->stream); |
|
52
|
|
|
if ($callable) { |
|
53
|
|
|
$iterator = new CallbackFilterIterator($iterator, $callable); |
|
54
|
|
|
} |
|
55
|
|
|
return $iterator; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns a sequential array of all items |
|
60
|
|
|
* |
|
61
|
|
|
* The callable function will be applied to each Iterator item |
|
62
|
|
|
* |
|
63
|
|
|
* @param callable|null $callable a callable function |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public function fetchAll(callable $callable = null) |
|
68
|
|
|
{ |
|
69
|
|
|
return iterator_to_array($this->fetch($callable)); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.