Completed
Push — master ( c21f80...d9d319 )
by Ben
05:32
created

Numbers   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 4
Metric Value
wmc 11
c 5
b 0
f 4
lcom 1
cbo 1
dl 0
loc 76
ccs 28
cts 28
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A asCurrency() 0 18 3
A asUnsigned() 0 12 3
A asNumber() 0 8 2
A normaliseValue() 0 8 3
1
<?php
2
3
namespace Benrowe\Formatter\Providers;
4
5
use Benrowe\Formatter\AbstractFormatterProvider;
6
7
/**
8
 * @package Benrowe\Formatter
9
 */
10
class Numbers extends AbstractFormatterProvider
11
{
12
    /**
13
     * Display the value as currency
14
     *
15
     * @param  mixed $value
16
     * @return string
17
     */
18 13
    public function asCurrency($value)
19
    {
20 13
        if ($value === null) {
21 1
            return $this->nullValue;
22
        }
23
24 12
        $value = $this->normaliseValue($value);
25
26 12
        $prefix = '$';
27 12
        if ($value < 0) {
28 5
            $prefix = '-$';
29 5
            $value *= -1;
30 5
        }
31
32 12
        $newValue = number_format($value, 2);
33
34 12
        return $prefix.$newValue;
35
    }
36
37
    /**
38
     * Displays the number as an unsigned value
39
     *
40
     * @param  mixed $value
41
     * @return float|int
0 ignored issues
show
Documentation introduced by
Should the return type not be string|integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
42
     */
43 6
    public function asUnsigned($value)
44
    {
45 6
        if ($value === null) {
46 1
            return $this->nullValue;
47
        }
48
49 5
        $value = $this->normaliseValue($value);
50 5
        if ($value < 0) {
51 2
            $value *= -1;
52 2
        }
53 5
        return $value;
54
    }
55
56
    /**
57
     * Format the supplied value as a number
58
     *
59
     * @param  mixed $value
60
     * @return float|int
0 ignored issues
show
Documentation introduced by
Should the return type not be string|integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
61
     */
62 6
    public function asNumber($value)
63
    {
64 6
        if ($value === null) {
65 1
            return $this->nullValue;
66
        }
67
        
68 5
        return $this->normaliseValue($value);
69
    }
70
71
    /**
72
     * Convert the supplied value into a valid number value
73
     *
74
     * @param  mixed $value
75
     * @return float
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
76
     */
77 22
    private function normaliseValue($value)
78
    {
79 22
        if (!is_numeric($value)) {
80 1
            $value = 0;
81 1
        }
82
        // cast the value to int or float based on its true type
83 22
        return (float)$value === (float)(int)$value ? (int)$value : (float)$value;
84
    }
85
}
86