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 Graze\DataFile\Format\FormatAwareInterface; |
17
|
|
|
use Graze\DataFile\Format\FormatInterface; |
18
|
|
|
use Graze\DataFile\Format\Parser\ParserFactory; |
19
|
|
|
use Graze\DataFile\Format\Parser\ParserFactoryInterface; |
20
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
21
|
|
|
use Graze\DataFile\Node\NodeStreamInterface; |
22
|
|
|
use GuzzleHttp\Psr7\Stream; |
23
|
|
|
use InvalidArgumentException; |
24
|
|
|
|
25
|
|
|
class FileReader extends StreamReader |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* FileReader constructor. |
29
|
|
|
* |
30
|
|
|
* @param FileNodeInterface $file |
31
|
|
|
* @param FormatInterface|null $format |
32
|
|
|
* @param ParserFactoryInterface|null $parserFactory |
33
|
|
|
*/ |
34
|
8 |
|
public function __construct( |
35
|
|
|
FileNodeInterface $file, |
36
|
|
|
FormatInterface $format = null, |
37
|
|
|
ParserFactoryInterface $parserFactory = null |
38
|
|
|
) { |
39
|
8 |
|
if ($file instanceof NodeStreamInterface) { |
40
|
7 |
|
$stream = $file->getStream('r'); |
41
|
|
|
} else { |
42
|
1 |
|
$stream = new Stream($file->readStream()); |
43
|
|
|
} |
44
|
|
|
|
45
|
8 |
|
if (is_null($format) |
46
|
8 |
|
&& $file instanceof FormatAwareInterface |
47
|
|
|
) { |
48
|
2 |
|
$format = $file->getFormat(); |
49
|
|
|
} |
50
|
|
|
|
51
|
8 |
|
if (is_null($format)) { |
52
|
1 |
|
throw new InvalidArgumentException("No format could be determined from \$file or \$format"); |
53
|
|
|
} |
54
|
|
|
|
55
|
7 |
|
$factory = $parserFactory ?: new ParserFactory(); |
56
|
7 |
|
$parser = $factory->getParser($format); |
57
|
|
|
|
58
|
7 |
|
parent::__construct($stream, $parser); |
59
|
7 |
|
} |
60
|
|
|
} |
61
|
|
|
|