NormalizeDate   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 1
dl 0
loc 31
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filterSingle() 0 6 1
A parseDateFromString() 0 12 1
1
<?php
2
declare(strict_types=1);
3
namespace Sirius\Filtration\Filter;
4
5
class NormalizeDate extends AbstractFilter
6
{
7
    const OPTION_INPUT_FORMAT = 'input_format';
8
9
    const OPTION_OUTPUT_FORMAT = 'output_format';
10
11
    protected $options = [
12
        self::OPTION_INPUT_FORMAT => 'd/m/Y',
13
        self::OPTION_OUTPUT_FORMAT => 'Y-m-d'
14
    ];
15
16 2
    public function filterSingle($value, string $valueIdentifier = null)
17
    {
18 2
        $value = (string) $value;
19 2
        $timestamp = $this->parseDateFromString($value, $this->options['input_format']);
20 2
        return date($this->options['output_format'], $timestamp);
21
    }
22
23 2
    protected function parseDateFromString($string, $format)
24
    {
25 2
        $result = date_parse_from_format($format, $string);
26 2
        return mktime(
27 2
            (int) $result['hour'],
28 2
            (int) $result['minute'],
29 2
            (int) $result['second'],
30 2
            (int) $result['month'],
31 2
            (int) $result['day'],
32 2
            (int) $result['year']
33
        );
34
    }
35
}
36