NormalizeDate::filterSingle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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