Encoder
1 month ago
EncoderFactory
1 month ago
Repository
1 month ago
Table
1 month ago
Encoder.php
1 month ago
EncoderFactory.php
1 month ago
KeyValue.php
1 month ago
Option.php
1 month ago
OptionData.php
1 month ago
OptionDataFactory.php
1 month ago
OptionFactory.php
1 month ago
SiteOption.php
1 month ago
SiteOptionFactory.php
1 month ago
Table.php
1 month ago
Timestamp.php
1 month ago
Transaction.php
1 month ago
UserData.php
1 month ago
UserMeta.php
1 month ago
UserOption.php
1 month ago
Timestamp.php
53 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Storage; |
| 6 | |
| 7 | use AC\Expirable; |
| 8 | use LogicException; |
| 9 | |
| 10 | final class Timestamp implements Expirable, KeyValue |
| 11 | { |
| 12 | |
| 13 | private KeyValue $storage; |
| 14 | |
| 15 | public function __construct(KeyValue $storage) |
| 16 | { |
| 17 | $this->storage = $storage; |
| 18 | } |
| 19 | |
| 20 | public function is_expired(?int $timestamp = null): bool |
| 21 | { |
| 22 | if (null === $timestamp) { |
| 23 | $timestamp = time(); |
| 24 | } |
| 25 | |
| 26 | return $timestamp > (int)$this->get(); |
| 27 | } |
| 28 | |
| 29 | public function validate($value): bool |
| 30 | { |
| 31 | return (bool)preg_match('/^[1-9]\d*$/', (string)$value); |
| 32 | } |
| 33 | |
| 34 | public function get() |
| 35 | { |
| 36 | return $this->storage->get(); |
| 37 | } |
| 38 | |
| 39 | public function delete(): void |
| 40 | { |
| 41 | $this->storage->delete(); |
| 42 | } |
| 43 | |
| 44 | public function save($value): void |
| 45 | { |
| 46 | if ( ! $this->validate($value)) { |
| 47 | throw new LogicException('Value needs to be a positive integer.'); |
| 48 | } |
| 49 | |
| 50 | $this->storage->save((int)$value); |
| 51 | } |
| 52 | |
| 53 | } |