Passed
Push — master ( 2508fd...1acd8b )
by Stephen
01:50 queued 11s
created

FormatDollars::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Sfneal\Currency;
4
5
use Sfneal\Actions\Action;
6
7
class FormatDollars extends Action
8
{
9
    /**
10
     * @var float
11
     */
12
    private $amount;
13
14
    /**
15
     * @var int
16
     */
17
    private $decimals;
18
19
    /**
20
     * @var string
21
     */
22
    private $thousands_sep;
23
24
    /**
25
     * @var bool
26
     */
27
    private $dollar_sign_prefix;
28
29
    /**
30
     * FormatDollars constructor.
31
     *
32
     * @param float $amount
33
     * @param int $decimals
34
     * @param string $thousands_sep
35
     * @param bool $dollar_sign_prefix
36
     */
37
    public function __construct(float $amount,
38
                                int $decimals = 2,
39
                                string $thousands_sep = '',
40
                                bool $dollar_sign_prefix = false)
41
    {
42
        $this->amount = $amount;
43
        $this->decimals = $decimals;
44
        $this->thousands_sep = $thousands_sep;
45
        $this->dollar_sign_prefix = $dollar_sign_prefix;
46
    }
47
48
    /**
49
     * Format a currency float value to a standard format.
50
     *
51
     * @return string
52
     */
53
    public function execute(): string
54
    {
55
        // Format $amount
56
        $value = number_format($this->amount, $this->decimals, '.', $this->thousands_sep);
57
58
        // Optionally prefix the value with a dollar sign
59
        return $this->dollar_sign_prefix ? '$'.$value : $value;
60
    }
61
}
62