Completed
Push — master ( 21291c...2ce44d )
by Jeroen De
06:22
created

SimpleSlidePresenter::dataItemToText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace ModernTimeline\SlidePresenter;
6
7
use ModernTimeline\ResultFacade\Subject;
8
use SMW\DataValueFactory;
9
use SMW\Query\PrintRequest;
10
11
class SimpleSlidePresenter implements SlidePresenter {
12
13 2
	private $parameters;
14 2
15
	public function __construct( array $parameters ) {
16
		$this->parameters = $parameters;
17 2
	}
18 2
19 2
	public function getText( Subject $subject ): string {
20 2
		return implode( '<br>', iterator_to_array( $this->getDisplayValues( $subject ) ) );
21
	}
22
23 2
	private function getDisplayValues( Subject $subject ) {
24
		foreach ( $subject->getPropertyValueCollections() as $propertyValues ) {
25 2
			foreach ( $propertyValues->getDataItems() as $dataItem ) {
26 2
				if ( !$this->isHiddenPrintRequest( $propertyValues->getPrintRequest() ) ) {
27 2
					yield $this->getDisplayValue( $propertyValues->getPrintRequest(), $dataItem );
28
				}
29 2
			}
30 2
		}
31
	}
32
33 2
	private function isHiddenPrintRequest( PrintRequest $pr ) {
34
		return $pr->getText( null ) === $this->parameters['image property'];
35
	}
36 2
37 2
	private function getDisplayValue( PrintRequest $pr, \SMWDataItem $dataItem ) {
38
		$property = $pr->getText( null );
39
		$value = $this->dataItemToText( $dataItem );
40
41
		if ( $property === '' ) {
42
			return $value;
43
		}
44
45
		return $property . ': ' . $value;
46
	}
47
48
	private function dataItemToText( \SMWDataItem $dataItem ): string {
49
		return DataValueFactory::getInstance()->newDataValueByItem( $dataItem )->getLongHTMLText();
50
	}
51
52
}
53