summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/ModernTimeline/src/SlidePresenter/TemplateSlidePresenter.php
blob: 09c821d5e24c5b8210a19bf364f2e558d6782571 (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
<?php

declare( strict_types = 1 );

namespace ModernTimeline\SlidePresenter;

use ModernTimeline\ResultFacade\PropertyValueCollection;
use ModernTimeline\ResultFacade\Subject;
use SMW\DataValueFactory;

class TemplateSlidePresenter implements SlidePresenter {

	private $templateName;

	public function __construct( string $templateName ) {
		$this->templateName = $templateName;
	}

	public function getText( Subject $subject ): string {
		$parser = $this->getParser();

		return $parser->recursiveTagParseFully(
			( new TemplateSlidePresenter( $this->templateName ) )->getTemplateText( $subject )
		);
	}

	private function getParser(): \Parser {
		return $GLOBALS['wgParser'];
	}

	public function getTemplateText( Subject $subject ): string {
		return '{{' . implode( '|', $this->getTemplateSegments( $subject ) ) . '}}';
	}

	private function getTemplateSegments( Subject $subject ): array {
		return array_merge(
			[
				$this->templateName,
				$this->parameter( 'title', $subject->getWikiPage()->getTitle()->getFullText() )
			],
			array_map(
				function( PropertyValueCollection $pvc ) {
					return $this->parameter(
						$pvc->getPrintRequest()->getText( null ) ?? '',
						$pvc->getDataItems() === [] ? '' : $this->dataItemToText( $pvc->getDataItems()[0] )
					);
				},
				$subject->getPropertyValueCollections()
			)
		);
	}

	private function dataItemToText( \SMWDataItem $dataItem ): string {
		return DataValueFactory::getInstance()->newDataValueByItem( $dataItem )->getLongHTMLText();
	}

	private function parameter( string $name, string $value ): string {
		return $name . '=' . $value;
	}
}