1 |
efrain |
1 |
# PSR Clock
|
|
|
2 |
|
|
|
3 |
This repository holds the interface for [PSR-20][psr-url].
|
|
|
4 |
|
|
|
5 |
Note that this is not a clock of its own. It is merely an interface that
|
|
|
6 |
describes a clock. See the specification for more details.
|
|
|
7 |
|
|
|
8 |
## Installation
|
|
|
9 |
|
|
|
10 |
```bash
|
|
|
11 |
composer require psr/clock
|
|
|
12 |
```
|
|
|
13 |
|
|
|
14 |
## Usage
|
|
|
15 |
|
|
|
16 |
If you need a clock, you can use the interface like this:
|
|
|
17 |
|
|
|
18 |
```php
|
|
|
19 |
<?php
|
|
|
20 |
|
|
|
21 |
use Psr\Clock\ClockInterface;
|
|
|
22 |
|
|
|
23 |
class Foo
|
|
|
24 |
{
|
|
|
25 |
private ClockInterface $clock;
|
|
|
26 |
|
|
|
27 |
public function __construct(ClockInterface $clock)
|
|
|
28 |
{
|
|
|
29 |
$this->clock = $clock;
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public function doSomething()
|
|
|
33 |
{
|
|
|
34 |
/** @var DateTimeImmutable $currentDateAndTime */
|
|
|
35 |
$currentDateAndTime = $this->clock->now();
|
|
|
36 |
// do something useful with that information
|
|
|
37 |
}
|
|
|
38 |
}
|
|
|
39 |
```
|
|
|
40 |
|
|
|
41 |
You can then pick one of the [implementations][implementation-url] of the interface to get a clock.
|
|
|
42 |
|
|
|
43 |
If you want to implement the interface, you can require this package and
|
|
|
44 |
implement `Psr\Clock\ClockInterface` in your code.
|
|
|
45 |
|
|
|
46 |
Don't forget to add `psr/clock-implementation` to your `composer.json`s `provide`-section like this:
|
|
|
47 |
|
|
|
48 |
```json
|
|
|
49 |
{
|
|
|
50 |
"provide": {
|
|
|
51 |
"psr/clock-implementation": "1.0"
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
```
|
|
|
55 |
|
|
|
56 |
And please read the [specification text][specification-url] for details on the interface.
|
|
|
57 |
|
|
|
58 |
[psr-url]: https://www.php-fig.org/psr/psr-20
|
|
|
59 |
[package-url]: https://packagist.org/packages/psr/clock
|
|
|
60 |
[implementation-url]: https://packagist.org/providers/psr/clock-implementation
|
|
|
61 |
[specification-url]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-20-clock.md
|