Completed
Push — master ( 36e04f...8ec39e )
by Jeroen De
04:55 queued 03:58
created

SimpleSlidePresenter::getDisplayValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0185
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
	private $parameters;
14
15 3
	public function __construct( array $parameters ) {
16 3
		$this->parameters = $parameters;
17 3
	}
18
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 2
		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
			}
30
		}
31 2
	}
32
33 2
	private function isHiddenPrintRequest( PrintRequest $pr ) {
34 2
		return $pr->getText( null ) === $this->parameters['image property'];
35
	}
36
37 2
	private function getDisplayValue( PrintRequest $pr, \SMWDataItem $dataItem ) {
38 2
		$property = $pr->getText( null );
39 2
		$value = $this->dataItemToText( $dataItem );
40
41 2
		if ( $property === '' ) {
42
			return $value;
43
		}
44
45 2
		return $property . ': ' . $value;
46
	}
47
48 2
	private function dataItemToText( \SMWDataItem $dataItem ): string {
49 2
		return DataValueFactory::getInstance()->newDataValueByItem( $dataItem )->getLongHTMLText();
50
	}
51
52
}
53