Skip to content

Commit 8d35e95

Browse files
committed
fixup! feat: extend Entity and adjust QBMapper to support Snowflake IDs
1 parent 1f5ad54 commit 8d35e95

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed

apps/files_versions/lib/Db/VersionEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct() {
4444

4545
public function jsonSerialize(): array {
4646
return [
47-
'id' => $this->id,
47+
'id' => $this->getId(),
4848
'file_id' => $this->fileId,
4949
'timestamp' => $this->timestamp,
5050
'size' => $this->size,

lib/public/AppFramework/Db/Entity.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,23 @@
1313
use function substr;
1414

1515
/**
16-
* @method int getId()
17-
* @method void setId(int $id)
1816
* @since 7.0.0
1917
* @psalm-consistent-constructor
2018
*/
2119
abstract class Entity {
22-
/** @var int $id */
23-
public $id = null;
24-
20+
private ?int $id = null;
2521
private array $_updatedFields = [];
26-
/** @var array<string, \OCP\DB\Types::*> */
22+
/** @psalm-param $_fieldTypes array<string, Types::*> */
2723
private array $_fieldTypes = ['id' => 'integer'];
2824

25+
public function setId($id): void {
26+
$this->id = $id;
27+
}
28+
29+
public function getId(): ?int {
30+
return $this->id;
31+
}
32+
2933
/**
3034
* Simple alternative constructor for building entities from a request
3135
* @param array $params the array which was obtained via $this->params('key')
@@ -64,7 +68,7 @@ public static function fromRow(array $row): static {
6468

6569

6670
/**
67-
* @return array<string, \OCP\DB\Types::*> with attribute and type
71+
* @return array<string, Types::*> with attribute and type
6872
* @since 7.0.0
6973
*/
7074
public function getFieldTypes(): array {
@@ -266,8 +270,8 @@ public function getUpdatedFields(): array {
266270
* that value once its being returned from the database
267271
*
268272
* @param string $fieldName the name of the attribute
269-
* @param \OCP\DB\Types::* $type the type which will be used to match a cast
270-
* @since 31.0.0 Parameter $type is now restricted to {@see \OCP\DB\Types} constants. The formerly accidentally supported types 'int'|'bool'|'double' are mapped to Types::INTEGER|Types::BOOLEAN|Types::FLOAT accordingly.
273+
* @param Types::* $type the type which will be used to match a cast
274+
* @since 31.0.0 Parameter $type is now restricted to {@see Types} constants. The formerly accidentally supported types 'int'|'bool'|'double' are mapped to Types::INTEGER|Types::BOOLEAN|Types::FLOAT accordingly.
271275
* @since 7.0.0
272276
*/
273277
protected function addType(string $fieldName, string $type): void {

lib/public/AppFramework/Db/QBMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function insert(Entity $entity): Entity {
114114

115115
if ($entity instanceof SnowflakeAwareEntity) {
116116
/** @psalm-suppress DocblockTypeContradiction */
117-
if ($entity->id === null) {
117+
if ($entity->getId() === null) {
118118
$entity->setId();
119119
}
120120
$qb->executeStatement();

lib/public/AppFramework/Db/SnowflakeAwareEntity.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,23 @@
2121
*/
2222
#[Consumable(since: '33.0.0')]
2323
abstract class SnowflakeAwareEntity extends Entity {
24-
/** @var string $id */
25-
public $id;
26-
24+
private ?string $id = null;
2725
protected ?Snowflake $snowflake = null;
2826

29-
/** @var array<string, \OCP\DB\Types::*> */
27+
/** @psalm-param $_fieldTypes array<string, Types::*> */
3028
private array $_fieldTypes = ['id' => Types::STRING];
3129

3230
/**
3331
* Automatically creates a snowflake ID
3432
*/
35-
public function setId(): void {
33+
#[\Override]
34+
public function setId($id = null): void {
3635
if ($this->id === null) {
3736
$this->id = Server::get(ISnowflakeGenerator::class)->nextId();
3837
$this->markFieldUpdated('id');
3938
}
4039
}
4140

42-
/**
43-
* @psalm-suppress InvalidReturnStatement
44-
* @psalm-suppress InvalidReturnType
45-
*/
46-
public function getId(): string {
47-
return $this->id;
48-
}
49-
5041
public function getCreatedAt(): ?\DateTimeImmutable {
5142
return $this->getSnowflake()?->getCreatedAt();
5243
}

0 commit comments

Comments
 (0)