1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matecat\XliffParser\Utils; |
4
|
|
|
|
5
|
|
|
class Files { |
6
|
|
|
/** |
7
|
|
|
* PHP Pathinfo is not UTF-8 aware, so we rewrite it. |
8
|
|
|
* It returns array with complete info about a path |
9
|
|
|
* [ |
10
|
|
|
* 'dirname' => PATHINFO_DIRNAME, |
11
|
|
|
* 'basename' => PATHINFO_BASENAME, |
12
|
|
|
* 'extension' => PATHINFO_EXTENSION, |
13
|
|
|
* 'filename' => PATHINFO_FILENAME |
14
|
|
|
* ] |
15
|
|
|
* |
16
|
|
|
* @param string $path |
17
|
|
|
* @param int|null $options |
18
|
|
|
* |
19
|
|
|
* @return array|mixed |
20
|
|
|
*/ |
21
|
|
|
public static function pathInfo( string $path, ?int $options = 15 ) { |
22
|
|
|
$rawPath = explode( DIRECTORY_SEPARATOR, $path ); |
23
|
|
|
|
24
|
|
|
$basename = array_pop( $rawPath ); |
25
|
|
|
$dirname = implode( DIRECTORY_SEPARATOR, $rawPath ); |
26
|
|
|
|
27
|
|
|
$explodedFileName = explode( ".", $basename ); |
28
|
|
|
$extension = strtolower( array_pop( $explodedFileName ) ); |
29
|
|
|
$filename = implode( ".", $explodedFileName ); |
30
|
|
|
|
31
|
|
|
$returnArray = []; |
32
|
|
|
|
33
|
|
|
$flagMap = [ |
34
|
|
|
'dirname' => PATHINFO_DIRNAME, |
35
|
|
|
'basename' => PATHINFO_BASENAME, |
36
|
|
|
'extension' => PATHINFO_EXTENSION, |
37
|
|
|
'filename' => PATHINFO_FILENAME |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
// foreach flag, add in $return_array the corresponding field, |
41
|
|
|
// obtained by variable name correspondence |
42
|
|
|
foreach ( $flagMap as $field => $i ) { |
43
|
|
|
//binary AND |
44
|
|
|
if ( ( $options & $i ) > 0 ) { |
45
|
|
|
//variable substitution: $field can be one between 'dirname', 'basename', 'extension', 'filename' |
46
|
|
|
// $$field gets the value of the variable named $field |
47
|
|
|
$returnArray[ $field ] = $$field; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ( count( $returnArray ) == 1 ) { |
52
|
|
|
$returnArray = array_pop( $returnArray ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $returnArray; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $path |
60
|
|
|
* |
61
|
|
|
* @return ?string |
62
|
|
|
*/ |
63
|
|
|
public static function getExtension( $path ): ?string { |
64
|
|
|
$pathInfo = self::pathInfo( $path ); |
65
|
|
|
|
66
|
|
|
if ( empty( $pathInfo ) ) { |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return strtolower( $pathInfo[ 'extension' ] ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string|null $path |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public static function isXliff( ?string $path ): bool { |
79
|
|
|
$extension = self::getExtension( $path ); |
80
|
|
|
|
81
|
|
|
if ( !$extension ) { |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
switch ( $extension ) { |
86
|
|
|
case 'xliff': |
87
|
|
|
case 'sdlxliff': |
88
|
|
|
case 'tmx': |
89
|
|
|
case 'xlf': |
90
|
|
|
return true; |
91
|
|
|
default: |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $path |
98
|
|
|
* |
99
|
|
|
* @return bool|string |
100
|
|
|
*/ |
101
|
|
|
public static function getMemoryFileType( string $path ) { |
102
|
|
|
$pathInfo = self::pathInfo( $path ); |
103
|
|
|
|
104
|
|
|
if ( empty( $pathInfo ) ) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
switch ( strtolower( $pathInfo[ 'extension' ] ) ) { |
109
|
|
|
case 'tmx': |
110
|
|
|
return 'tmx'; |
111
|
|
|
default: |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $path |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public static function isTMXFile( $path ): bool { |
122
|
|
|
return self::getMemoryFileType( $path ) === 'tmx'; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param $path |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
public static function isGlossaryFile( $path ): bool { |
131
|
|
|
return self::getMemoryFileType( $path ) === 'glossary'; // return false |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|