Completed
Push — master ( 121c59...3d94bf )
by angel
02:25
created

Money::generaFormato()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 12
nop 2
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 * Copyright (c) 2017 Angel Cruz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the “Software”), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 *
24
 * @author Angel Cruz <[email protected]>
25
 * @license MIT License
26
 * @copyright 2017 Angel Cruz
27
 */
28
29
namespace Abr4xas\Utils;
30
use Exception;
31
32
class Money
33
{
34
    /**
35
     * Genera el formato de moneda
36
     *
37
     * @param  float $valor monto a transformar
38
     * @param array $options additional key/value attributes to include
39
     * @return string valor formateado según $simbolo y $decimal
40
     * @throws Exception
41
     */
42
43
    public static function generaFormato($valor = 0,  $options = [])
44
    {
45
46
        $simbolo  = isset($options['s']) ? $options['s'] : 'BsF';
47
        $decimal  = isset($options['d']) ? $options['d'] : 2;  
48
        
49
        if (!is_numeric($valor))
50
            throw new Exception("{$valor} debe indicar un número que sea válido");
51
        if (!is_int($decimal))
52
            throw new Exception("El valor {$decimal} no es válido");          
53
54
        return $simbolo . ' ' . number_format($valor, $decimal, '.', '');
55
    }
56
    /**
57
     * Retira el formato generado de moneda
58
     *
59
     * @param  string $str monto a transformar
60
     * @param array $options additional key/value attributes to include
61
     * @return float
62
     */
63
    public static function quitarFormato($str, $options = [])
64
    {
65
        $simbolo  = isset($options['s']) ? $options['s'] : 'BsF';
66
        $decimal  = isset($options['d']) ? $options['d'] : 2;
67
        
68
        return number_format(str_replace($simbolo, '', $str), $decimal, '.', '');
69
    }
70
}
71