Debian Codegraph @

lib/3rdParty/composer/nesbot/carbon/src/Carbon/WrapperClock.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * This file is part of the Carbon package.
7 *
8 * (c) Brian Nesbitt <[email protected]>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14 namespace Carbon;
15
16 use DateTime;
17 use DateTimeImmutable;
18 use DateTimeInterface;
19 use DateTimeZone;
20 use Psr\Clock\ClockInterface as PsrClockInterface;
21 use RuntimeException;
22 use Symfony\Component\Clock\ClockInterface;
23
24 final class WrapperClock implements ClockInterface
25 {
26 public function __construct(
27 private PsrClockInterface|Factory|DateTimeInterface $currentClock,
28 ) {
29 }
30
31 public function unwrap(): PsrClockInterface|Factory|DateTimeInterface
32 {
33 return $this->currentClock;
34 }
35
36 public function getFactory(): Factory
37 {
38 if ($this->currentClock instanceof Factory) {
39 return $this->currentClock;
40 }
41
42 if ($this->currentClock instanceof DateTime) {
43 $factory = new Factory();
44 $factory->setTestNowAndTimezone($this->currentClock);
45
46 return $factory;
47 }
48
49 if ($this->currentClock instanceof DateTimeImmutable) {
50 $factory = new FactoryImmutable();
51 $factory->setTestNowAndTimezone($this->currentClock);
52
53 return $factory;
54 }
55
56 $factory = new FactoryImmutable();
57 $factory->setTestNowAndTimezone(fn () => $this->currentClock->now());
58
59 return $factory;
60 }
61
62 private function nowRaw(): DateTimeInterface
63 {
64 if ($this->currentClock instanceof DateTimeInterface) {
65 return $this->currentClock;
66 }
67
68 if ($this->currentClock instanceof Factory) {
69 return $this->currentClock->__call('now', []);
70 }
71
72 return $this->currentClock->now();
73 }
74
75 public function now(): DateTimeImmutable
76 {
77 $now = $this->nowRaw();
78
79 return $now instanceof DateTimeImmutable
80 ? $now
81 : new CarbonImmutable($now);
82 }
83
84 /**
85 * @template T of CarbonInterface
86 *
87 * @param class-string<T> $class
88 *
89 * @return T
90 */
91 public function nowAs(string $class, DateTimeZone|string|int|null $timezone = null): CarbonInterface
92 {
93 $now = $this->nowRaw();
94 $date = $now instanceof $class ? $now : $class::instance($now);
95
96 return $timezone === null ? $date : $date->setTimezone($timezone);
97 }
98
99 public function nowAsCarbon(DateTimeZone|string|int|null $timezone = null): CarbonInterface
100 {
101 $now = $this->nowRaw();
102
103 return $now instanceof CarbonInterface
104 ? ($timezone === null ? $now : $now->setTimezone($timezone))
105 : $this->dateAsCarbon($now, $timezone);
106 }
107
108 private function dateAsCarbon(DateTimeInterface $date, DateTimeZone|string|int|null $timezone): CarbonInterface
109 {
110 return $date instanceof DateTimeImmutable
111 ? new CarbonImmutable($date, $timezone)
112 : new Carbon($date, $timezone);
113 }
114
115 public function sleep(float|int $seconds): void
116 {
117 if ($seconds === 0 || $seconds === 0.0) {
118 return;
119 }
120
121 if ($seconds < 0) {
122 throw new RuntimeException('Expected positive number of seconds, '.$seconds.' given');
123 }
124
125 if ($this->currentClock instanceof DateTimeInterface) {
126 $this->currentClock = $this->addSeconds($this->currentClock, $seconds);
127
128 return;
129 }
130
131 if ($this->currentClock instanceof ClockInterface) {
132 $this->currentClock->sleep($seconds);
133
134 return;
135 }
136
137 $this->currentClock = $this->addSeconds($this->currentClock->now(), $seconds);
138 }
139
140 public function withTimeZone(DateTimeZone|string $timezone): static
141 {
142 if ($this->currentClock instanceof ClockInterface) {
143 return new self($this->currentClock->withTimeZone($timezone));
144 }
145
146 $now = $this->currentClock instanceof DateTimeInterface
147 ? $this->currentClock
148 : $this->currentClock->now();
149
150 if (!($now instanceof DateTimeImmutable)) {
151 $now = clone $now;
152 }
153
154 if (\is_string($timezone)) {
155 $timezone = new DateTimeZone($timezone);
156 }
157
158 return new self($now->setTimezone($timezone));
159 }
160
161 private function addSeconds(DateTimeInterface $date, float|int $seconds): DateTimeInterface
162 {
163 $secondsPerHour = CarbonInterface::SECONDS_PER_MINUTE * CarbonInterface::MINUTES_PER_HOUR;
164 $hours = number_format(
165 floor($seconds / $secondsPerHour),
166 thousands_separator: '',
167 );
168 $microseconds = number_format(
169 ($seconds - $hours * $secondsPerHour) * CarbonInterface::MICROSECONDS_PER_SECOND,
170 thousands_separator: '',
171 );
172
173 if (!($date instanceof DateTimeImmutable)) {
174 $date = clone $date;
175 }
176
177 if ($hours !== '0') {
178 $date = $date->modify("$hours hours");
179 }
180
181 if ($microseconds !== '0') {
182 $date = $date->modify("$microseconds microseconds");
183 }
184
185 return $date;
186 }
187 }