Files
server/shared/lib/Resource/Identifier/ResourceIdentifier.php
Sebastian Krupinski dfba1d43be
All checks were successful
JS Unit Tests / test (pull_request) Successful in 20s
Build Test / build (pull_request) Successful in 23s
PHP Unit Tests / test (pull_request) Successful in 48s
feat: entity move
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
2026-03-27 20:37:26 -04:00

58 lines
1.4 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\Identifier;
/**
* Provider-level resource identifier (depth 1)
*
* Format: {provider}
*/
class ResourceIdentifier implements ResourceIdentifierInterface {
public const SEPARATOR = ':';
public function __construct(
private readonly string $provider,
) {
}
public function provider(): string {
return $this->provider;
}
public function depth(): int {
return 1;
}
public function __toString(): string {
return $this->provider;
}
/**
* Parse a colon-separated identifier string and return the appropriate level class
*
* @return ResourceIdentifier|ServiceIdentifier|CollectionIdentifier|EntityIdentifier
*/
public static function fromString(string $identifier): ResourceIdentifierInterface {
$parts = explode(self::SEPARATOR, $identifier, 4);
if (count($parts) < 1 || $parts[0] === '') {
throw new \InvalidArgumentException("Invalid resource identifier: {$identifier}");
}
return match (count($parts)) {
4 => new EntityIdentifier($parts[0], $parts[1], $parts[2], $parts[3]),
3 => new CollectionIdentifier($parts[0], $parts[1], $parts[2]),
2 => new ServiceIdentifier($parts[0], $parts[1]),
default => new self($parts[0]),
};
}
}