1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Infrastructure\Repository\CharacterRepository\FileRepository\PropertyFile; |
4
|
|
|
|
5
|
|
|
use UCD\Exception\InvalidArgumentException; |
6
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\File\PHPFile; |
7
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\Property; |
8
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\PropertyFile; |
9
|
|
|
|
10
|
|
|
class PHPPropertyFile extends PropertyFile |
11
|
|
|
{ |
12
|
|
|
const FILE_NAME_REGEX = '/^(?P<type>[A-Za-z_-]+)\.php\.gz$/'; |
13
|
|
|
const FILE_PATH_FORMAT = '%s/%s.php.gz'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param \SplFileInfo $fileInfo |
17
|
|
|
* @return PHPPropertyFile |
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
|
|
|
$file = new PHPFile($fileInfo); |
27
|
|
|
$property = Property::ofType($matches['type']); |
28
|
|
|
|
29
|
|
|
return new self($file, $property); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \SplFileInfo $basePath |
34
|
|
|
* @param Property $property |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
|
|
public static function fromProperty(\SplFileInfo $basePath, Property $property) |
38
|
|
|
{ |
39
|
|
|
$filePath = sprintf(self::FILE_PATH_FORMAT, $basePath->getPathname(), $property); |
40
|
|
|
$fileInfo = new \SplFileInfo($filePath); |
41
|
|
|
$file = new PHPFile($fileInfo); |
42
|
|
|
|
43
|
|
|
return new self($file, $property); |
44
|
|
|
} |
45
|
|
|
} |