Datetime   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 83
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 7 2
A datetime() 0 12 3
A checkDate() 0 9 3
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
use Particle\Validator\Rule;
12
13
/**
14
 * This Rule is for validating a date/time.
15
 *
16
 * @package Particle\Validator\Rule
17
 */
18
class Datetime extends Rule
19
{
20
    /**
21
     * A constant that will be used when an invalid date/time is passed.
22
     */
23
    const INVALID_VALUE = 'DateTime::INVALID_VALUE';
24
25
    /**
26
     * The message templates which can be returned by this validator.
27
     *
28
     * @var array
29
     */
30
    protected $messageTemplates = [
31
        self::INVALID_VALUE => '{{ name }} must be a valid date',
32
    ];
33
34
    /**
35
     * @var string
36
     */
37
    protected $format;
38
39
    /**
40
     * Construct the Datetime validator.
41
     *
42
     * @param string $format
43
     */
44 10
    public function __construct($format = null)
45
    {
46 10
        $this->format = $format;
47 10
    }
48
49
    /**
50
     * Validates if $value is in the correct date / time format and that it's a valid date.
51
     *
52
     * @param mixed $value
53
     * @return bool
54
     */
55 10
    public function validate($value)
56
    {
57 10
        if (!($this->datetime($value, $this->format) instanceof \DateTime)) {
58 5
            return $this->error(self::INVALID_VALUE);
59
        }
60 6
        return true;
61
    }
62
63
    /**
64
     * Takes $value and $format and attempts to build a valid DateTime object with it.
65
     *
66
     * @param string $value
67
     * @param string $format
68
     * @return \DateTime|false
69
     */
70 10
    protected function datetime($value, $format = null)
71
    {
72 10
        if ($format !== null) {
73 8
            $dateTime = date_create_from_format($format, $value);
74
75 8
            if ($dateTime instanceof \DateTime) {
76 7
                return $this->checkDate($dateTime, $format, $value);
77
            }
78 2
            return false;
79
        }
80 2
        return @date_create($value);
81
    }
82
83
    /**
84
     * Checks if $dateTime is a valid date-time object, and if the formatted date is the same as the value passed.
85
     *
86
     * @param \DateTime $dateTime
87
     * @param string $format
88
     * @param mixed $value
89
     * @return \DateTime|false
90
     */
91 7
    protected function checkDate($dateTime, $format, $value)
92
    {
93 7
        $equal = (string) $dateTime->format($format) === (string) $value;
94
95 7
        if ($dateTime->getLastErrors()['warning_count'] === 0 && $equal) {
96 5
            return $dateTime;
97
        }
98 2
        return false;
99
    }
100
}
101