- Merge latest commits and added virtual closures too

This commit is contained in:
mcndave committed 2022-05-15 03:50:34 +00:00
1 parent e14a3670fc
commit 724ab250ab
3 files changed
+54 -5

No files matched your search

+37 -2
View File
@@ -45,11 +45,27 @@ class RelationBuilder
return $dataset;
}
return $this->resolveRelation($name);
return $this->resolveRelation($name) ?: $this->resolveVirtual($name);
}
else {
return $this->instanciateEmptyObject($name, $this->resolver->searchFieldAnnotation($name, new Relation() ));
}
}
protected function resolveRelation(string $name) /* : object|EntityCollection */
protected function resolveVirtual(string $name) /* : bool|object|EntityCollection */
{
if (null !== ($virtual = $this->resolver->searchFieldAnnotation($name, new Annotation\Property\Virtual()))) {
if ($virtual->closure ?? false) {
return call_user_func_array($virtual->closure, [ $this->entity ]);
}
return call_user_func_array([ $this->entity, $virtual->method ], [ $this->entity ]);
}
return false;
}
protected function resolveRelation(string $name) /* : bool|object|EntityCollection */
{
if ( null !== ( $relation = $this->resolver->searchFieldAnnotation($name, new Relation() ) ) ) {
$this->orders = $this->resolver->searchFieldAnnotationList($name, new OrderBy() );
@@ -139,6 +155,25 @@ class RelationBuilder
return new $class();
}
protected function instanciateEmptyObject(string $name, Relation $relation) : object
{
switch( true ) {
case $relation->isOneToOne():
return $this->instanciateEmptyEntity($name, $relation);
case $relation->isOneToMany():
return ($relation->entity ?? $this->resolver->properties[$name]['type'])::entityCollection();
case $relation->isManyToMany():
extract($this->relationAnnotations($name, $relation));
return $relation->bridgeField ?? false ? $relation->bridge::entityCollection() : $relationRelation->entity::entityCollection();
}
return new $class();
}
protected function fetchFromDataset($name, ? array $data = null) /* object|bool */
{
$annotation = $this->resolver->searchFieldAnnotation($name, new Annotation\Property\Join) ?: