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