Completed
Pull Request — master (#3)
by Harry
07:29
created

StreamReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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
0 ignored issues
show
Documentation introduced by
Should the type for parameter $callable not be null|callable?

This check looks for @param annotations 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.

Loading history...
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