summaryrefslogtreecommitdiff
blob: 1b19433451125a8993865cde9bc88c7b75760d42 (plain)
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
<?php
/**
 * @file
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 */

declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\Validation;

use MediaWiki\Extension\Translate\TranslatorInterface\Insertable\InsertablesSuggester;
use TMessage;

/**
 * Object adapter for message validators that implement the deprecated interface.
 *
 * @since 2020.06
 */
class LegacyValidatorAdapter implements MessageValidator, InsertablesSuggester {
	/** @var Validator */
	private $validator;

	public function __construct( Validator $validator ) {
		$this->validator = $validator;
	}

	/** @inheritDoc */
	public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues {
		$notices = [];
		$this->validator->validate( $message, $targetLanguage, $notices );
		return $this->convertNoticesToValidationIssues( $notices, $message->key() );
	}

	private function convertNoticesToValidationIssues(
		array $notices,
		string $messageKey
	): ValidationIssues {
		$issues = new ValidationIssues();
		foreach ( $notices[$messageKey] ?? [] as $notice ) {
			$issue = new ValidationIssue(
				$notice[0][0],
				$notice[0][1],
				$notice[1],
				array_slice( $notice, 2 )
			);
			$issues->add( $issue );
		}

		return $issues;
	}

	/** @inheritDoc */
	public function getInsertables( string $text ): array {
		if ( $this->validator instanceof InsertablesSuggester ) {
			return $this->validator->getInsertables( $text );
		}

		return [];
	}
}

class_alias( LegacyValidatorAdapter::class, '\MediaWiki\Extensions\Translate\LegacyValidatorAdapter' );