|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UCD\Infrastructure\Repository\CharacterRepository\FileRepository\RangeFile; |
|
4
|
|
|
|
|
5
|
|
|
use UCD\Exception\InvalidArgumentException; |
|
6
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\File\PHPFile; |
|
7
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\Range; |
|
8
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\RangeFile; |
|
9
|
|
|
|
|
10
|
|
|
class PHPRangeFile extends RangeFile |
|
11
|
|
|
{ |
|
12
|
|
|
const FILE_NAME_REGEX = '/^(?P<start>\d+)-(?P<end>\d+)!(?P<total>\d+)\.php\.gz$/'; |
|
13
|
|
|
const FILE_PATH_FORMAT = '%s/%08d-%08d!%04d.php.gz'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param \SplFileInfo $fileInfo |
|
17
|
|
|
* @return PHPRangeFile |
|
18
|
|
|
* @throws InvalidArgumentException |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function fromFileInfo(\SplFileInfo $fileInfo) |
|
21
|
|
|
{ |
|
22
|
|
|
if (preg_match(self::FILE_NAME_REGEX, $fileInfo->getBasename(), $matches) !== 1) { |
|
23
|
|
|
throw new InvalidArgumentException(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$range = new Range( |
|
27
|
|
|
(int)$matches['start'], |
|
28
|
|
|
(int)$matches['end'] |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
$file = new PHPFile($fileInfo); |
|
32
|
|
|
$total = (int)$matches['total']; |
|
33
|
|
|
|
|
34
|
|
|
return new self($file, $range, $total); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param \SplFileInfo $basePath |
|
39
|
|
|
* @param Range $range |
|
40
|
|
|
* @param int $total |
|
41
|
|
|
* @return PHPRangeFile |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function fromRangeAndTotal(\SplFileInfo $basePath, Range $range, $total) |
|
44
|
|
|
{ |
|
45
|
|
|
$filePath = self::generateFilePath($basePath, $range, $total); |
|
46
|
|
|
$fileInfo = new \SplFileInfo($filePath); |
|
47
|
|
|
$file = new PHPFile($fileInfo); |
|
48
|
|
|
|
|
49
|
|
|
return new self($file, $range, $total); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param \SplFileInfo $basePath |
|
54
|
|
|
* @param Range $range |
|
55
|
|
|
* @param int $total |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
private static function generateFilePath(\SplFileInfo $basePath, Range $range, $total) |
|
59
|
|
|
{ |
|
60
|
|
|
return sprintf( |
|
61
|
|
|
self::FILE_PATH_FORMAT, |
|
62
|
|
|
$basePath->getPathname(), |
|
63
|
|
|
$range->getStart(), |
|
64
|
|
|
$range->getEnd() - 1, |
|
65
|
|
|
$total |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
} |