Completed
Push — master ( 9af361...a55b85 )
by Rafał
09:23
created

AbstractContext::isDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Behat\Contexts;
6
7
use DateTime;
8
use Symfony\Component\PropertyAccess\PropertyAccess;
9
10
abstract class AbstractContext
11
{
12
    protected function fillObject($object, array $data)
13
    {
14
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
15
        foreach ($data as $column => $value) {
16
            if (is_string($value) && $this->isDate($value)) {
17
                $value = new DateTime($value);
18
            }
19
20
            $propertyAccessor->setValue($object, $column, $value);
21
        }
22
    }
23
24
    protected function isDate(string $date): bool
25
    {
26
        $dateTime = DateTime::createFromFormat('Y-m-d', $date);
27
28
        return $dateTime && $dateTime->format('Y-m-d') === $date;
29
    }
30
}
31