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\Modify; |
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\Format\Parser\ParserFactory; |
21
|
|
|
use Graze\DataFile\Format\Parser\ParserFactoryInterface; |
22
|
|
|
use Graze\DataFile\Helper\Builder\BuilderAwareInterface; |
23
|
|
|
use Graze\DataFile\Helper\Builder\BuilderInterface; |
24
|
|
|
use Graze\DataFile\Helper\FileHelper; |
25
|
|
|
use Graze\DataFile\Helper\GetOptionTrait; |
26
|
|
|
use Graze\DataFile\Helper\OptionalLoggerTrait; |
27
|
|
|
use Graze\DataFile\IO\FileReader; |
28
|
|
|
use Graze\DataFile\IO\FileWriter; |
29
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
30
|
|
|
use Graze\DataFile\Node\LocalFileNodeInterface; |
31
|
|
|
use InvalidArgumentException; |
32
|
|
|
use Psr\Log\LoggerAwareInterface; |
33
|
|
|
|
34
|
|
|
class ReFormat implements FileModifierInterface, LoggerAwareInterface, BuilderAwareInterface |
35
|
|
|
{ |
36
|
|
|
use OptionalLoggerTrait; |
37
|
|
|
use GetOptionTrait; |
38
|
|
|
use FileProcessTrait; |
39
|
|
|
use FileHelper; |
40
|
|
|
|
41
|
|
|
/** @var FormatterFactoryInterface */ |
42
|
|
|
private $formatterFactory; |
43
|
|
|
/** @var ParserFactoryInterface */ |
44
|
|
|
private $parserFactory; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* ReFormat constructor. |
48
|
|
|
* |
49
|
|
|
* @param FormatterFactoryInterface|null $formatterFactory |
50
|
|
|
* @param ParserFactoryInterface|null $parserFactory |
51
|
|
|
* @param BuilderInterface|null $builder |
52
|
|
|
*/ |
53
|
11 |
|
public function __construct( |
54
|
|
|
FormatterFactoryInterface $formatterFactory = null, |
55
|
|
|
ParserFactoryInterface $parserFactory = null, |
56
|
|
|
BuilderInterface $builder = null |
57
|
|
|
) { |
58
|
11 |
|
$this->builder = $builder; |
59
|
11 |
|
$this->formatterFactory = $formatterFactory ?: $this->getBuilder()->build(FormatterFactory::class); |
60
|
11 |
|
$this->parserFactory = $parserFactory ?: $this->getBuilder()->build(ParserFactory::class); |
61
|
11 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Can this file be modified by this modifier |
65
|
|
|
* |
66
|
|
|
* @param FileNodeInterface $file |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
2 |
|
public function canModify(FileNodeInterface $file) |
71
|
|
|
{ |
72
|
2 |
|
return ($file->exists() |
73
|
2 |
|
&& $file instanceof FormatAwareInterface |
74
|
2 |
|
&& $file->getFormat() !== null |
75
|
2 |
|
&& $this->parserFactory->getParser($file->getFormat()) !== null); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Modify the file |
80
|
|
|
* |
81
|
|
|
* @param FileNodeInterface $file |
82
|
|
|
* @param array $options |
83
|
|
|
* |
84
|
|
|
* @return FileNodeInterface |
85
|
|
|
*/ |
86
|
3 |
|
public function modify(FileNodeInterface $file, array $options = []) |
87
|
|
|
{ |
88
|
3 |
|
$this->options = $options; |
89
|
|
|
|
90
|
3 |
|
$format = $this->getOption('format', null); |
91
|
3 |
|
$output = $this->getOption('output', null); |
92
|
3 |
|
if ((is_null($format) || (!$format instanceof FormatInterface)) |
93
|
3 |
|
&& (is_null($output) || (!$output instanceof LocalFileNodeInterface))) { |
94
|
1 |
|
throw new InvalidArgumentException("Missing a Required option: 'format' or 'output'"); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
return $this->reFormat($file, $format, $output); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param FileNodeInterface $file |
102
|
|
|
* @param FormatInterface|null $outputFormat |
103
|
|
|
* @param FileNodeInterface|null $output |
104
|
|
|
* @param FormatInterface|null $inputFormat |
105
|
|
|
* |
106
|
|
|
* @return FileNodeInterface |
107
|
|
|
*/ |
108
|
7 |
|
public function reFormat( |
109
|
|
|
FileNodeInterface $file, |
110
|
|
|
FormatInterface $outputFormat = null, |
111
|
|
|
FileNodeInterface $output = null, |
112
|
|
|
FormatInterface $inputFormat = null |
113
|
|
|
) { |
114
|
7 |
|
if (is_null($output)) { |
115
|
3 |
|
if ($file instanceof LocalFileNodeInterface) { |
116
|
2 |
|
$output = $this->getTargetFile($file, $this->getOption('postfix', 'format')); |
117
|
|
|
} else { |
118
|
1 |
|
$output = $this->getTemporaryFile(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
7 |
|
if ($output instanceof FormatAwareInterface |
123
|
7 |
|
&& $outputFormat != null |
124
|
|
|
) { |
125
|
3 |
|
$output->setFormat($outputFormat); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** @var FileReader $reader */ |
129
|
7 |
|
$reader = $this->getBuilder()->build(FileReader::class, $file, $inputFormat, $this->parserFactory); |
130
|
|
|
/** @var FileWriter $writer */ |
131
|
7 |
|
$writer = $this->getBuilder()->build(FileWriter::class, $output, $outputFormat, $this->formatterFactory); |
132
|
|
|
|
133
|
7 |
|
foreach ($reader->fetch() as $row) { |
134
|
7 |
|
$writer->insertOne($row); |
135
|
|
|
} |
136
|
|
|
|
137
|
7 |
|
return $output; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|