Completed
Push — master ( 9dc7b5...63aa58 )
by Jeroen De
08:12 queued 07:13
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 MediaWiki\MediaWikiServices;
8
use ModernTimeline\ResultFacade\PropertyValueCollection;
9
use ModernTimeline\ResultFacade\Subject;
10
use SMW\DataValueFactory;
11
12
class TemplateSlidePresenter implements SlidePresenter {
13
14
	private $templateName;
15
16 2
	public function __construct( string $templateName ) {
17 2
		$this->templateName = $templateName;
18 2
	}
19
20 1
	public function getText( Subject $subject ): string {
21 1
		$parser = $this->getParser();
22
23 1
		return $parser->recursiveTagParseFully(
24 1
			( new TemplateSlidePresenter( $this->templateName ) )->getTemplateText( $subject )
25
		);
26
	}
27
28 1
	private function getParser(): \Parser {
29 1
		return MediaWikiServices::getInstance()->getParser();
30
	}
31
32 2
	public function getTemplateText( Subject $subject ): string {
33 2
		return '{{' . implode( '|', $this->getTemplateSegments( $subject ) ) . '}}';
34
	}
35
36 2
	private function getTemplateSegments( Subject $subject ): array {
37 2
		return array_merge(
38
			[
39 2
				$this->templateName,
40 2
				$this->parameter( 'title', $subject->getWikiPage()->getTitle()->getFullText() )
41
			],
42 2
			array_map(
43 2
				function( PropertyValueCollection $pvc ) {
44 2
					return $this->parameter(
45 2
						$pvc->getPrintRequest()->getText( null ) ?? '',
46 2
						$pvc->getDataItems() === [] ? '' : $this->dataItemToText( $pvc->getDataItems()[0] )
47
					);
48 2
				},
49 2
				$subject->getPropertyValueCollections()->toArray()
50
			)
51
		);
52
	}
53
54 2
	private function dataItemToText( \SMWDataItem $dataItem ): string {
55 2
		return DataValueFactory::getInstance()->newDataValueByItem( $dataItem )->getLongHTMLText();
56
	}
57
58 2
	private function parameter( string $name, string $value ): string {
59 2
		return $name . '=' . $value;
60
	}
61
}
62