aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Feedback/RatingHelper.php
blob: c258f7e5dac695cecf39b8438d8fb18d1634c477 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php

namespace JsonApi\Routes\Feedback;

use FeedbackElement;
use InvalidArgumentException;

trait RatingHelper
{
    private function getRating(FeedbackElement $element, int $rating): int
    {
        $mode = intval($element['mode']);

        if ($mode === 0) {
            return 0;
        }

        if ($rating === 0) {
            return 1;
        }

        if ($mode === 1) {
            return min(5, $rating);
        }

        if ($mode === 2) {
            return min(10, $rating);
        }

        throw new InvalidArgumentException("Invalid mode {$mode}");
    }
}