- WIP on Attributes conversion from annotations
This commit is contained in:
1 parent
f4fffbd480
commit
a876ea9045
2 files changed
+242
-33
No files matched your search
+122
-33
@@ -28,59 +28,60 @@ class ObjectResolver {
|
||||
$this->resolveAnnotations();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Transform an annotation into it's object's counterpart
|
||||
*/
|
||||
public function getAnnotationFromClassname(string $className) : ? object
|
||||
public function getAttributeFromClassname(array|string $className, bool $throwOnError = true) : object|bool
|
||||
{
|
||||
if ( $name = $this->uses[$className] ?? false) {
|
||||
foreach(array_reverse($this->class['tags']) as $item) {
|
||||
if ( $item['tag'] === $name ) {
|
||||
foreach((array) $className as $class) {
|
||||
foreach (array_reverse($this->class['tags']) as $item) {
|
||||
if ($item['object'] instanceof $class) {
|
||||
return $this->instanciateAnnotationObject($item);
|
||||
}
|
||||
|
||||
foreach($this->properties as $property) {
|
||||
foreach($property['tags'] as $item) {
|
||||
if ( $item['tag'] === $name ) {
|
||||
return $this->instanciateAnnotationObject($item);
|
||||
foreach ($this->properties as $property) {
|
||||
foreach ($property['tags'] as $tag) {
|
||||
if ($item['object'] instanceof $class) {
|
||||
return $this->instanciateAnnotationObject($tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach($this->methods as $method) {
|
||||
foreach($method['tags'] as $item) {
|
||||
if ( $item['tag'] === $name ) {
|
||||
return $this->instanciateAnnotationObject($item);
|
||||
foreach ($this->methods as $method) {
|
||||
foreach ($method['tags'] as $tag) {
|
||||
if ($item['object'] instanceof $class) {
|
||||
return $this->instanciateAnnotationObject($tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new \Exception("Annotation `$className` could not be found within your object `{$this->objectClass}`");
|
||||
}
|
||||
else {
|
||||
throw new \InvalidArgumentException("Class `$className` was not found within {$this->objectClass} uses statement (or it's children / traits)");
|
||||
}
|
||||
|
||||
return null;
|
||||
if ($throwOnError) {
|
||||
throw new \Exception(sprintf("Annotation `%s` could not be found within your object `%s`.", implode(', ', (array)$className), $this->objectClass));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform an annotation into it's object's counterpart
|
||||
*/
|
||||
public function getAnnotationListFromClassname(string $className, bool $throwOnError = true) : array
|
||||
public function getAttributeListFromClassname(array|string $className, bool $throwOnError = true) : array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
if ( $name = $this->uses[$className] ?? false) {
|
||||
foreach($this->class['tags'] as $item) {
|
||||
if ($item['tag'] === $name) {
|
||||
foreach((array) $className as $class) {
|
||||
foreach ($this->class['tags'] as $item) {
|
||||
if ($item['object'] instanceof $class) {
|
||||
$list[] = $this->instanciateAnnotationObject($item);
|
||||
}
|
||||
}
|
||||
|
||||
foreach($this->properties as $property) {
|
||||
foreach($property['tags'] as $item) {
|
||||
if ( $item['tag'] === $name ) {
|
||||
foreach ($this->properties as $property) {
|
||||
foreach ($property['tags'] as $item) {
|
||||
if ($item['object'] instanceof $class) {
|
||||
$list[$property['name']] ??= [];
|
||||
|
||||
$list[$property['name']][] = $this->instanciateAnnotationObject($item);
|
||||
@@ -88,17 +89,101 @@ class ObjectResolver {
|
||||
}
|
||||
}
|
||||
|
||||
foreach($this->methods as $method) {
|
||||
foreach($method['tags'] as $item) {
|
||||
if ( $item['tag'] === $name ) {
|
||||
foreach ($this->methods as $method) {
|
||||
foreach ($method['tags'] as $item) {
|
||||
if ($item['object'] instanceof $class) {
|
||||
$list[$method['name']] ??= [];
|
||||
|
||||
$list[$method['name']][] = $this->instanciateAnnotationObject($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Will soon be deprecated in favour of PHP's native attributes
|
||||
* Transform an annotation into it's object's counterpart
|
||||
*/
|
||||
public function getAnnotationFromClassname(array|string $className, bool $throwOnError = true) : object|bool
|
||||
{
|
||||
foreach((array) $className as $class) {
|
||||
|
||||
if ( $name = $this->uses[$class] ?? false) {
|
||||
foreach (array_reverse($this->class['tags']) as $item) {
|
||||
if ($item['tag'] === $name) {
|
||||
return $this->instanciateAnnotationObject($item);
|
||||
}
|
||||
|
||||
foreach ($this->properties as $property) {
|
||||
foreach ($property['tags'] as $item) {
|
||||
if ($item['tag'] === $name) {
|
||||
return $this->instanciateAnnotationObject($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->methods as $method) {
|
||||
foreach ($method['tags'] as $item) {
|
||||
if ($item['tag'] === $name) {
|
||||
return $this->instanciateAnnotationObject($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($throwOnError) {
|
||||
throw new \Exception(sprintf("Annotation `%s` could not be found within your object `%s`.", implode(', ', (array)$className), $this->objectClass));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform an annotation into it's object's counterpart
|
||||
*/
|
||||
public function getAnnotationListFromClassname(array|string $className, bool $throwOnError = true) : array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach((array) $className as $class) {
|
||||
if ($name = $this->uses[$class] ?? false) {
|
||||
foreach ($this->class['tags'] as $item) {
|
||||
if ($item['tag'] === $name) {
|
||||
$list[] = $this->instanciateAnnotationObject($item);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->properties as $property) {
|
||||
foreach ($property['tags'] as $item) {
|
||||
if ($item['tag'] === $name) {
|
||||
$list[$property['name']] ??= [];
|
||||
|
||||
$list[$property['name']][] = $this->instanciateAnnotationObject($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->methods as $method) {
|
||||
foreach ($method['tags'] as $item) {
|
||||
if ($item['tag'] === $name) {
|
||||
$list[$method['name']] ??= [];
|
||||
|
||||
$list[$method['name']][] = $this->instanciateAnnotationObject($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty($list) ) {
|
||||
if ($throwOnError) throw new \InvalidArgumentException("Class `$className` was not found within {$this->objectClass} uses statement (or it's children / traits)");
|
||||
}
|
||||
|
||||
@@ -107,6 +192,10 @@ class ObjectResolver {
|
||||
|
||||
public function instanciateAnnotationObject(array $tagDefinition) : Annotation
|
||||
{
|
||||
if (isset($tagDefinition['object']) && $tagDefinition['object'] instanceof \Attribute) {
|
||||
return $tagDefinition['object'];
|
||||
}
|
||||
|
||||
$arguments = $this->extractArguments($tagDefinition['arguments']);
|
||||
|
||||
if ( false === $class = array_search($tagDefinition['tag'], $this->uses) ) {
|
||||
@@ -145,18 +234,18 @@ class ObjectResolver {
|
||||
protected function resolveAnnotations()
|
||||
{
|
||||
foreach($this->class['tags'] as $key => &$tag) {
|
||||
$tag['object'] = $this->instanciateAnnotationObject($tag);
|
||||
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
|
||||
}
|
||||
|
||||
foreach($this->properties as &$property) {
|
||||
foreach($property['tags'] as &$tag){
|
||||
$tag['object'] = $this->instanciateAnnotationObject($tag);
|
||||
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
|
||||
}
|
||||
}
|
||||
|
||||
foreach($this->methods as &$method) {
|
||||
foreach($method['tags'] as &$tag){
|
||||
$tag['object'] = $this->instanciateAnnotationObject($tag);
|
||||
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user