From 07ba3f611b432ce4752753c76604725df811db9c Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 6 Oct 2020 11:35:54 -0400 Subject: [PATCH] - Refractored Date/time fields - Added an implode function on EntityCollection --- src/Entity/Field/Date.php | 7 ++----- src/Entity/Field/Time.php | 5 +---- src/EntityCollection.php | 11 +++++++++++ src/EntityTrait.php | 4 ++++ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Entity/Field/Date.php b/src/Entity/Field/Date.php index fe0df70..c2c5e7e 100644 --- a/src/Entity/Field/Date.php +++ b/src/Entity/Field/Date.php @@ -4,11 +4,8 @@ namespace Ulmus\Entity\Field; class Date extends Datetime { - public function __toString() - { - return $this->format("Y-m-d"); - } - + public string $format = "Y-m-d"; + public function formatLocale(string $format) : string { return strftime($format, $this->getTimestamp()); diff --git a/src/Entity/Field/Time.php b/src/Entity/Field/Time.php index ecbdd11..df4db07 100644 --- a/src/Entity/Field/Time.php +++ b/src/Entity/Field/Time.php @@ -4,9 +4,6 @@ namespace Ulmus\Entity\Field; class Time extends Datetime { - public function __toString() - { - return $this->format("H:i:s"); - } + public string $format = "H:i:s"; } diff --git a/src/EntityCollection.php b/src/EntityCollection.php index 91ea2e5..77eafbf 100644 --- a/src/EntityCollection.php +++ b/src/EntityCollection.php @@ -107,4 +107,15 @@ class EntityCollection extends \ArrayObject { return $list; } + + public function implode(string $glue, ? Callable $callback = null) : string + { + $values = []; + + foreach($this as $item) { + $values[] = $callback ? $callback($item) : (string) $item; + } + + return implode($glue, $values); + } } diff --git a/src/EntityTrait.php b/src/EntityTrait.php index c19077e..6dcbf62 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -110,6 +110,10 @@ trait EntityTrait { $bridgeRelation = $bridgeEntity->searchFieldAnnotation($relation->field, new Relation() ); $relationRelation = $bridgeEntity->searchFieldAnnotation($relation->foreignField, new Relation() ); + if ($relationRelation === null) { + throw new \Exception("@Relation annotation not found for field `{$relation->foreignField}` in entity {$relation->bridge}"); + } + $repository = $relationRelation->entity()->repository(); $bridgeAlias = uniqid("bridge_");