You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Kartierung\Repository;
use Kartierung\Analyze\InvalidRepository;
use Kartierung\Attribute\Query;
use ReflectionClass;
class Repository
{
/**
* @template R of object
* @param class-string<R> $clazz
* @return R
* @noinspection PhpDocMissingThrowsInspection
*/
public function ofType(string $clazz): object
{
/** @noinspection PhpUnhandledExceptionInspection */
$refClass = new ReflectionClass($clazz);
/**
* @var R of object
* @phpstan-ignore varTag.nativeType
*/
return new class($refClass) {
/**
* @param ReflectionClass<R> $refClass
*/
public function __construct(
private readonly ReflectionClass $refClass
) {}
/**
* @param string $name
* @param list<mixed> $arguments
* @return mixed
*/
public function __call(string $name, array $arguments): mixed
{
$method = $this->refClass->getMethod($name);
$queryAttr = $method->getAttributes(Query::class);
if (count($queryAttr) !== 1) {
throw new InvalidRepository(
"Called method $name has an invalid number of Query attributes. Expected: 1"
);
}
return '';
}
};
}
}