1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Reflection\Stdlib\Scalar; |
11
|
|
|
|
12
|
|
|
use Railt\Reflection\Definition\ScalarDefinition; |
13
|
|
|
use Railt\Reflection\Document; |
14
|
|
|
use Railt\Reflection\Exception\TypeConflictException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class DateTimeScalar |
18
|
|
|
*/ |
19
|
|
|
class DateTimeScalar extends ScalarDefinition |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public const TYPE_NAME = 'DateTime'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public const TYPE_DESCRIPTION = 'The DateTime scalar conforms to the RFC 3339 profile of the ISO 8601 standard.'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* BooleanScalar constructor. |
33
|
|
|
* @param Document $document |
34
|
9 |
|
*/ |
35
|
|
|
public function __construct(Document $document) |
36
|
9 |
|
{ |
37
|
|
|
parent::__construct($document, self::TYPE_NAME); |
38
|
9 |
|
|
39
|
9 |
|
$this->withDescription(self::TYPE_DESCRIPTION); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param mixed $value |
44
|
|
|
* @return \DateTimeInterface |
45
|
|
|
* @throws TypeConflictException |
46
|
|
|
*/ |
47
|
|
|
public function parse($value): \DateTimeInterface |
48
|
|
|
{ |
49
|
|
|
if ($value instanceof \DateTimeInterface) { |
50
|
|
|
return $value; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (! \is_scalar($value)) { |
54
|
|
|
throw new TypeConflictException(\sprintf('Could not parse %s type', \gettype($value))); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$value = (string)parent::parse($value); |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
return new \DateTime($value); |
61
|
|
|
} catch (\Throwable $e) { |
62
|
|
|
throw new TypeConflictException($e->getMessage(), $e->getCode()); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param mixed $value |
68
|
|
|
* @return string |
69
|
|
|
* @throws TypeConflictException |
70
|
|
|
*/ |
71
|
|
|
public function serialize($value): string |
72
|
|
|
{ |
73
|
|
|
if ($value instanceof \DateTimeInterface) { |
74
|
|
|
return $value->format(\DateTime::RFC3339); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (! \is_scalar($value)) { |
78
|
|
|
throw new TypeConflictException(\sprintf('Could not serialize %s type', \gettype($value))); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$value = parent::parse($value); |
|
|
|
|
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
return (new \DateTime((string)$value))->format(\DateTime::RFC3339); |
85
|
|
|
} catch (\Throwable $e) { |
86
|
|
|
throw new TypeConflictException($e->getMessage(), $e->getCode()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
|
|
public function getLine(): int |
94
|
|
|
{ |
95
|
|
|
return 31; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.