Completed
Push — master ( 951284...b5c57e )
by
unknown
06:36 queued 11s
created

ParserFunctions/VariantsAwareRenderer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Wikibase\Client\DataAccess\ParserFunctions;
4
5
use OutOfBoundsException;
6
use Wikibase\DataModel\Entity\EntityId;
7
8
/**
9
 * Handler of the {{#property}} parser function.
10
 *
11
 * @license GPL-2.0-or-later
12
 * @author Katie Filbert < [email protected] >
13
 * @author Thiemo Kreuz
14
 */
15
class VariantsAwareRenderer implements StatementGroupRenderer {
16
17
	/**
18
	 * @var string[]
19
	 */
20
	private $variants;
21
22
	/**
23
	 * @var LanguageAwareRenderer[]
24
	 */
25
	private $languageAwareRenderers;
26
27
	/**
28
	 * @param LanguageAwareRenderer[] $languageAwareRenderers
29
	 * @param string[] $variants
30
	 */
31
	public function __construct( array $languageAwareRenderers, array $variants ) {
32
		$this->languageAwareRenderers = $languageAwareRenderers;
33
		$this->variants = $variants;
34
	}
35
36
	/**
37
	 * @param EntityId $entityId
38
	 * @param string $propertyLabelOrId
39
	 *
40
	 * @throws OutOfBoundsException
41
	 * @return string Wikitext
42
	 */
43
	public function render( EntityId $entityId, $propertyLabelOrId ) {
44
		$renderedVariantsArray = $this->buildRenderedVariantsArray( $entityId, $propertyLabelOrId );
45
46
		return $this->processRenderedArray( $renderedVariantsArray );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->processRenderedAr...renderedVariantsArray); of type string|false adds false to the return on line 46 which is incompatible with the return type declared by the interface Wikibase\Client\DataAcce...ntGroupRenderer::render of type string. It seems like you forgot to handle an error condition.
Loading history...
47
	}
48
49
	/**
50
	 * @param EntityId $entityId
51
	 * @param string $propertyLabelOrId
52
	 *
53
	 * @throws OutOfBoundsException
54
	 * @return string[] key by variant codes
55
	 */
56
	private function buildRenderedVariantsArray( EntityId $entityId, $propertyLabelOrId ) {
57
		$renderedVariantsArray = [];
58
59
		foreach ( $this->variants as $variantCode ) {
60
			$variantText = $this->getVariantText( $variantCode, $entityId, $propertyLabelOrId );
61
62
			// LanguageConverter doesn't handle empty strings correctly, and it's more difficult
63
			// to fix the issue there, as it's using empty string as a special value.
64
			// Also keeping the ability to check a missing property with {{#if: }} is another reason.
65
			if ( $variantText !== '' ) {
66
				$renderedVariantsArray[$variantCode] = $variantText;
67
			}
68
		}
69
70
		return $renderedVariantsArray;
71
	}
72
73
	/**
74
	 * Post-process rendered array (variant text) into wikitext to be used in pages.
75
	 *
76
	 * @param string[] $textArray
77
	 *
78
	 * @return string Wikitext
79
	 */
80
	private function processRenderedArray( array $textArray ) {
81
		if ( $textArray === [] ) {
82
			return '';
83
		}
84
85
		if ( count( array_unique( $textArray ) ) === 1 ) {
86
			return reset( $textArray );
87
		}
88
89
		$text = '';
90
91
		foreach ( $textArray as $variantCode => $variantText ) {
92
			$text .= "$variantCode:$variantText;";
93
		}
94
95
		return '-{' . $text . '}-';
96
	}
97
98
	/**
99
	 * @param string $variantCode
100
	 * @param EntityId $entityId
101
	 * @param string $propertyLabelOrId
102
	 *
103
	 * @throws OutOfBoundsException
104
	 * @return string Wikitext
105
	 */
106
	private function getVariantText( $variantCode, EntityId $entityId, $propertyLabelOrId ) {
107
		$renderer = $this->getLanguageAwareRendererFromCode( $variantCode );
108
109
		return $renderer->render( $entityId, $propertyLabelOrId );
110
	}
111
112
	/**
113
	 * @param string $variantCode
114
	 *
115
	 * @throws OutOfBoundsException
116
	 * @return LanguageAwareRenderer
117
	 */
118
	private function getLanguageAwareRendererFromCode( $variantCode ) {
119
		if ( !isset( $this->languageAwareRenderers[$variantCode] ) ) {
120
			throw new OutOfBoundsException( 'No LanguageAwareRenderer set for ' . $variantCode );
121
		}
122
123
		return $this->languageAwareRenderers[$variantCode];
124
	}
125
126
}
127