141 lines
3.9 KiB
PHP
141 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace KTXF\Resource\Provider\Node;
|
|
|
|
use DateTimeImmutable;
|
|
use KTXF\Resource\Identifier\CollectionIdentifier;
|
|
use KTXF\Resource\Identifier\CollectionIdentifierInterface;
|
|
use KTXF\Resource\Identifier\EntityIdentifierInterface;
|
|
use KTXF\Resource\Identifier\ResourceIdentifier;
|
|
use KTXF\Resource\Identifier\ServiceIdentifier;
|
|
|
|
/**
|
|
* Abstract Node Base Class
|
|
*
|
|
* Provides common implementation for all resource nodes
|
|
*
|
|
* @since 2025.05.01
|
|
*/
|
|
abstract class NodeBaseAbstract implements NodeBaseInterface {
|
|
|
|
/**
|
|
* Internal data storage
|
|
*/
|
|
protected string $type = 'resource.node';
|
|
protected array $data = [];
|
|
|
|
public function __construct(
|
|
protected readonly string $provider,
|
|
protected readonly string|int $service,
|
|
) {
|
|
$this->data = [
|
|
static::PROPERTY_TYPE => $this->type,
|
|
static::PROPERTY_PROVIDER => $this->provider,
|
|
static::PROPERTY_SERVICE => $this->service,
|
|
static::PROPERTY_COLLECTION => null,
|
|
static::PROPERTY_IDENTIFIER => null,
|
|
static::PROPERTY_SIGNATURE => null,
|
|
static::PROPERTY_CREATED => null,
|
|
static::PROPERTY_MODIFIED => null,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function jsonSerialize(): array {
|
|
$data = $this->data;
|
|
$data['provider'] = new ResourceIdentifier($this->data[static::PROPERTY_PROVIDER]);
|
|
$data['service'] = new ServiceIdentifier($this->data[static::PROPERTY_PROVIDER], $this->data[static::PROPERTY_SERVICE]);
|
|
$data['collection'] = isset($this->data[static::PROPERTY_COLLECTION])
|
|
? new CollectionIdentifier($this->data[static::PROPERTY_PROVIDER], $this->data[static::PROPERTY_SERVICE], $this->data[static::PROPERTY_COLLECTION])
|
|
: null;
|
|
$data['identifier'] = $this->nodeIdentifier();
|
|
$data[static::PROPERTY_PROPERTIES] = $this->getProperties()->jsonSerialize();
|
|
return $data;
|
|
}
|
|
|
|
abstract protected function nodeIdentifier(): CollectionIdentifierInterface|EntityIdentifierInterface|null;
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function type(): string {
|
|
return $this->data[static::PROPERTY_TYPE];
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function urn(): string {
|
|
return (string) $this->nodeIdentifier();
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function provider(): string {
|
|
return $this->data[static::PROPERTY_PROVIDER];
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function service(): string|int {
|
|
return $this->data[static::PROPERTY_SERVICE];
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function collection(): string|int|null {
|
|
return $this->data[static::PROPERTY_COLLECTION] ?? null;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function identifier(): string|int|null {
|
|
return $this->data[static::PROPERTY_IDENTIFIER] ?? null;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function signature(): string|null {
|
|
return isset($this->data[static::PROPERTY_SIGNATURE])
|
|
? (string)$this->data[static::PROPERTY_SIGNATURE]
|
|
: null;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function created(): DateTimeImmutable|null {
|
|
return isset($this->data[static::PROPERTY_CREATED])
|
|
? new DateTimeImmutable($this->data[static::PROPERTY_CREATED])
|
|
: null;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function modified(): DateTimeImmutable|null {
|
|
return isset($this->data[static::PROPERTY_MODIFIED])
|
|
? new DateTimeImmutable($this->data[static::PROPERTY_MODIFIED])
|
|
: null;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
abstract public function getProperties(): NodePropertiesBaseInterface;
|
|
}
|