Browse Source

Make $responses always a collection

shalvah 3 years ago
parent
commit
b7a3f215b2
3 changed files with 22 additions and 5 deletions
  1. 20 0
      camel/BaseDTO.php
  2. 1 0
      camel/BaseDTOCollection.php
  3. 1 5
      camel/Extraction/ExtractedEndpointData.php

+ 20 - 0
camel/BaseDTO.php

@@ -21,4 +21,24 @@ class BaseDTO extends DataTransferObject implements Arrayable
 
         return new static($data);
     }
+
+    protected function parseArray(array $array): array
+    {
+        // Reimplementing here so our DTOCollection items can be recursively toArray'ed
+        foreach ($array as $key => $value) {
+            if ($value instanceof Arrayable) {
+                $array[$key] = $value->toArray();
+
+                continue;
+            }
+
+            if (! is_array($value)) {
+                continue;
+            }
+
+            $array[$key] = $this->parseArray($value);
+        }
+
+        return $array;
+    }
 }

+ 1 - 0
camel/BaseDTOCollection.php

@@ -39,6 +39,7 @@ class BaseDTOCollection extends Collection
         foreach ($items as $item) {
             $this->push(is_array($item) ? new static::$base($item) : $item);
         }
+        return $this;
     }
 
     public function toArray(): array

+ 1 - 5
camel/Extraction/ExtractedEndpointData.php

@@ -60,10 +60,7 @@ class ExtractedEndpointData extends BaseDTO
      */
     public array $fileParameters = [];
 
-    /**
-     * @var ResponseCollection|array
-     */
-    public $responses;
+    public ResponseCollection $responses;
 
     /**
      * @var array<string,\Knuckles\Camel\Extraction\ResponseField>
@@ -192,7 +189,6 @@ class ExtractedEndpointData extends BaseDTO
             'route', 'controller', 'method', 'auth',
         );
         $copy->metadata = $copy->metadata->except('groupName', 'groupDescription');
-        $copy->responses = $copy->responses->toArray();
 
         return $copy;
     }