|
2 | 2 |
|
3 | 3 | namespace OCP\AppFramework\Db; |
4 | 4 |
|
5 | | -use OC\Snowflake\Decoder; |
6 | | -use OC\Snowflake\Generator; |
7 | 5 | use OCP\Server; |
| 6 | +use OCP\Snowflake\IDecoder; |
| 7 | +use OCP\Snowflake\IGenerator; |
8 | 8 |
|
| 9 | +/** |
| 10 | + * @method string getId() |
| 11 | + */ |
9 | 12 | class SnowflakeAwareEntity extends Entity { |
| 13 | + /** @var string $id */ |
| 14 | + public $id; |
| 15 | + |
| 16 | + /** @var array<string, \OCP\DB\Types::*> */ |
| 17 | + private array $_fieldTypes = ['id' => 'string']; |
10 | 18 |
|
11 | 19 | #[\Override] |
12 | | - public function setId(): string { |
13 | | - $generator = Server::get(Generator::class); |
14 | | - $this->id = $generator->nextId(); |
| 20 | + public function setId(): void { |
| 21 | + if (empty($this->id)) { |
| 22 | + $generator = Server::get(IGenerator::class); |
| 23 | + $this->id = $generator->nextId(); |
| 24 | + $this->markFieldUpdated('id'); |
| 25 | + } |
15 | 26 | } |
16 | 27 |
|
17 | | - public function getCreatedAt(): \DateTimeImmutable { |
18 | | - $decoder = Server::get(Decoder::class); |
| 28 | + public function getCreatedAt(): ?\DateTimeImmutable { |
| 29 | + if (empty($this->id)) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + $decoder = Server::get(IDecoder::class); |
19 | 33 | return $decoder->decode($this->id)['created_at']; |
20 | 34 | } |
21 | 35 |
|
22 | 36 | public function getDecodedId(): array { |
23 | | - return Server::get(Decoder::class)->decode($this->id); |
| 37 | + if (empty($this->id)) { |
| 38 | + return []; |
| 39 | + } |
| 40 | + return Server::get(IDecoder::class)->decode($this->id); |
24 | 41 | } |
25 | 42 | } |
0 commit comments