Files
server/shared/lib/SystemStore/StoreReference.php
T
2026-08-28 20:47:27 -04:00

63 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXF\SystemStore;
use InvalidArgumentException;
/**
* Provider and service selected for one logical tenant system store.
*/
final readonly class StoreReference
{
public function __construct(
public string $provider,
public string|int $service,
public string $namespace,
) {
if ($this->provider === '') {
throw new InvalidArgumentException('System-store provider cannot be empty');
}
if ($this->service === '') {
throw new InvalidArgumentException('System-store service cannot be empty');
}
if (
$this->namespace === ''
|| str_starts_with($this->namespace, '/')
|| str_ends_with($this->namespace, '/')
|| str_contains($this->namespace, '\\')
|| str_contains($this->namespace, "\0")
|| in_array('', explode('/', $this->namespace), true)
|| in_array('.', explode('/', $this->namespace), true)
|| in_array('..', explode('/', $this->namespace), true)
) {
throw new InvalidArgumentException('System-store namespace must be a normalized relative key');
}
}
public static function fromArray(array $data): self
{
$provider = $data['provider'] ?? null;
$service = $data['service'] ?? null;
$namespace = $data['namespace'] ?? null;
if (!is_string($provider) || (!is_string($service) && !is_int($service)) || !is_string($namespace)) {
throw new InvalidArgumentException('Invalid system-store reference');
}
return new self($provider, $service, $namespace);
}
public function toArray(): array
{
return [
'provider' => $this->provider,
'service' => $this->service,
'namespace' => $this->namespace,
];
}
}