|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright Mediact. All rights reserved. |
|
4
|
|
|
* https://www.Mediact.nl |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Mediact\FileMapping; |
|
8
|
|
|
|
|
9
|
|
|
use ArrayIterator; |
|
10
|
|
|
use Iterator; |
|
11
|
|
|
use SplFileObject; |
|
12
|
|
|
|
|
13
|
|
|
class UnixFileMappingReader implements FileMappingReaderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var array */ |
|
16
|
|
|
private $mappingFilePaths; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
private $sourceDirectory; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
private $targetDirectory; |
|
23
|
|
|
|
|
24
|
|
|
/** @var Iterator|FileMappingInterface[] */ |
|
25
|
|
|
private $mappings; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $sourceDirectory |
|
31
|
|
|
* @param string $targetDirectory |
|
32
|
|
|
* @param string[] ...$mappingFilePaths |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct( |
|
35
|
|
|
string $sourceDirectory, |
|
36
|
|
|
string $targetDirectory, |
|
37
|
|
|
string ...$mappingFilePaths |
|
38
|
|
|
) { |
|
39
|
|
|
$this->sourceDirectory = $sourceDirectory; |
|
40
|
|
|
$this->targetDirectory = $targetDirectory; |
|
41
|
|
|
$this->mappingFilePaths = $mappingFilePaths; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get the mappings. |
|
46
|
|
|
* |
|
47
|
|
|
* @return Iterator |
|
48
|
|
|
*/ |
|
49
|
|
|
private function getMappings(): Iterator |
|
50
|
|
|
{ |
|
51
|
|
|
if ($this->mappings === null) { |
|
52
|
|
|
$filePaths = []; |
|
53
|
|
|
|
|
54
|
|
|
foreach ($this->mappingFilePaths as $mappingFilePath) { |
|
55
|
|
|
$newFilePaths = iterator_to_array(new SplFileObject($mappingFilePath, 'r')); |
|
56
|
|
|
$filePaths = array_merge($filePaths, $newFilePaths); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->mappings = new ArrayIterator( |
|
60
|
|
|
array_map( |
|
61
|
|
|
function (string $mapping) : FileMappingInterface { |
|
62
|
|
|
return new UnixFileMapping( |
|
63
|
|
|
$this->sourceDirectory, |
|
64
|
|
|
$this->targetDirectory, |
|
65
|
|
|
trim($mapping) |
|
66
|
|
|
); |
|
67
|
|
|
}, |
|
68
|
|
|
// Filter out empty lines. |
|
69
|
|
|
array_filter( |
|
70
|
|
|
$filePaths |
|
71
|
|
|
) |
|
72
|
|
|
) |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->mappings; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Move forward to next element. |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
public function next() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->getMappings()->next(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Return the key of the current element. |
|
91
|
|
|
* |
|
92
|
|
|
* @return int |
|
93
|
|
|
*/ |
|
94
|
|
|
public function key(): int |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->getMappings()->key(); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Checks if current position is valid. |
|
101
|
|
|
* |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public function valid(): bool |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->getMappings()->valid(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Rewind the Iterator to the first element. |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
|
|
public function rewind() |
|
115
|
|
|
{ |
|
116
|
|
|
$this->getMappings()->rewind(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get the current file mapping. |
|
121
|
|
|
* |
|
122
|
|
|
* @return FileMappingInterface |
|
123
|
|
|
*/ |
|
124
|
|
|
public function current(): FileMappingInterface |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->getMappings()->current(); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|