Completed
Pull Request — master (#13)
by Jeroen De
03:37
created

TemplateSlidePresenter::getParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace ModernTimeline\SlidePresenter;
6
7
use ModernTimeline\ResultFacade\PropertyValueCollection;
8
use ModernTimeline\ResultFacade\Subject;
9
use SMW\DataValueFactory;
10
11
class TemplateSlidePresenter implements SlidePresenter {
12
13
	private $templateName;
14
15 2
	public function __construct( string $templateName ) {
16 2
		$this->templateName = $templateName;
17 2
	}
18
19 1
	public function getText( Subject $subject ): string {
20 1
		$parser = $this->getParser();
21
22 1
		return $parser->recursiveTagParseFully(
23 1
			( new TemplateSlidePresenter( $this->templateName ) )->getTemplateText( $subject )
24
		);
25
	}
26
27 1
	private function getParser(): \Parser {
28 1
		return $GLOBALS['wgParser'];
29
	}
30
31 2
	public function getTemplateText( Subject $subject ): string {
32 2
		return '{{' . implode( '|', $this->getTemplateSegments( $subject ) ) . '}}';
33
	}
34
35 2
	private function getTemplateSegments( Subject $subject ): array {
36 2
		return array_merge(
37
			[
38 2
				$this->templateName,
39 2
				$this->parameter( 'title', $subject->getWikiPage()->getTitle()->getFullText() )
40
			],
41 2
			array_map(
42 2
				function( PropertyValueCollection $pvc ) {
43 2
					return $this->parameter(
44 2
						$pvc->getPrintRequest()->getText( null ) ?? '',
45 2
						$pvc->getDataItems() === [] ? '' : $this->dataItemToText( $pvc->getDataItems()[0] )
46
					);
47 2
				},
48 2
				$subject->getPropertyValueCollections()
49
			)
50
		);
51
	}
52
53 2
	private function dataItemToText( \SMWDataItem $dataItem ): string {
54 2
		return DataValueFactory::getInstance()->newDataValueByItem( $dataItem )->getLongHTMLText();
55
	}
56
57 2
	private function parameter( string $name, string $value ): string {
58 2
		return $name . '=' . $value;
59
	}
60
}
61