From 0e8443b1c136dd4d6ee3e3609f88c15840e34ed0 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 10 Oct 2022 14:26:27 +0000 Subject: [PATCH] Prepping everything for a PHP 8 release --- src/Adapter/DefaultAdapterTrait.php | 2 +- src/Entity/Field/Datetime.php | 5 +++++ src/EntityCollection.php | 6 ++++++ src/EntityTrait.php | 12 ++++-------- src/Repository.php | 22 +++++++++++++++++++--- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/Adapter/DefaultAdapterTrait.php b/src/Adapter/DefaultAdapterTrait.php index 4fee52b..00f6fa0 100644 --- a/src/Adapter/DefaultAdapterTrait.php +++ b/src/Adapter/DefaultAdapterTrait.php @@ -90,6 +90,6 @@ trait DefaultAdapterTrait break; } - return $typeOnly ? $type : $type . ( $length ? "($length" . ( $precision ? ",$precision" : "" ) . ")" : "" ); + return $typeOnly ? $type : $type . ( isset($length) ? "($length" . ( ! empty($precision) ? ",$precision" : "" ) . ")" : "" ); } } \ No newline at end of file diff --git a/src/Entity/Field/Datetime.php b/src/Entity/Field/Datetime.php index 7f6cfd8..e5c3a5e 100644 --- a/src/Entity/Field/Datetime.php +++ b/src/Entity/Field/Datetime.php @@ -34,4 +34,9 @@ class Datetime extends \DateTime implements EntityObjectInterface { { return strftime($format, $this->getTimestamp()); } + + public function ageAt(null|\DateTime $dateTime) : int + { + return (int) ( (int) ($dateTime ?: new DateTime())->format('Ymd') - (int) $this->format('Ymd') ) / 10000; + } } diff --git a/src/EntityCollection.php b/src/EntityCollection.php index 3b09c40..9d4d165 100644 --- a/src/EntityCollection.php +++ b/src/EntityCollection.php @@ -308,6 +308,12 @@ class EntityCollection extends \ArrayObject { $this->append( $this->arrayToEntity($value) ); } else { + foreach ($this as $duplicate) { + if ($duplicate === $value) { + return; + } + } + parent::append($value); } } diff --git a/src/EntityTrait.php b/src/EntityTrait.php index 942686a..88e786b 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -2,14 +2,10 @@ namespace Ulmus; -use Ulmus\Repository, - Ulmus\Query, - Ulmus\Common\EntityResolver, - Ulmus\Common\EntityField; - +use Ulmus\{ Repository, Query, Common\EntityResolver, Common\EntityField }; use Ulmus\Annotation\Classes\{ Method, Table, Collation, }; use Ulmus\Annotation\Property\{ Field, Filter, FilterJoin, Relation, OrderBy, Where, OrWhere, Join, Virtual, On, WithJoin, }; -use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, Datetime as DateTime, Date, Time, Bigint, Tinyint, Text, Mediumtext, Longtext, Blob, Mediumblob, Longblob }; +use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, Datetime as DateTime, Date, Time, Bigint, Tinyint, Text, Mediumtext, Longtext, Blob, Mediumblob, Longblob, }; use Ulmus\Annotation\Property\Relation\{ Ignore as RelationIgnore }; trait EntityTrait { @@ -110,7 +106,7 @@ trait EntityTrait { public function resetVirtualProperties() : self { foreach($this->resolveEntity()->properties as $prop => $property) { - if ( ! $property['builtin'] ) { + if ( empty($property['builtin']) ) { foreach($property['tags'] as $tag) { if ( in_array(strtolower($tag['tag']), [ 'relation', 'join', 'virtual' ] ) ) { unset($this->$prop); @@ -273,7 +269,7 @@ trait EntityTrait { /** * @Ignore */ - public function jsonSerialize() : mixed + public function jsonSerialize() { return $this->entityGetDataset(); } diff --git a/src/Repository.php b/src/Repository.php index 085a3bf..2ab1441 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -294,7 +294,20 @@ class Repository $dataset = array_change_key_case($entity->entityGetDataset(false, true)); - return array_diff_assoc($oldValues ? $dataset : $array , $oldValues ? $array : $dataset ); + return array_udiff_assoc($oldValues ? $dataset : $array , $oldValues ? $array : $dataset, function($e1, $e2) { + if ( is_array($e1) ) { + if (is_array($e2)) { + return Ulmus::encodeArray($e1) !== Ulmus::encodeArray($e2); + } + else { + return false; + } + } + + return (string) $e1 !== (string) $e2; + }); + + # return array_diff_assoc($oldValues ? $dataset : $array , $oldValues ? $array : $dataset ); } @@ -606,11 +619,14 @@ class Repository $field = clone $condition->field; } - if ( $condition->field->entityClass === $entity ) { + /* @TODO FIX THIS ! + * + * if ( $condition->field->entityClass === $entity ) { + if ( $this->entityClass === $entity ) { $field->alias = $alias; $join->where(is_object($field) ? $field : $entity::field($field, $alias), $condition->value, $condition->operator); - } + }*/ } foreach($this->entityResolver->searchFieldAnnotationList($item, new FilterJoin() ) as $filter) {