From c3a31009ba4a16ef5b53425c1c752e310334c6f8 Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 22 Jul 2021 13:05:17 +0100 Subject: [PATCH 1/4] Support spatie/dto 3 --- .gitattributes | 2 -- CHANGELOG.md | 4 +++ camel/BaseDTO.php | 3 +- camel/BaseDTOCollection.php | 34 +++++++++---------- camel/Extraction/ExtractedEndpointData.php | 7 ++-- camel/Extraction/ResponseCollection.php | 12 ++----- composer.dingo.json | 2 +- composer.json | 8 ++++- src/Tools/Globals.php | 2 +- .../Responses/ResponseCallsTest.php | 8 ++--- tests/Unit/PostmanCollectionWriterTest.php | 1 - 11 files changed, 44 insertions(+), 39 deletions(-) diff --git a/.gitattributes b/.gitattributes index 88e96eb7..c3ce6a26 100644 --- a/.gitattributes +++ b/.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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b7e207d..de149abf 100644 --- a/CHANGELOG.md +++ b/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)) diff --git a/camel/BaseDTO.php b/camel/BaseDTO.php index 91881e4d..39645beb 100644 --- a/camel/BaseDTO.php +++ b/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 diff --git a/camel/BaseDTOCollection.php b/camel/BaseDTOCollection.php index 23b29277..b04e6818 100644 --- a/camel/BaseDTOCollection.php +++ b/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 + ); } } \ No newline at end of file diff --git a/camel/Extraction/ExtractedEndpointData.php b/camel/Extraction/ExtractedEndpointData.php index b8652888..7a1bc3f3 100644 --- a/camel/Extraction/ExtractedEndpointData.php +++ b/camel/Extraction/ExtractedEndpointData.php @@ -185,13 +185,16 @@ public function normalizeResourceParamName(string $uri, Route $route): string */ 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 diff --git a/camel/Extraction/ResponseCollection.php b/camel/Extraction/ResponseCollection.php index 4f0df98e..14014c02 100644 --- a/camel/Extraction/ResponseCollection.php +++ b/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; } } \ No newline at end of file diff --git a/composer.dingo.json b/composer.dingo.json index b3444968..5702ce05 100644 --- a/composer.dingo.json +++ b/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" }, diff --git a/composer.json b/composer.json index 6b2a3085..8956e988 100644 --- a/composer.json +++ b/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", diff --git a/src/Tools/Globals.php b/src/Tools/Globals.php index 7d21d93b..09267ed2 100644 --- a/src/Tools/Globals.php +++ b/src/Tools/Globals.php @@ -4,7 +4,7 @@ class Globals { - public const SCRIBE_VERSION = '3.6.3'; + public const SCRIBE_VERSION = '3.7.0'; public static bool $shouldBeVerbose = false; } diff --git a/tests/Strategies/Responses/ResponseCallsTest.php b/tests/Strategies/Responses/ResponseCallsTest.php index d40d577e..0dbea296 100644 --- a/tests/Strategies/Responses/ResponseCallsTest.php +++ b/tests/Strategies/Responses/ResponseCallsTest.php @@ -67,10 +67,10 @@ public function can_upload_file_parameters_in_response_calls() $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 */ diff --git a/tests/Unit/PostmanCollectionWriterTest.php b/tests/Unit/PostmanCollectionWriterTest.php index 1f125b43..bdc011fa 100644 --- a/tests/Unit/PostmanCollectionWriterTest.php +++ b/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; From b7a3f215b2c22ba79fd9c706877ee0e716128e13 Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 22 Jul 2021 13:16:01 +0100 Subject: [PATCH 2/4] Make $responses always a collection --- camel/BaseDTO.php | 20 ++++++++++++++++++++ camel/BaseDTOCollection.php | 1 + camel/Extraction/ExtractedEndpointData.php | 6 +----- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/camel/BaseDTO.php b/camel/BaseDTO.php index 39645beb..1551756a 100644 --- a/camel/BaseDTO.php +++ b/camel/BaseDTO.php @@ -21,4 +21,24 @@ public static function create($data): self 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; + } } \ No newline at end of file diff --git a/camel/BaseDTOCollection.php b/camel/BaseDTOCollection.php index b04e6818..046f7316 100644 --- a/camel/BaseDTOCollection.php +++ b/camel/BaseDTOCollection.php @@ -39,6 +39,7 @@ public function concat($items) foreach ($items as $item) { $this->push(is_array($item) ? new static::$base($item) : $item); } + return $this; } public function toArray(): array diff --git a/camel/Extraction/ExtractedEndpointData.php b/camel/Extraction/ExtractedEndpointData.php index 7a1bc3f3..c8b9c6ce 100644 --- a/camel/Extraction/ExtractedEndpointData.php +++ b/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 @@ -192,7 +189,6 @@ public function forSerialisation() 'route', 'controller', 'method', 'auth', ); $copy->metadata = $copy->metadata->except('groupName', 'groupDescription'); - $copy->responses = $copy->responses->toArray(); return $copy; } From d49f4402e4fa6099a67c535cda6a37cb36033c78 Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 22 Jul 2021 13:19:51 +0100 Subject: [PATCH 3/4] Fix composer.json --- composer.json | 6 ------ 1 file changed, 6 deletions(-) diff --git a/composer.json b/composer.json index 8956e988..e14056dc 100644 --- a/composer.json +++ b/composer.json @@ -35,12 +35,6 @@ "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", From 87b8f234a0f4b31202f2195bf4d3bfb7b92e218b Mon Sep 17 00:00:00 2001 From: shalvah Date: Thu, 22 Jul 2021 13:27:00 +0100 Subject: [PATCH 4/4] Changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de149abf..9985497f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## 3.7.0 (Thursday, 22 July 2021) ### Added -- Allow installation of spatie/dto 3 [#282]((https://github.com/knuckleswtf/scribe/pull/282)) +- Allow installation of spatie/dto 3 [#285]((https://github.com/knuckleswtf/scribe/pull/285)) ## 3.6.3 (Tuesday, 20 July 2021) ### Fixed