Browse Source

Sort responses by status code

shalvah 4 years ago
parent
commit
c524446e01

+ 13 - 2
camel/BaseDTOCollection.php

@@ -2,6 +2,7 @@
 
 namespace Knuckles\Camel;
 
+use Illuminate\Support\Arr;
 use Knuckles\Camel\Extraction\T;
 use Spatie\DataTransferObject\DataTransferObjectCollection;
 
@@ -13,7 +14,7 @@ class BaseDTOCollection extends DataTransferObjectCollection
     /**
      * @var string The name of the base DTO class.
      */
-    public static $base = '';
+    public static string $base = '';
 
     public function __construct(array $collection = [])
     {
@@ -31,10 +32,20 @@ class BaseDTOCollection extends DataTransferObjectCollection
     /**
      * @param T[] $items
      */
-    public function concat($items)
+    public function concat(array $items)
     {
         foreach ($items as $item) {
             $this[] = is_array($item) ? new static::$base($item) : $item;
         }
     }
+
+    /**
+     * @param string $key
+     */
+    public function sortBy(string $key): self
+    {
+        $items = $this->items();
+        $items = Arr::sort($items, $key);
+        return new static(array_values($items));
+    }
 }

+ 2 - 3
camel/Extraction/ResponseCollection.php

@@ -9,15 +9,14 @@ use Knuckles\Camel\BaseDTOCollection;
  */
 class ResponseCollection extends BaseDTOCollection
 {
-    /** @var string */
-    public static $base = Response::class;
+    public static string $base = Response::class;
 
     public function current(): Response
     {
         return parent::current();
     }
 
-    public function hasSuccessResponse()
+    public function hasSuccessResponse(): bool
     {
         return collect($this->toArray())
                 ->first(function ($response) {

+ 2 - 1
src/Extracting/Extractor.php

@@ -174,7 +174,8 @@ class Extractor
             // Responses from different strategies are all added, not overwritten
             $endpointData->responses->concat($results);
         });
-
+        // Ensure 200 responses come first
+        $endpointData->responses = $endpointData->responses->sortBy('status');
     }
 
     protected function fetchResponseFields(ExtractedEndpointData $endpointData, array $rulesToApply): void

+ 1 - 1
src/Extracting/Strategies/Responses/ResponseCalls.php

@@ -41,7 +41,7 @@ class ResponseCalls extends Strategy
         return $this->makeResponseCall($endpointData, $rulesToApply);
     }
 
-    public function makeResponseCall(ExtractedEndpointData $endpointData, array $rulesToApply)
+    public function makeResponseCall(ExtractedEndpointData $endpointData, array $rulesToApply): ?array
     {
         $this->configureEnvironment($rulesToApply);