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\Formatter\FormatterFactory; |
19
|
|
|
use Graze\DataFile\Format\Formatter\FormatterFactoryInterface; |
20
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
21
|
|
|
use Graze\DataFile\Node\NodeStreamInterface; |
22
|
|
|
use InvalidArgumentException; |
23
|
|
|
|
24
|
|
|
class FileWriter extends StreamWriter |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* FileReader constructor. |
28
|
|
|
* |
29
|
|
|
* @param FileNodeInterface $file |
30
|
|
|
* @param FormatInterface|null $format |
31
|
|
|
* @param FormatterFactoryInterface|null $formatterFactory |
32
|
|
|
*/ |
33
|
8 |
|
public function __construct( |
34
|
|
|
FileNodeInterface $file, |
35
|
|
|
FormatInterface $format = null, |
36
|
|
|
FormatterFactoryInterface $formatterFactory = null |
37
|
|
|
) { |
38
|
8 |
|
if ($file instanceof NodeStreamInterface) { |
39
|
7 |
|
$stream = $file->getStream('c+b'); |
40
|
|
|
} else { |
41
|
1 |
|
throw new InvalidArgumentException( |
42
|
1 |
|
"Only files that implement " . NodeStreamInterface::class . "can be written to" |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
7 |
|
if (is_null($format) |
47
|
7 |
|
&& $file instanceof FormatAwareInterface |
48
|
|
|
) { |
49
|
2 |
|
$format = $file->getFormat(); |
50
|
|
|
} |
51
|
|
|
|
52
|
7 |
|
if (is_null($format)) { |
53
|
1 |
|
throw new InvalidArgumentException("No format could be determined from \$file or \$format"); |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
$factory = $formatterFactory ?: new FormatterFactory(); |
57
|
6 |
|
$formatter = $factory->getFormatter($format); |
58
|
|
|
|
59
|
6 |
|
parent::__construct($stream, $formatter); |
60
|
6 |
|
} |
61
|
|
|
} |
62
|
|
|
|