refactor: standardize design
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
101
lib/Providers/Personal/CollectionResource.php
Normal file
101
lib/Providers/Personal/CollectionResource.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderLocalChrono\Providers\Personal;
|
||||
|
||||
use KTXF\Chrono\Collection\CollectionMutableAbstract;
|
||||
|
||||
class CollectionResource extends CollectionMutableAbstract {
|
||||
|
||||
private ?string $userId = null;
|
||||
private ?string $collectionUuid = null;
|
||||
private bool $collectionEnabled = true;
|
||||
|
||||
public function __construct(
|
||||
string $provider = 'default',
|
||||
string|int $service = 'personal',
|
||||
) {
|
||||
parent::__construct($provider, $service);
|
||||
}
|
||||
|
||||
public function fromStore(array|object $data): self
|
||||
{
|
||||
if (is_object($data)) {
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
if (isset($data['cid'])) {
|
||||
$this->data[self::JSON_PROPERTY_IDENTIFIER] = (string) $data['cid'];
|
||||
} elseif (isset($data['_id'])) {
|
||||
if (is_object($data['_id']) && method_exists($data['_id'], '__toString')) {
|
||||
$this->data[self::JSON_PROPERTY_IDENTIFIER] = (string) $data['_id'];
|
||||
} elseif (is_array($data['_id']) && isset($data['_id']['$oid'])) {
|
||||
$this->data[self::JSON_PROPERTY_IDENTIFIER] = (string) $data['_id']['$oid'];
|
||||
} else {
|
||||
$this->data[self::JSON_PROPERTY_IDENTIFIER] = (string) $data['_id'];
|
||||
}
|
||||
}
|
||||
$this->userId = $data['uid'] ?? null;
|
||||
$this->collectionUuid = isset($data['uuid']) ? (string) $data['uuid'] : null;
|
||||
$this->getProperties()->fromStore($data);
|
||||
$this->data[self::JSON_PROPERTY_CREATED] = $data['createdOn'] ?? $data['created'] ?? null;
|
||||
$this->data[self::JSON_PROPERTY_MODIFIED] = $data['modifiedOn'] ?? $data['modified'] ?? null;
|
||||
$this->collectionEnabled = $data['enabled'] ?? true;
|
||||
$this->data[self::JSON_PROPERTY_SIGNATURE] = isset($data['signature']) ? md5((string) $data['signature']) : null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toStore(): array
|
||||
{
|
||||
$properties = $this->getProperties();
|
||||
|
||||
$data = array_filter([
|
||||
'uid' => $this->userId,
|
||||
'uuid' => $this->collectionUuid,
|
||||
'label' => $properties->getLabel(),
|
||||
'description' => $properties->getDescription(),
|
||||
'priority' => $properties->getPriority(),
|
||||
'visibility' => $properties->getVisibility(),
|
||||
'color' => $properties->getColor(),
|
||||
'createdOn' => $this->data[self::JSON_PROPERTY_CREATED] ?? null,
|
||||
'modifiedOn' => $this->data[self::JSON_PROPERTY_MODIFIED] ?? null,
|
||||
'signature' => $this->data[self::JSON_PROPERTY_SIGNATURE] ?? null,
|
||||
'enabled' => $this->collectionEnabled,
|
||||
], static fn($value) => $value !== null);
|
||||
|
||||
if ($this->identifier() !== null) {
|
||||
$data['cid'] = (string) $this->identifier();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function uuid(): ?string {
|
||||
return $this->collectionUuid;
|
||||
}
|
||||
|
||||
public function getEnabled(): bool {
|
||||
return (bool)$this->collectionEnabled;
|
||||
}
|
||||
|
||||
public function setEnabled(bool $value): self {
|
||||
$this->collectionEnabled = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getProperties(): CollectionProperties {
|
||||
if (!isset($this->properties)) {
|
||||
$this->properties = new CollectionProperties([]);
|
||||
}
|
||||
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user