PluginProbe
Site Reviews / trunk
Site Reviews vtrunk
8.3.1 8.3.0 8.2.2 8.2.1 8.2.0 8.1.0 8.0.13 8.0.12 8.0.11 trunk 1.2.2 2.17.1 3.5.4 4.7.0 5.25.1 6.11.8 7.0.10 7.0.11 7.0.12 7.0.13 7.0.14 7.0.15 7.0.16 7.0.17 7.0.18 All 54 releases
site-reviews / plugin / Database / RatingManager.php

RatingManager.php in Site Reviews trunk, at plugin/Database/RatingManager.php

59 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace GeminiLabs\SiteReviews\Database;
4
5 use GeminiLabs\SiteReviews\Helper;
6 use GeminiLabs\SiteReviews\Helpers\Arr;
7 use GeminiLabs\SiteReviews\Helpers\Str;
8 use GeminiLabs\SiteReviews\Modules\Rating;
9
10 class RatingManager
11 {
12 public function ratings(array $args = []): array
13 {
14 $ratings = glsr(Query::class)->ratings($args);
15 $ratings = $this->reduce($ratings, $args);
16 return glsr()->filterArray('ratings', $ratings, $args);
17 }
18
19 public function ratingsGroupedBy(string $metaGroup, array $args = []): array
20 {
21 $metaGroup = strtolower(Str::restrictTo(['post', 'term', 'user'], $metaGroup, 'post'));
22 $metaTable = sprintf('%smeta', $metaGroup);
23 $ratings = glsr(Query::class)->ratingsFor($metaTable, $args);
24 foreach ($ratings as $id => &$group) {
25 $group = $this->reduce($group, $args);
26 }
27 return glsr()->filterArray('ratings/grouped', $ratings, $metaGroup, $args);
28 }
29
30 protected function maxRating(array $args): int
31 {
32 return Arr::getAs('int', $args, 'max', Rating::max());
33 }
34
35 protected function minRating(array $args): int
36 {
37 return Arr::getAs('int', $args, 'min', Rating::min());
38 }
39
40 /**
41 * Combine ratings grouped by type into a single rating array.
42 */
43 protected function reduce(array $ratings, array $args = []): array
44 {
45 $max = $this->maxRating($args);
46 $min = $this->minRating($args);
47 $normalized = [];
48 array_walk_recursive($ratings, function ($rating, $index) use (&$normalized) {
49 $normalized[$index] = $rating + intval(Arr::get($normalized, $index, 0));
50 });
51 foreach ($normalized as $index => &$rating) {
52 if (!Helper::inRange($index, $min, $max)) {
53 $rating = 0;
54 }
55 }
56 return $normalized;
57 }
58 }
59