58 lines
1.4 KiB
PHP
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]),
|
|
};
|
|
}
|
|
|
|
}
|