Skip to content

Commit

Permalink
embeddedMetadata: fix bug if other is null in the database
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Oct 3, 2023
1 parent af9befd commit 76dc9da
Showing 1 changed file with 49 additions and 11 deletions.
60 changes: 49 additions & 11 deletions packages/file-association-entity/src/EmbeddedMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EmbeddedMetadata implements RawMetadataInterface, \IteratorAggregate
/**
* @var array<string,int|string|bool|null>
*/
private array $other = [];
private ?array $other = [];

/**
* Indicates if the file is present. It is assumed that a file is present
Expand All @@ -46,6 +46,49 @@ public function isFilePresent(): bool
return $this->type !== null;
}

private function unsetOther(string $key): void
{
if ($this->other === null) {
$this->other = [];
}

unset($this->other[$key]);
}

private function setOther(string $key, int|string|bool|null $value): void
{
if ($this->other === null) {
$this->other = [];
}

$this->other[$key] = $value;
}

private function getOther(string $key): int|string|bool|null
{
if ($this->other === null) {
$this->other = [];
}

if (!array_key_exists($key, $this->other)) {
throw new MetadataNotFoundException($key);
}

return $this->other[$key];
}

/**
* @return array<string,int|string|bool|null>
*/
private function getOthers(): array
{
if ($this->other === null) {
$this->other = [];
}

return $this->other;
}

public function clear(): void
{
$this->name = null;
Expand All @@ -68,7 +111,7 @@ public function getIterator(): \Traversable
yield Constants::MEDIA_WIDTH => $this->width;
yield Constants::MEDIA_HEIGHT => $this->height;

yield from $this->other;
yield from $this->getOthers();
}

public function get(string $key): int|string|bool|null
Expand All @@ -82,7 +125,7 @@ public function get(string $key): int|string|bool|null
: null,
Constants::MEDIA_WIDTH => $this->width,
Constants::MEDIA_HEIGHT => $this->height,
default => $this->other[$key] ?? throw new MetadataNotFoundException($key),
default => $this->getOther($key),
};
}

Expand All @@ -106,7 +149,7 @@ public function set(string $key, int|string|bool|null $value): void
: null,
Constants::MEDIA_WIDTH => $this->width = $value !== null ? (int) $value : null,
Constants::MEDIA_HEIGHT => $this->height = $value !== null ? (int) $value : null,
default => $this->other[$key] = $value,
default => $this->setOther($key, $value),
};
}

Expand All @@ -119,15 +162,10 @@ public function delete(string $key): void
Constants::FILE_MODIFICATION_TIME => $this->modificationTime = null,
Constants::MEDIA_WIDTH => $this->width = null,
Constants::MEDIA_HEIGHT => $this->height = null,
default => $this->unset($key)
default => $this->unsetOther($key)
};
}

private function unset(string $key): void
{
unset($this->other[$key]);
}

public function merge(iterable $metadata): void
{
foreach ($metadata as $key => $value) {
Expand All @@ -137,6 +175,6 @@ public function merge(iterable $metadata): void

public function count(): int
{
return count($this->other) + 6;
return count($this->getOthers()) + 6;
}
}

0 comments on commit 76dc9da

Please sign in to comment.