From 1469d167c4f50d379390f92492f9169dfd2eb636 Mon Sep 17 00:00:00 2001
From: Dave Mc Nicoll <info@mcnd.ca>
Date: Mon, 29 Mar 2021 20:01:31 +0000
Subject: [PATCH] - Added a closure on key to EntityCollection buildArray
 method. - Some bugfixes done.

---
 src/Common/EntityResolver.php |  8 +++++---
 src/EntityCollection.php      | 10 ++++++----
 src/Repository.php            |  4 ++--
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/Common/EntityResolver.php b/src/Common/EntityResolver.php
index 04f93c8..c5d588b 100644
--- a/src/Common/EntityResolver.php
+++ b/src/Common/EntityResolver.php
@@ -238,7 +238,7 @@ class EntityResolver {
     /**
      * Transform an annotation into it's object's counterpart
      */
-    public function getAnnotationFromClassname(string $className) : ? object
+    public function getAnnotationFromClassname(string $className, bool $throwError = true) : ? object
     {
         if ( $name = $this->uses[$className] ?? false ) {
 
@@ -264,9 +264,11 @@ class EntityResolver {
                 }
             }
 
-            throw new \TypeError("Annotation `$className` could not be found within your object `{$this->entityClass}`");
+            if ($throwError) {
+                throw new \TypeError("Annotation `$className` could not be found within your object `{$this->entityClass}`");
+            }
         }
-        else {
+        elseif ($throwError) {
             throw new \InvalidArgumentException("Class `$className` was not found within {$this->entityClass} uses statement (or it's children / traits)");
         }
 
diff --git a/src/EntityCollection.php b/src/EntityCollection.php
index 2fb8120..db7be33 100644
--- a/src/EntityCollection.php
+++ b/src/EntityCollection.php
@@ -181,25 +181,27 @@ class EntityCollection extends \ArrayObject {
         return $return ?? null;
     }
 
-    public function buildArray(string $keyColumn, /* string|callable|null */ $value = null) : array
+    public function buildArray(/* string|callable|null */ $keyColumn, /* string|callable|null */ $value = null) : array
     {
         $list = [];
 
         foreach($this as $item) {
+            $key = $keyColumn instanceof \Closure ? $keyColumn($item) : $item->$keyColumn;
+
             switch (true) {
                 case is_null($value):
-                    $list[] = $item->$keyColumn;
+                    $list[] = $key;
                     break;
 
                 case is_callable($value):
-                    $list[$item->$keyColumn] = $value($item);
+                    $list[$key] = $value($item);
                     break;
 
                 case is_object($value):
                 case is_string($value):
                     $value = (string) $value;
 
-                    $list[$item->$keyColumn] = $item->$value;
+                    $list[$key] = $item->$value;
                     break;
             }
         }
diff --git a/src/Repository.php b/src/Repository.php
index 9f1f97d..cfa5e1f 100644
--- a/src/Repository.php
+++ b/src/Repository.php
@@ -79,12 +79,12 @@ class Repository
         return Ulmus::runSelectQuery($this->queryBuilder, $this->adapter)->fetchColumn(0);
     }
 
-    protected function deleteOne()
+    public function deleteOne()
     {
         return $this->limit(1)->deleteSqlQuery()->runQuery();
     }
 
-    protected function deleteAll()
+    public function deleteAll()
     {
         return $this->deleteSqlQuery()->runQuery();
     }