for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Matecat\XliffParser\XliffUtils;
use Matecat\XliffParser\Exception\NotSupportedVersionException;
use Matecat\XliffParser\Exception\NotValidFileException;
class XliffVersionDetector {
/**
* @var array
*/
private static array $versions_1 = [ '1.0', '1.1', '1.2' ];
private static array $versions_2 = [ '2.0', '2.1' ];
* @param string $xliffContent
*
* @return int
* @throws NotSupportedVersionException
* @throws NotValidFileException
public static function detect( string $xliffContent ): int {
preg_match( '|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr( $xliffContent, 0, 1000 ), $versionMatches );
if ( empty( $versionMatches ) ) {
throw new NotValidFileException( 'This is not a valid xliff file' );
}
$version = $versionMatches[ 1 ];
return self::resolveVersion( $version );
* @param string $version
private static function resolveVersion( string $version ): int {
if ( in_array( $version, self::$versions_1 ) ) {
return 1;
if ( in_array( $version, self::$versions_2 ) ) {
return 2;
throw new NotSupportedVersionException( 'Not supported version' );