Completed
Push — 2.x ( b717bf...e8038a )
by Paul
04:12 queued 01:02
created

DateTime::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
crap 2
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter\Rule\Sanitize;
10
11
use Aura\Filter\Rule\AbstractDateTime;
12
13
/**
14
 *
15
 * Sanitize a datetime to a specifed format.
16
 *
17
 * @package Aura.Filter
18
 *
19
 */
20
class DateTime extends AbstractDateTime
21
{
22
    /**
23
     *
24
     * Sanitize a datetime to a specified format, default "Y-m-d H:i:s".
25
     *
26
     * @param object $subject The subject to be filtered.
27
     *
28
     * @param string $field The subject field name.
29
     *
30
     * @param string $format The date format to use.
31
     *
32
     * @return bool True if the value was sanitized, false if not.
33
     *
34
     */
35 5
    public function __invoke($subject, $field, $format = 'Y-m-d H:i:s')
36
    {
37 5
        $value = $subject->$field;
38 5
        $datetime = $this->newDateTime($value);
39 5
        if (! $datetime) {
40 3
            return false;
41
        }
42 2
        $subject->$field = $datetime->format($format);
43 2
        return true;
44
    }
45
}
46