Browse Source

Support spatie/dto 3

shalvah 3 years ago
parent
commit
c3a31009ba

+ 0 - 2
.gitattributes

@@ -6,5 +6,3 @@
 /.gitignore export-ignore
 /.travis.yml export-ignore
 /phpunit.xml export-ignore
-/README.md export-ignore
-/body-params.png export-ignore

+ 4 - 0
CHANGELOG.md

@@ -12,6 +12,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 
 ### Removals
 
+## 3.7.0 (Thursday, 22 July 2021)
+### Added
+- Allow installation of spatie/dto 3 [#282]((https://github.com/knuckleswtf/scribe/pull/282))
+
 ## 3.6.3 (Tuesday, 20 July 2021)
 ### Fixed
 - Stop Validator::make parsing from crashing unnecessarily [#281]((https://github.com/knuckleswtf/scribe/pull/281))

+ 2 - 1
camel/BaseDTO.php

@@ -2,10 +2,11 @@
 
 namespace Knuckles\Camel;
 
+use Illuminate\Contracts\Support\Arrayable;
 use Spatie\DataTransferObject\DataTransferObject;
 
 
-class BaseDTO extends DataTransferObject
+class BaseDTO extends DataTransferObject implements Arrayable
 {
     /**
      * @param array|self $data

+ 17 - 17
camel/BaseDTOCollection.php

@@ -3,49 +3,49 @@
 namespace Knuckles\Camel;
 
 use ArrayIterator;
+use Illuminate\Contracts\Support\Arrayable;
 use Illuminate\Support\Arr;
+use Illuminate\Support\Collection;
 use Spatie\DataTransferObject\DataTransferObjectCollection;
 
 /**
  * @template T of \Spatie\DataTransferObject\DataTransferObject
  */
-class BaseDTOCollection extends DataTransferObjectCollection
+class BaseDTOCollection extends Collection
 {
     /**
      * @var string The name of the base DTO class.
      */
     public static string $base = '';
 
-    public function __construct(array $collection = [])
+    public function __construct($items = [])
     {
         // Manually cast nested arrays
-        $collection = array_map(
-            function ($item) {
-                return is_array($item) ? new static::$base($item) : $item;
-            },
-            $collection
+        $items = array_map(
+            fn($item) => is_array($item) ? new static::$base($item) : $item,
+            $items instanceof Collection ? $items->toArray() : $items
         );
 
-        parent::__construct($collection);
+        parent::__construct($items);
     }
 
     /**
+     * Append items to the collection, mutating it.
+     *
      * @param T[]|array[] $items
      */
-    public function concat(array $items)
+    public function concat($items)
     {
         foreach ($items as $item) {
-            $this[] = is_array($item) ? new static::$base($item) : $item;
+            $this->push(is_array($item) ? new static::$base($item) : $item);
         }
     }
 
-    /**
-     * @param string $key
-     */
-    public function sortBy(string $key): void
+    public function toArray(): array
     {
-        $items = $this->items();
-        $items = Arr::sort($items, $key);
-        $this->iterator = new ArrayIterator(array_values($items));
+        return array_map(
+            fn($item) => $item instanceof Arrayable ? $item->toArray() : $item,
+            $this->items
+        );
     }
 }

+ 5 - 2
camel/Extraction/ExtractedEndpointData.php

@@ -185,13 +185,16 @@ class ExtractedEndpointData extends BaseDTO
      */
     public function forSerialisation()
     {
-        $this->metadata = $this->metadata->except('groupName', 'groupDescription');
-        return $this->except(
+        $copy = $this->except(
             // Get rid of all duplicate data
             'cleanQueryParameters', 'cleanUrlParameters', 'fileParameters', 'cleanBodyParameters',
             // and objects used only in extraction
             'route', 'controller', 'method', 'auth',
         );
+        $copy->metadata = $copy->metadata->except('groupName', 'groupDescription');
+        $copy->responses = $copy->responses->toArray();
+
+        return $copy;
     }
 
     public static function getFieldBindingForUrlParam(Route $route, string $paramName, string $default = null): ?string

+ 3 - 9
camel/Extraction/ResponseCollection.php

@@ -11,16 +11,10 @@ class ResponseCollection extends BaseDTOCollection
 {
     public static string $base = Response::class;
 
-    public function current(): Response
-    {
-        return parent::current();
-    }
-
     public function hasSuccessResponse(): bool
     {
-        return collect($this->toArray())
-                ->first(function ($response) {
-                    return ((string)$response['status'])[0] == '2';
-                }) !== null;
+        return $this->first(
+                fn($response) => strval($response->status)[0] == '2'
+            ) !== null;
     }
 }

+ 1 - 1
composer.dingo.json

@@ -32,7 +32,7 @@
         "nunomaduro/collision": "^3.0|^4.0|^5.0",
         "ramsey/uuid": "^3.8|^4.0",
         "shalvah/clara": "^3.0.2",
-        "spatie/data-transfer-object": "^2.6",
+        "spatie/data-transfer-object": "^2.6|^3.0",
         "symfony/var-exporter": "^4.0|^5.0",
         "symfony/yaml": "^4.0|^5.0"
     },

+ 7 - 1
composer.json

@@ -31,10 +31,16 @@
     "nunomaduro/collision": "^3.0|^4.0|^5.0",
     "ramsey/uuid": "^3.8|^4.0",
     "shalvah/clara": "^3.0.2",
-    "spatie/data-transfer-object": "^2.6",
+    "spatie/data-transfer-object": "^2.6|^3.0",
     "symfony/var-exporter": "^4.0|^5.0",
     "symfony/yaml": "^4.0|^5.0"
   },
+  "repositories": [
+    {
+      "type": "path",
+      "url": "../upgrader"
+    }
+  ],
   "require-dev": {
     "brianium/paratest": "^6.0",
     "dms/phpunit-arraysubset-asserts": "^0.2.0",

+ 1 - 1
src/Tools/Globals.php

@@ -4,7 +4,7 @@ namespace Knuckles\Scribe\Tools;
 
 class Globals
 {
-    public const SCRIBE_VERSION = '3.6.3';
+    public const SCRIBE_VERSION = '3.7.0';
 
     public static bool $shouldBeVerbose = false;
 }

+ 4 - 4
tests/Strategies/Responses/ResponseCallsTest.php

@@ -67,10 +67,10 @@ class ResponseCallsTest extends BaseLaravelTest
         $responses = $parsed->responses->toArray();
         $this->assertCount(1, $responses);
         $this->assertArraySubset([
-                "status" => 200,
-                "description" => null,
-                "content" => '{"filename":"scribe.php","filepath":"config","name":"cat.jpg"}',
-            ], $responses[0]);
+            "status" => 200,
+            "description" => null,
+            "content" => '{"filename":"scribe.php","filepath":"config","name":"cat.jpg"}',
+        ], $responses[0]);
     }
 
     /** @test */

+ 0 - 1
tests/Unit/PostmanCollectionWriterTest.php

@@ -3,7 +3,6 @@
 namespace Knuckles\Scribe\Tests\Unit;
 
 use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
-use Faker\Factory;
 use Knuckles\Camel\Output\OutputEndpointData;
 use Knuckles\Camel\Output\Parameter;
 use Knuckles\Scribe\Extracting\Extractor;