Completed
Push — master ( 8ce34a...26baa3 )
by Marcus
03:49
created

Date::sanitizeDateString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Mbright\Validation\Rule\Validate\MySql;
4
5
use Mbright\Validation\Rule\Validate\ValidateRuleInterface;
6
7
/**
8
 * Validates that data can be inserted into one of the following column types:
9
 * - Date
10
 */
11
class Date implements ValidateRuleInterface
12
{
13
    use DateTypeTrait;
14
15
    protected $delimiterPattern = '[^[:punct:]]';
16
17
    /**
18
     * Returns a bool indicating if the value can be safely stored in a MySql Date column.
19
     *
20
     * @param object $subject
21
     * @param string $field
22
     *
23
     * @return bool
24
     */
25 36
    public function __invoke($subject, string $field): bool
26
    {
27 36
        $value = (string) $subject->$field;
28 36
        $dateParts = $this->extractDateParts($value, $this->delimiterPattern, true);
29
30 36
        return !is_null($dateParts) && checkdate($dateParts->month, $dateParts->day, $dateParts->year);
31
    }
32
}
33