forked from move-elevator/composer-translation-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXliffSchemaValidator.php
More file actions
138 lines (116 loc) · 4.68 KB
/
Copy pathXliffSchemaValidator.php
File metadata and controls
138 lines (116 loc) · 4.68 KB
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
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
/*
* This file is part of the "composer-translation-validator" Composer plugin.
*
* (c) 2025-2026 Konrad Michalik <km@move-elevator.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MoveElevator\ComposerTranslationValidator\Validator;
use Exception;
use MoveElevator\ComposerTranslationValidator\Parser\{ParserInterface, XliffParser};
use MoveElevator\ComposerTranslationValidator\Result\Issue;
use Symfony\Component\Config\Util\XmlUtils;
use Symfony\Component\Translation\Util\XliffUtils;
use function sprintf;
use function strtolower;
/**
* XliffSchemaValidator.
*
* @author Konrad Michalik <km@move-elevator.de>
* @license GPL-3.0-or-later
*/
class XliffSchemaValidator extends AbstractValidator implements ValidatorInterface
{
public function processFile(ParserInterface $file): array
{
/*
* With XmlUtils::loadFile() we always get a strange symfony error related to global composer autoloading issue.
* Call to undefined method Symfony\Component\Filesystem\Filesystem::readFile()
*/
if (!file_exists($file->getFilePath())) {
$this->logger?->error('File does not exist: '.$file->getFileName());
return [];
}
$fileContent = file_get_contents($file->getFilePath());
if (false === $fileContent) {
$this->logger?->error('Failed to read file: '.$file->getFileName());
return [];
}
try {
$dom = XmlUtils::parse($fileContent);
} catch (Exception $e) {
$this->logger?->error('Failed to parse XML: '.$e->getMessage());
return [];
}
// Schema validation — may throw for unsupported XLIFF versions (e.g. 2.x)
$errors = [];
try {
$errors = XliffUtils::validateSchema($dom);
} catch (Exception $e) {
if (str_contains($e->getMessage(), 'No support implemented for loading XLIFF version')) {
$this->logger?->notice(sprintf('Skipping %s: %s', $this->getShortName(), $e->getMessage()));
} else {
$this->logger?->error('Failed to validate XML schema: '.$e->getMessage());
}
}
// Additional check: if filename encodes a locale, verify it matches target-language in the file header
if (!$file instanceof XliffParser) {
return $errors;
}
$expectedLanguage = $file->getLanguageFromFileName();
if (null !== $expectedLanguage) {
$targetLang = $file->getTargetLanguage();
$isVersion2 = $file->isVersion2();
$attribute = $isVersion2 ? 'trgLang' : 'target-language';
$element = $isVersion2 ? '<xliff>' : '<file>';
if (null === $targetLang) {
$errors[] = [
'message' => sprintf(
'Missing "%s" attribute on %s node; expected "%s" based on filename',
$attribute,
$element,
$expectedLanguage,
),
'level' => 'ERROR',
];
} elseif (strtolower($targetLang) !== $expectedLanguage) {
$errors[] = [
'message' => sprintf(
'"%s" attribute "%s" does not match filename language "%s"',
$attribute,
$targetLang,
$expectedLanguage,
),
'level' => 'ERROR',
];
}
}
return $errors;
}
public function formatIssueMessage(Issue $issue, string $prefix = ''): string
{
$details = $issue->getDetails();
// Since AbstractValidator creates one Issue per error array,
// $details is the individual error array, not an array of errors
if (isset($details['message'])) {
$message = $details['message'];
$line = isset($details['line']) ? " (Line: {$details['line']})" : '';
$code = isset($details['code']) ? " (Code: {$details['code']})" : '';
$level = $details['level'] ?? 'ERROR';
$color = 'ERROR' === strtoupper((string) $level) ? 'red' : 'yellow';
$levelText = ucfirst(strtolower((string) $level));
return "- <fg=$color>$levelText</> {$prefix}$message$line$code";
}
return "- <fg=red>Error</> {$prefix}Schema validation error";
}
/**
* @return class-string<ParserInterface>[]
*/
public function supportsParser(): array
{
return [XliffParser::class];
}
}