Skip to content

Commit 4eaed44

Browse files
committed
fixup! feat(snowflake): extend Entity class to support snowflakes
1 parent 28c1bb2 commit 4eaed44

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

lib/public/AppFramework/Db/SnowflakeAwareEntity.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,41 @@
22

33
namespace OCP\AppFramework\Db;
44

5-
use OC\Snowflake\Decoder;
6-
use OC\Snowflake\Generator;
75
use OCP\Server;
6+
use OCP\Snowflake\IDecoder;
7+
use OCP\Snowflake\IGenerator;
88

9+
/**
10+
* @method string getId()
11+
*/
912
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'];
1018

1119
#[\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+
}
1526
}
1627

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);
1933
return $decoder->decode($this->id)['created_at'];
2034
}
2135

2236
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);
2441
}
2542
}

0 commit comments

Comments
 (0)