FormatHelper::onlyNumbers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bludata\Common\Helpers;
4
5
use DateTime;
6
use InvalidArgumentException;
7
8
class FormatHelper
9
{
10
    public static function onlyNumbers($input)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
11
    {
12
        return preg_replace('/\D/i', '', $input);
13
    }
14
15
    public static function parseDate($date, $from = 'Y-m-d', $to = 'obj')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
16
    {
17
        if (!is_string($from)) {
18
            throw new InvalidArgumentException('Formato de entrada inválido');
19
        }
20
21
        if (!is_string($to)) {
22
            throw new InvalidArgumentException('Formato de saída inválido');
23
        }
24
25
        if ($date instanceof DateTime && $to === 'obj') {
26
            return $date;
27
        }
28
29
        if ($date instanceof DateTime) {
30
            return $date->format($to);
31
        }
32
33
        $dateObject = DateTime::createFromFormat($from, $date);
34
35
        if ($to === 'obj') {
36
            return $dateObject;
37
        }
38
39
        if (is_string($to)) {
40
            return $dateObject->format($to);
41
        }
42
43
        throw new InvalidArgumentException(
44
            sprintf(
45
                'Não foi possível converter a data "%s" do formato "%s" para o formato "%s"',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 93 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
46
                $date,
47
                $from,
48
                $to
49
            )
50
        );
51
    }
52
}
53