Passed
Push — master ( ece31d...41b8a6 )
by Paul
10:20 queued 04:17
created

ReviewsHtml::offsetGet()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 12
ccs 0
cts 12
cp 0
crap 20
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html;
4
5
use ArrayObject;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
8
class ReviewsHtml extends ArrayObject
9
{
10
    /**
11
     * @var array
12
     */
13
    public $args;
14
15
    /**
16
     * @var int
17
     */
18
    public $max_num_pages;
19
20
    /**
21
     * @var string
22
     */
23
    public $pagination;
24
25
    /**
26
     * @var array
27
     */
28
    public $reviews;
29
30
    public function __construct(array $reviews, $maxPageCount, array $args)
31
    {
32
        $this->args = $args;
33
        $this->max_num_pages = $maxPageCount;
34
        $this->reviews = $reviews;
35
        $this->pagination = $this->buildPagination();
36
        parent::__construct($reviews, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS);
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function __toString()
43
    {
44
        return glsr(Template::class)->build('templates/reviews', [
45
            'args' => $this->args,
46
            'context' => [
47
                'assigned_to' => $this->args['assigned_to'],
48
                'category' => $this->args['category'],
49
                'class' => $this->getClass(),
50
                'id' => $this->args['id'],
51
                'pagination' => $this->getPagination(),
52
                'reviews' => $this->getReviews(),
53
            ],
54
        ]);
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getPagination()
61
    {
62
        return wp_validate_boolean($this->args['pagination'])
63
            ? $this->pagination
64
            : '';
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getReviews()
71
    {
72
        $html = empty($this->reviews)
73
            ? $this->getReviewsFallback()
74
            : implode(PHP_EOL, $this->reviews);
75
        $wrapper = '<div class="glsr-reviews">%s</div>';
76
        $wrapper = apply_filters('site-reviews/reviews/reviews-wrapper', $wrapper);
77
        return sprintf($wrapper, $html);
78
    }
79
80
    /**
81
     * @param mixed $key
82
     * @return mixed
83
     */
84
    public function offsetGet($key)
85
    {
86
        if ('navigation' == $key) {
87
            glsr()->deprecated[] = 'The $reviewsHtml->navigation property has been been deprecated. Please use the $reviewsHtml->pagination property instead.';
88
            return $this->pagination;
89
        }
90
        if (property_exists($this, $key)) {
91
            return $this->$key;
92
        }
93
        return array_key_exists($key, $this->reviews)
94
            ? $this->reviews[$key]
95
            : null;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    protected function buildPagination()
102
    {
103
        $html = glsr(Partial::class)->build('pagination', [
104
            'baseUrl' => glsr_get($this->args, 'pagedUrl'),
105
            'current' => glsr_get($this->args, 'paged'),
106
            'total' => $this->max_num_pages,
107
        ]);
108
        $html.= sprintf('<glsr-pagination hidden data-atts=\'%s\'></glsr-pagination>', $this->args['json']);
109
        $wrapper = '<div class="glsr-pagination">%s</div>';
110
        $wrapper = apply_filters('site-reviews/reviews/pagination-wrapper', $wrapper);
111
        return sprintf($wrapper, $html);
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    protected function getClass()
118
    {
119
        $defaults = [
120
            'glsr-default',
121
        ];
122
        if ('ajax' == $this->args['pagination']) {
123
            $defaults[] = 'glsr-ajax-pagination';
124
        }
125
        $classes = explode(' ', $this->args['class']);
126
        $classes = array_unique(array_merge($defaults, array_filter($classes)));
127
        return implode(' ', $classes);
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    protected function getReviewsFallback()
134
    {
135
        if (empty($this->args['fallback']) && glsr(OptionManager::class)->getBool('settings.reviews.fallback')) {
136
            $this->args['fallback'] = __('There are no reviews yet. Be the first one to write one.', 'site-reviews');
137
        }
138
        $fallback = '<p class="glsr-no-margins">'.$this->args['fallback'].'</p>';
139
        return apply_filters('site-reviews/reviews/fallback', $fallback, $this->args);
140
    }
141
}
142