Completed
Push — master ( fefd2b...bbdd37 )
by Vincenzo
02:31
created

TextFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A currencyStringToFloat() 0 15 1
A snakeToCamelCase() 0 11 2
1
<?php
2
namespace App\Lib\Helpers;
3
4
5
/**
6
 * Class TextFormatter
7
 * @package App\Lib\Helpers
8
 */
9
class TextFormatter
10
{
11
12
    /**
13
     * @param $currencyString
14
     * @return float
15
     */
16
    public static function currencyStringToFloat($currencyString)
17
    {
18
        //currency is always in this format € 550
19
20
        return floatval(
21
            str_replace(
22
                ',',
23
                '',
24
                RegExp::getFirstMatch(
25
                    '/([+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)/',
26
                    $currencyString
27
                )
28
            )
29
        );
30
    }
31
32
    /**
33
     * @param $string
34
     * @param bool $ucFirst
35
     * @return string
36
     */
37
    public static function snakeToCamelCase($string, $ucFirst = true)
38
    {
39
        $string = preg_replace_callback("/(?:^|_)([a-z])/", function ($matches) {
40
            return strtoupper($matches[1]);
41
        }, $string);
42
43
        if (!$ucFirst) {
44
            $string = lcfirst($string);
45
        }
46
        return $string;
47
    }
48
49
}