1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-flow |
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-flow/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-flow |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFlow\Flow\File; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Node\LocalFile; |
17
|
|
|
use Graze\DataFlow\Flow\InvokeTrait; |
18
|
|
|
use Graze\DataFlow\FlowInterface; |
19
|
|
|
use Graze\DataNode\NodeInterface; |
20
|
|
|
use InvalidArgumentException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Convert the Encoding of a file |
24
|
|
|
* |
25
|
|
|
* For a list of the supported encodings run: |
26
|
|
|
* |
27
|
|
|
* ```bash |
28
|
|
|
* iconv -l |
29
|
|
|
* ``` |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
class ConvertEncoding extends \Graze\DataFile\Modify\Encoding\ConvertEncoding implements FlowInterface |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
use InvokeTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $encoding; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $encoding Encoding as defined by iconv |
42
|
|
|
* @param array $options -postfix <string> (Default: toEncoding) |
43
|
|
|
* -keepOldFile <bool> (Default: true) |
44
|
|
|
*/ |
45
|
4 |
|
public function __construct($encoding, array $options = []) |
46
|
|
|
{ |
47
|
4 |
|
$this->encoding = $encoding; |
48
|
4 |
|
$this->options = $options; |
49
|
4 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param NodeInterface $node |
53
|
|
|
* |
54
|
|
|
* @return NodeInterface |
55
|
|
|
* @throws InvalidArgumentException |
56
|
|
|
*/ |
57
|
4 |
|
public function flow(NodeInterface $node) |
58
|
|
|
{ |
59
|
4 |
|
if (!($node instanceof LocalFile)) { |
60
|
1 |
|
throw new InvalidArgumentException("Node: $node should be an instance of LocalFile"); |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
return $this->toEncoding($node, $this->encoding, $this->options); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.