2019-12-10 19:32:44 +00:00
< ? php
namespace Notes ;
2024-05-09 19:43:08 +00:00
use Notes\Common\ReflectedClass ;
2023-01-26 13:29:59 +00:00
use Psr\SimpleCache\CacheInterface ;
2019-12-10 19:32:44 +00:00
class ObjectResolver {
public string $objectClass ;
2024-05-09 19:43:08 +00:00
public ReflectedClass $reflectedClass ;
2019-12-10 19:32:44 +00:00
public array $class ;
public array $properties ;
public array $methods ;
2024-05-09 19:43:08 +00:00
public function __construct ( string $objectClass , ? CacheInterface $cache = null )
2019-12-10 19:32:44 +00:00
{
$this -> objectClass = $objectClass ;
2024-05-09 19:43:08 +00:00
$this -> reflectedClass = ObjectReflection :: fromClass ( $objectClass , $cache ) -> reflectClass ();
2019-12-10 19:32:44 +00:00
}
2023-01-21 18:37:54 +00:00
2019-12-10 19:32:44 +00:00
/**
* Transform an annotation into it 's object' s counterpart
*/
2023-01-21 18:37:54 +00:00
public function getAttributeListFromClassname ( array | string $className , bool $throwOnError = true ) : array
2019-12-10 19:32:44 +00:00
{
$list = [];
2023-01-21 18:37:54 +00:00
foreach (( array ) $className as $class ) {
2024-05-09 19:43:08 +00:00
foreach ( $this -> reflectedClass -> getAttributes () as $item ) {
if ( $item -> object instanceof $class ) {
$list [] = $item -> object ;
2019-12-10 19:32:44 +00:00
}
2021-02-16 03:07:27 +00:00
}
2019-12-10 19:32:44 +00:00
2024-05-09 19:43:08 +00:00
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 ;
2019-12-10 19:32:44 +00:00
}
}
2021-02-16 03:07:27 +00:00
}
2019-12-10 19:32:44 +00:00
2024-05-09 19:43:08 +00:00
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 ;
2023-01-21 18:37:54 +00:00
}
}
}
}
if ( empty ( $list ) ) {
2019-12-10 19:32:44 +00:00
if ( $throwOnError ) throw new \InvalidArgumentException ( " Class ` $className ` was not found within { $this -> objectClass } uses statement (or it's children / traits) " );
}
return $list ;
}
}