Passed
Push — master ( d98594...cee2ee )
by Paul
07:20 queued 03:47
created

ReviewsHtml::getClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 11
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html;
4
5
use ArrayObject;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Modules\Html\Partial;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
use GeminiLabs\SiteReviews\Reviews;
10
11
class ReviewsHtml extends ArrayObject
12
{
13
	/**
14
	 * @var array
15
	 */
16
	public $args;
17
18
	/**
19
	 * @var string
20
	 */
21
	public $navigation;
22
23
	/**
24
	 * @var array
25
	 */
26
	public $reviews;
27
28
	public function __construct( array $reviews, $maxPageCount, array $args )
29
	{
30
		$this->args = $args;
31
		$this->reviews = $reviews;
32
		$this->navigation = glsr( Partial::class )->build( 'pagination', [
33
			'total' => $maxPageCount,
34
		]);
35
		parent::__construct( $reviews, ArrayObject::STD_PROP_LIST|ArrayObject::ARRAY_AS_PROPS );
36
	}
37
38
	/**
39
	 * @return string
40
	 */
41
	public function __get( $key )
42
	{
43
		return array_key_exists( $key, $this->reviews )
44
			? $this->reviews[$key]
45
			: '';
46
	}
47
48
	/**
49
	 * @return string
50
	 */
51
	public function __toString()
52
	{
53
		return glsr( Template::class )->build( 'templates/reviews', [
54
			'context' => [
55
				'assigned_to' => $this->args['assigned_to'],
56
				'class' => $this->getClass(),
57
				'id' => $this->args['id'],
58
				'navigation' => $this->getNavigation(),
59
				'reviews' => $this->getReviews(),
60
			],
61
		]);
62
	}
63
64
	/**
65
	 * @return string
66
	 */
67
	protected function getClass()
68
	{
69
		$defaults = [
70
			'glsr-reviews', 'glsr-default',
71
		];
72
		if( $this->args['pagination'] == 'ajax' ) {
73
			$defaults[] = 'glsr-ajax-pagination';
74
		}
75
		$classes = explode( ' ', $this->args['class'] );
76
		$classes = array_unique( array_merge( $defaults, $classes ));
77
		return implode( ' ', $classes );
78
	}
79
80
	/**
81
	 * @return string
82
	 */
83
	protected function getNavigation()
84
	{
85
		return wp_validate_boolean( $this->args['pagination'] )
86
			? $this->navigation
87
			: '';
88
	}
89
90
	/**
91
	 * @return string
92
	 */
93
	protected function getReviews()
94
	{
95
		return empty( $this->reviews )
96
			? $this->getReviewsFallback()
97
			: implode( PHP_EOL, $this->reviews );
98
	}
99
100
	/**
101
	 * @return string
102
	 */
103
	protected function getReviewsFallback()
104
	{
105
		if( empty( $this->args['fallback'] ) && glsr( OptionManager::class )->getBool( 'settings.reviews.fallback' )) {
106
			$this->args['fallback'] = __( 'There are no reviews yet. Be the first one to write one.', 'site-reviews' );
107
		}
108
		$fallback = '<p class="glsr-no-margins">'.$this->args['fallback'].'</p>';
109
		return apply_filters( 'site-reviews/reviews/fallback', $fallback, $this->args );
110
	}
111
}
112