1 | <?php |
||
17 | class FileReader implements ReaderInterface, LoggerAwareInterface |
||
18 | { |
||
19 | use OptionalLoggerTrait; |
||
20 | |||
21 | /** @var FileNodeInterface */ |
||
22 | private $file; |
||
23 | /** @var FormatInterface */ |
||
24 | private $format; |
||
25 | /** @var StreamReader */ |
||
26 | private $reader; |
||
27 | |||
28 | /** |
||
29 | * FileReader constructor. |
||
30 | * |
||
31 | * @param FileNodeInterface $file |
||
32 | * @param FormatInterface|null $format |
||
33 | * @param ParserFactoryInterface|null $parserFactory |
||
34 | */ |
||
35 | 7 | public function __construct( |
|
36 | FileNodeInterface $file, |
||
37 | FormatInterface $format = null, |
||
38 | ParserFactoryInterface $parserFactory = null |
||
39 | ) { |
||
40 | 7 | $this->file = $file; |
|
41 | 7 | $this->format = $format; |
|
42 | |||
43 | 7 | if ($this->file instanceof NodeStreamInterface) { |
|
44 | 6 | $stream = $this->file->getStream('r'); |
|
45 | } else { |
||
46 | 1 | $stream = new Stream($this->file->readStream()); |
|
47 | } |
||
48 | |||
49 | 7 | if (is_null($this->format) |
|
50 | 7 | && $file instanceof FormatAwareInterface |
|
51 | ) { |
||
52 | 1 | $this->format = $file->getFormat(); |
|
53 | } |
||
54 | |||
55 | 7 | if (is_null($this->format)) { |
|
56 | 1 | throw new InvalidArgumentException("No format could be determined from \$file or \$format"); |
|
57 | } |
||
58 | |||
59 | 6 | $factory = $parserFactory ?: new ParserFactory(); |
|
60 | 6 | $parser = $factory->getParser($this->format); |
|
61 | |||
62 | 6 | $this->reader = new StreamReader($stream, $parser); |
|
63 | 6 | } |
|
64 | |||
65 | /** |
||
66 | * Fetch the next row from a result set |
||
67 | * |
||
68 | * @param callable|null $callable a callable function to be applied to each Iterator item |
||
69 | * |
||
70 | * @return Iterator |
||
71 | */ |
||
72 | 1 | public function fetch(callable $callable = null) |
|
76 | |||
77 | /** |
||
78 | * Returns a sequential array of all items |
||
79 | * |
||
80 | * The callable function will be applied to each Iterator item |
||
81 | * |
||
82 | * @param callable|null $callable a callable function |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | 1 | public function fetchAll(callable $callable = null) |
|
90 | } |
||
91 |