Completed
Push — master ( 7fe6b0...d8a5ea )
by Paweł
02:19
created

DateTime   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B processDateTime() 0 23 9
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Traits;
15
16
use DateTimeInterface;
17
use InvalidArgumentException;
18
19
/**
20
 * Trait DateTime.
21
 *
22
 * @package Wszetko\Sitemap\Traits
23
 */
24
trait DateTime
25
{
26
    /**
27
     * @param DateTimeInterface|string $dateTime
28
     * @param bool                     $required
29
     *
30
     * @return null|string
31
     */
32 82
    private function processDateTime($dateTime, $required = false): ?string
33
    {
34 82
        if (is_string($dateTime)) {
35 18
            $dateTime = date_create($dateTime);
36
        }
37
38 82
        if ($dateTime && (int) $dateTime->format('Y') < 0) {
39 4
            $dateTime = null;
40
        }
41
42 82
        if (!empty($dateTime)) {
43 70
            if (0 == $dateTime->format('H') &&
44 70
                0 == $dateTime->format('i') &&
45 70
                0 == $dateTime->format('s')) {
46 66
                $dateTime = $dateTime->format('Y-m-d');
47
            } else {
48 70
                $dateTime = $dateTime->format(DateTimeInterface::W3C);
49
            }
50 12
        } elseif ($required) {
51 4
            throw new InvalidArgumentException('Invalid date parameter.');
52
        }
53
54 78
        return $dateTime;
55
    }
56
}
57