Completed
Push — master ( d0a026...e68bee )
by ARCANEDEV
14s queued 11s
created

HasRoundedValue::precision()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelMetrics\Metrics\Concerns;
6
7
/**
8
 * Trait     HasRoundedValue
9
 *
10
 * @package  Arcanedev\LaravelMetrics\Metrics\Concerns
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
trait HasRoundedValue
14
{
15
    /* -----------------------------------------------------------------
16
     |  Properties
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * Rounding precision.
22
     *
23
     * @var int
24
     */
25
    public $roundingPrecision = 0;
26
27
    /**
28
     * Rounding mode.
29
     *
30
     * @var int
31
     */
32
    public $roundingMode = PHP_ROUND_HALF_UP;
33
34
    /* -----------------------------------------------------------------
35
     |  Getters & Setters
36
     | -----------------------------------------------------------------
37
     */
38
39
    /**
40
     * Set the precision level used when rounding the value.
41
     *
42
     * @param  int  $precision
43
     * @param  int  $mode
44
     *
45
     * @return $this
46
     */
47 2
    public function precision($precision = 0, $mode = PHP_ROUND_HALF_UP)
48
    {
49 2
        $this->roundingPrecision = $precision;
50 2
        $this->roundingMode      = $mode;
51
52 2
        return $this;
53
    }
54
55
    /* -----------------------------------------------------------------
56
     |  Main Methods
57
     | -----------------------------------------------------------------
58
     */
59
60
    /**
61
     * Round the value.
62
     *
63
     * @param  int|float  $value
64
     *
65
     * @return float
66
     */
67 28
    public function roundValue($value)
68
    {
69 28
        return round($value, $this->roundingPrecision, $this->roundingMode);
70
    }
71
}
72