Debian Codegraph @

lib/3rdParty/composer/symfony/clock/ClockAwareTrait.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Clock;
13
14 use Psr\Clock\ClockInterface;
15 use Symfony\Contracts\Service\Attribute\Required;
16
17 /**
18 * A trait to help write time-sensitive classes.
19 *
20 * @author Nicolas Grekas <[email protected]>
21 */
22 trait ClockAwareTrait
23 {
24 private readonly ClockInterface $clock;
25
26 #[Required]
27 public function setClock(ClockInterface $clock): void
28 {
29 $this->clock = $clock;
30 }
31
32 protected function now(): DatePoint
33 {
34 $now = ($this->clock ??= new Clock())->now();
35
36 return $now instanceof DatePoint ? $now : DatePoint::createFromInterface($now);
37 }
38 }