Completed
Push — master ( b6d955...929034 )
by Marcus
02:03
created

DateTime::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Mbright\Validation\Rule\Sanitize;
4
5
use Mbright\Validation\Rule\AbstractDateTime;
6
7
class DateTime extends AbstractDateTime implements SanitizeRuleInterface
8
{
9
    /** @var string */
10
    protected $format;
11
12
    /**
13
     * @param string $format DateTime Format to use
14
     */
15 15
    public function __construct(string $format = 'Y-m-d H:i:s')
16
    {
17 15
        $this->format = $format;
18 15
    }
19
20
    /**
21
     * Sanitize a datetime to a specified format, default "Y-m-d H:i:s".
22
     *
23
     * @param object $subject The subject to be filtered.
24
     * @param string $field The subject field name.
25
     *
26
     * @return bool True if the value was sanitized, false if not.
27
     */
28 15
    public function __invoke($subject, string $field): bool
29
    {
30 15
        $value = $subject->$field;
31 15
        $datetime = $this->newDateTime($value);
32 15
        if (!$datetime) {
33 9
            return false;
34
        }
35 6
        $subject->$field = $datetime->format($this->format);
36
37 6
        return true;
38
    }
39
}
40