|
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\Encoding; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\DataFile\Helper\GetOptionTrait; |
|
17
|
|
|
use Graze\DataFile\Helper\OptionalLoggerTrait; |
|
18
|
|
|
use Graze\DataFile\Helper\Process\ProcessFactoryAwareInterface; |
|
19
|
|
|
use Graze\DataFile\Modify\FileModifierInterface; |
|
20
|
|
|
use Graze\DataFile\Modify\FileProcessTrait; |
|
21
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
|
22
|
|
|
use Graze\DataFile\Node\LocalFile; |
|
23
|
|
|
use InvalidArgumentException; |
|
24
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
25
|
|
|
use Psr\Log\LogLevel; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Convert the Encoding of a file |
|
29
|
|
|
* |
|
30
|
|
|
* For a list of the supported encodings run: |
|
31
|
|
|
* |
|
32
|
|
|
* ```bash |
|
33
|
|
|
* iconv -l |
|
34
|
|
|
* ``` |
|
35
|
|
|
*/ |
|
36
|
|
|
class ConvertEncoding implements FileModifierInterface, LoggerAwareInterface, ProcessFactoryAwareInterface |
|
37
|
|
|
{ |
|
38
|
|
|
use OptionalLoggerTrait; |
|
39
|
|
|
use FileProcessTrait; |
|
40
|
|
|
use GetOptionTrait; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @extend Graze\DataFile\Node\File\LocalFile Only apply to local files |
|
44
|
|
|
* |
|
45
|
|
|
* @param LocalFile $file |
|
46
|
|
|
* @param string $encoding Encoding as defined by iconv |
|
47
|
|
|
* @param array $options -postfix <string> (Default: toEncoding) |
|
48
|
|
|
* -keepOldFile <bool> (Default: true) |
|
49
|
|
|
* |
|
50
|
|
|
* @return LocalFile |
|
51
|
|
|
*/ |
|
52
|
7 |
|
public function toEncoding(LocalFile $file, $encoding, array $options = []) |
|
53
|
|
|
{ |
|
54
|
7 |
|
$this->options = $options; |
|
55
|
|
|
|
|
56
|
7 |
|
$postfix = $this->getOption('postfix', $encoding); |
|
57
|
7 |
|
$output = $this->getTargetFile($file, $postfix) |
|
58
|
7 |
|
->setEncoding($encoding); |
|
59
|
|
|
|
|
60
|
|
|
$cmd = "iconv " . |
|
61
|
7 |
|
($file->getEncoding() ? "--from-code={$file->getEncoding()} " : '') . |
|
62
|
7 |
|
"--to-code={$encoding} " . |
|
63
|
7 |
|
"{$file->getPath()} " . |
|
64
|
7 |
|
"> {$output->getPath()}"; |
|
65
|
|
|
|
|
66
|
7 |
|
$this->log(LogLevel::INFO, "Converting file: '{file}' encoding to '{encoding}'", [ |
|
67
|
7 |
|
'file' => $file, |
|
68
|
7 |
|
'encoding' => $encoding, |
|
69
|
7 |
|
]); |
|
70
|
|
|
|
|
71
|
7 |
|
return $this->processFile($file, $output, $cmd, $this->getOption('keepOldFile', true)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Can this file be modified by this modifier |
|
76
|
|
|
* |
|
77
|
|
|
* @param FileNodeInterface $file |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function canModify(FileNodeInterface $file) |
|
82
|
|
|
{ |
|
83
|
|
|
return ( |
|
84
|
3 |
|
($file instanceof localFile) && |
|
85
|
2 |
|
($file->exists()) |
|
86
|
3 |
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Modify the file |
|
91
|
|
|
* |
|
92
|
|
|
* @param FileNodeInterface $file |
|
93
|
|
|
* @param array $options List of options: |
|
94
|
|
|
* -encoding <string> |
|
95
|
|
|
* -postfix <string> (Default: toEncoding) Set this to blank to replace inline |
|
96
|
|
|
* -keepOldFile <bool> (Default: true) |
|
97
|
|
|
* |
|
98
|
|
|
* @return FileNodeInterface |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function modify(FileNodeInterface $file, array $options = []) |
|
101
|
|
|
{ |
|
102
|
2 |
|
if (!$this->canModify($file) || !($file instanceof LocalFile)) { |
|
103
|
1 |
|
throw new InvalidArgumentException("Supplied: $file is not a valid LocalFile"); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
$this->options = $options; |
|
107
|
1 |
|
$encoding = $this->requireOption('encoding'); |
|
108
|
1 |
|
unset($options['encoding']); |
|
109
|
|
|
|
|
110
|
1 |
|
return $this->toEncoding($file, $encoding, $options); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|