66 lines
1.9 KiB
PHP
66 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Notes;
|
|
|
|
use Notes\Common\ReflectedClass;
|
|
use Psr\SimpleCache\CacheInterface;
|
|
|
|
class ObjectResolver {
|
|
public string $objectClass;
|
|
|
|
public ReflectedClass $reflectedClass;
|
|
|
|
public array $class;
|
|
|
|
public array $properties;
|
|
|
|
public array $methods;
|
|
|
|
public function __construct(string $objectClass, ? CacheInterface $cache = null)
|
|
{
|
|
$this->objectClass = $objectClass;
|
|
|
|
$this->reflectedClass = ObjectReflection::fromClass($objectClass, $cache)->reflectClass();
|
|
}
|
|
|
|
/**
|
|
* Transform an annotation into it's object's counterpart
|
|
*/
|
|
public function getAttributeListFromClassname(array|string $className, bool $throwOnError = true) : array
|
|
{
|
|
$list = [];
|
|
|
|
foreach((array) $className as $class) {
|
|
foreach ($this->reflectedClass->getAttributes() as $item) {
|
|
if ($item->object instanceof $class) {
|
|
$list[] = $item->object;
|
|
}
|
|
}
|
|
|
|
foreach ($this->reflectedClass->getProperties(true) as $property) {
|
|
foreach ($property->attributes as $item) {
|
|
if ($item->object instanceof $class) {
|
|
$list[$property->name] ??= [];
|
|
$list[$property->name][] = $item->object;
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ($this->reflectedClass->getMethods(true) as $method) {
|
|
foreach ($method->attributes as $item) {
|
|
if ($item->object instanceof $class) {
|
|
$list[$method->name] ??= [];
|
|
$list[$method->name][] = $item->object;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ( empty($list) ) {
|
|
if ($throwOnError) throw new \InvalidArgumentException("Class `$className` was not found within {$this->objectClass} uses statement (or it's children / traits)");
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
}
|