From fed7d2e302c51aec31eee4a4380d0c244ad1a256 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 1 Nov 2023 09:55:19 -0400 Subject: [PATCH] - Some minors adjustments to match PHP 8.x coding styles --- src/EntityTrait.php | 10 ++++------ src/Query/Where.php | 8 ++++---- src/QueryBuilder.php | 6 +++--- src/Repository/RelationBuilder.php | 18 ++++++++++++------ 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/EntityTrait.php b/src/EntityTrait.php index b21efaf..fae60c8 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -62,7 +62,7 @@ trait EntityTrait { $data = json_decode($value, true); if (json_last_error() !== \JSON_ERROR_NONE) { - throw new \Exception(sprintf("JSON error while decoding in EntityTrait : '%s' given %s with field %s", json_last_error_msg(), $value, json_encode($field))); + throw new \Exception(sprintf("JSON error while decoding in EntityTrait : '%s' given %s", json_last_error_msg(), $value)); } $this->{$field['name']} = $data; @@ -123,11 +123,9 @@ trait EntityTrait { public function resetVirtualProperties() : self { foreach($this->resolveEntity()->properties as $prop => $property) { - if ( empty($property['builtin']) ) { - foreach($property['tags'] as $tag) { - if ( in_array(strtolower($tag['tag']), [ 'relation', 'join', 'virtual' ] ) ) { - unset($this->$prop); - } + foreach($property['tags'] as $tag) { + if ( in_array(strtolower($tag['tag']), [ 'relation', 'join', 'virtual' ] ) ) { + unset($this->$prop); } } } diff --git a/src/Query/Where.php b/src/Query/Where.php index 6f7e0ec..fd37cc6 100644 --- a/src/Query/Where.php +++ b/src/Query/Where.php @@ -16,7 +16,7 @@ class Where extends Fragment { const COMPARISON_IS = "IS"; const COMPARISON_NULL = "NULL"; - const SQL_TOKEN = "WHERE"; + const SQL_TOKEN = "WHERE"; public int $order = 50; @@ -35,7 +35,7 @@ class Where extends Fragment { $this->parent = $queryBuilder->where ?? null; } - public function add($field, $value, string $operator, string $condition, bool $not = false) : self + public function add($field, mixed $value, string $operator, string $condition, bool $not = false) : self { $this->validateFieldType($field); # $this->validateValueType($value); @@ -73,7 +73,7 @@ class Where extends Fragment { ]); } - protected function whereCondition($field, $value, string $operator = self::OPERATOR_EQUAL, string $condition = self::CONDITION_AND, bool $not = false) { + protected function whereCondition($field, mixed $value, string $operator = self::OPERATOR_EQUAL, string $condition = self::CONDITION_AND, bool $not = false) { return new class($this->queryBuilder, $field, $value, $operator, $condition, $not) { public mixed $value; @@ -123,7 +123,7 @@ class Where extends Fragment { $stack = []; if ($this->value) { - foreach ($this->value as $item) { + foreach($this->value as $item) { $stack[] = $this->filterValue($item); } } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 1f43f81..f91e351 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -198,9 +198,9 @@ class QueryBuilder implements Query\QueryBuilderInterface public function where(string|\Stringable $field, mixed $value, string $operator = Query\Where::OPERATOR_EQUAL, string $condition = Query\Where::CONDITION_AND, bool $not = false) : self { # Empty IN case - #if ( [] === $value ) { - # return $this; - #} + # if ( [] === $value ) { + # return $this; + # } if ( $this->where ?? false ) { $where = $this->where; diff --git a/src/Repository/RelationBuilder.php b/src/Repository/RelationBuilder.php index a9cdc39..796da0d 100644 --- a/src/Repository/RelationBuilder.php +++ b/src/Repository/RelationBuilder.php @@ -47,7 +47,7 @@ class RelationBuilder } } - public function searchRelation(string $name) : object|bool + public function searchRelation(string $name) : mixed { # Resolve relations here if one is called if ( $this->entity->isLoaded() ) { @@ -55,7 +55,13 @@ class RelationBuilder return $dataset; } - return $this->resolveRelation($name) ?: $this->resolveVirtual($name) ?: false; + if ( false !== $value = $this->resolveRelation($name) ) { + return $value; + } + elseif ( false !== $value = $this->resolveVirtual($name) ) { + return $value; + } + } else { if ( $relation = $this->resolver->searchFieldAnnotation($name, [ Attribute\Property\Relation::class , Relation::class ] ) ) { @@ -69,7 +75,7 @@ class RelationBuilder return false; } - protected function resolveVirtual(string $name) : bool|object + protected function resolveVirtual(string $name) : mixed { if (null !== ($virtual = $this->resolver->searchFieldAnnotation($name, [ Attribute\Property\Virtual::class, Annotation\Property\Virtual::class ]))) { if ($virtual->closure ?? false) { @@ -82,7 +88,7 @@ class RelationBuilder return false; } - protected function resolveRelation(string $name) : bool|object + protected function resolveRelation(string $name) : mixed { if ( null !== ( $relation = $this->resolver->searchFieldAnnotation($name, [ Attribute\Property\Relation::class, Relation::class ] ) ) ) { $this->orders = $this->resolver->searchFieldAnnotationList($name, [ Attribute\Property\OrderBy::class, OrderBy::class ] ); @@ -109,14 +115,14 @@ class RelationBuilder $this->entity->eventExecute(Event\EntityRelationLoadInterface::class, $name, $this->repository); - return call_user_func([ $this->repository, $relation->function() ]); +return call_user_func([ $this->repository, $relation->function() ]); case $relation->isManyToMany(): $this->manyToMany($name, $relation, $relationRelation); $this->entity->eventExecute(Event\EntityRelationLoadInterface::class, $name, $this->repository); - $results = call_user_func([ $this->repository, 'loadAll' ]); + $results = call_user_func([ $this->repository, $relationRelation->function() ]); if ($relation->bridgeField ?? false) { $collection = $relation->bridge::entityCollection();