DateTimeInterface::ObtainFromString()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 2
dl 0
loc 21
ccs 9
cts 9
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftObject\SchemaOrg\DataTypes;
8
9
use DateTimeImmutable;
10
use DateTimeZone;
11
12
abstract class DateTimeInterface extends DateTimeImmutable implements DataType
13
{
14 52
    public function DataTypeAsString() : string
15
    {
16 52
        return $this->format(static::ObtainFormat());
17
    }
18
19 52
    public function jsonSerialize() : string
20
    {
21 52
        return $this->DataTypeAsString();
22
    }
23
24 6
    public static function DataTypeFromString(string $value) : DataType
25
    {
26 6
        return static::ObtainFromString($value);
27
    }
28
29 12
    public static function ObtainFromString(
30
        string $value,
31
        DateTimeZone $tz = null
32
    ) : DateTimeInterface {
33 12
        $out = new static($value);
34 6
        $format = static::ObtainFormat();
35
36 6
        if ( ! is_null($tz)) {
37 3
            $out = $out->setTimezone($tz);
38
        }
39
40 6
        $out = new static($out->format($format));
41
42 6
        if ( ! is_null($tz)) {
43
            /**
44
            * @var DateTimeInterface
45
            */
46 3
            $out = $out->setTimezone($tz);
47
        }
48
49 6
        return $out;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $out could return the type false which is incompatible with the type-hinted return SignpostMarv\DaftObject\...Types\DateTimeInterface. Consider adding an additional type-check to rule them out.
Loading history...
50
    }
51
52
    abstract protected static function ObtainFormat() : string;
53
}
54