1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Spiral Framework package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Distribution\Internal; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @internal DateTimeIntervalFactory is an internal library class, please do not use it in your code. |
16
|
|
|
* @psalm-internal Spiral\Distribution |
17
|
|
|
*/ |
18
|
|
|
final class DateTimeIntervalFactory implements DateTimeIntervalFactoryInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private const ERROR_INVALID_INTERVAL_TYPE = 'The value of type `%s` is not a valid date interval type'; |
24
|
|
|
/** |
25
|
|
|
* @var DateTimeFactoryInterface|null |
26
|
|
|
*/ |
27
|
|
|
private $factory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param DateTimeFactoryInterface|null $factory |
31
|
|
|
*/ |
32
|
|
|
public function __construct(DateTimeFactoryInterface $factory = null) |
33
|
|
|
{ |
34
|
|
|
$this->factory = $factory ?? new DateTimeFactory(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
|
|
public function create($duration): \DateInterval |
41
|
|
|
{ |
42
|
|
|
try { |
43
|
|
|
return $this->createOrFail($duration); |
44
|
|
|
} catch (\InvalidArgumentException $e) { |
45
|
|
|
throw $e; |
46
|
|
|
} catch (\Throwable $e) { |
47
|
|
|
throw new \InvalidArgumentException($e->getMessage(), (int)$e->getCode(), $e); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
|
|
public function toDateTime(\DateInterval $interval): \DateTimeImmutable |
55
|
|
|
{ |
56
|
|
|
$now = $this->factory->now(); |
|
|
|
|
57
|
|
|
|
58
|
|
|
return $now->add($interval); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param mixed $duration |
63
|
|
|
* @return \DateInterval |
64
|
|
|
* @throws \Exception |
65
|
|
|
*/ |
66
|
|
|
private function createOrFail($duration): \DateInterval |
67
|
|
|
{ |
68
|
|
|
switch (true) { |
69
|
|
|
case $duration instanceof \DateInterval: |
70
|
|
|
return $duration; |
71
|
|
|
|
72
|
|
|
case $duration instanceof \DateTimeInterface: |
73
|
|
|
return $duration->diff($this->factory->now()); |
74
|
|
|
|
75
|
|
|
case \is_string($duration): |
76
|
|
|
return new \DateInterval($duration); |
77
|
|
|
|
78
|
|
|
case \is_int($duration): |
79
|
|
|
return new \DateInterval('PT' . $duration . 'S'); |
80
|
|
|
|
81
|
|
|
case $duration === null: |
82
|
|
|
return new \DateInterval('PT0S'); |
83
|
|
|
|
84
|
|
|
default: |
85
|
|
|
$type = \get_debug_type($duration); |
86
|
|
|
throw new \InvalidArgumentException(\sprintf(self::ERROR_INVALID_INTERVAL_TYPE, $type)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.