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