|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html\Partials; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Arguments; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Contracts\PartialContract; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Defaults\StarRatingDefaults; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
|
10
|
|
|
|
|
11
|
|
|
class StarRating implements PartialContract |
|
12
|
|
|
{ |
|
13
|
|
|
protected Arguments $data; |
|
14
|
|
|
|
|
15
|
1 |
|
public function build(array $args = []): string |
|
16
|
|
|
{ |
|
17
|
1 |
|
$this->data = glsr()->args( |
|
18
|
1 |
|
glsr(StarRatingDefaults::class)->restrict($args) |
|
19
|
1 |
|
); |
|
20
|
1 |
|
return glsr(Builder::class)->div([ |
|
21
|
1 |
|
'aria-label' => $this->label(), |
|
22
|
1 |
|
'class' => 'glsr-star-rating glsr-stars', |
|
23
|
1 |
|
'data-rating' => $this->data->rating, |
|
24
|
1 |
|
'data-reviews' => $this->data->reviews, |
|
25
|
1 |
|
'role' => 'img', |
|
26
|
1 |
|
'text' => $this->stars(), |
|
27
|
1 |
|
]); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
protected function label() |
|
31
|
|
|
{ |
|
32
|
1 |
|
$rating = $this->data->rating; |
|
33
|
1 |
|
$title = $this->data->reviews > 0 |
|
34
|
|
|
? __('Rated %s out of %s stars based on %s ratings', 'site-reviews') |
|
35
|
1 |
|
: __('Rated %s out of %s stars', 'site-reviews'); |
|
36
|
1 |
|
if (0 !== $this->data->num_half) { |
|
37
|
|
|
$rating = glsr(Rating::class)->format($rating); |
|
38
|
|
|
} |
|
39
|
1 |
|
return sprintf($title, $rating, Rating::max(), $this->data->reviews); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
protected function stars(): string |
|
43
|
|
|
{ |
|
44
|
1 |
|
$types = [ // order is intentional |
|
45
|
1 |
|
'full' => $this->data->num_full, |
|
46
|
1 |
|
'half' => $this->data->num_half, |
|
47
|
1 |
|
'empty' => $this->data->num_empty, |
|
48
|
1 |
|
]; |
|
49
|
1 |
|
$results = []; |
|
50
|
1 |
|
foreach ($types as $type => $repeat) { |
|
51
|
1 |
|
$template = glsr(Builder::class)->span([ |
|
52
|
1 |
|
'aria-hidden' => 'true', |
|
53
|
1 |
|
'class' => "glsr-star glsr-star-{$type}", |
|
54
|
1 |
|
]); |
|
55
|
1 |
|
$results[] = str_repeat($template, $repeat); |
|
56
|
|
|
} |
|
57
|
1 |
|
return implode('', $results); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|