DateTimeInterface   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 41
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A DataTypeFromString() 0 3 1
A jsonSerialize() 0 3 1
A DataTypeAsString() 0 3 1
A ObtainFromString() 0 21 3
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