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..9985497f 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 [#285]((https://github.com/knuckleswtf/scribe/pull/285)) + ## 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..1551756a 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 @@ -20,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 23b29277..046f7316 100644 --- a/camel/BaseDTOCollection.php +++ b/camel/BaseDTOCollection.php @@ -3,49 +3,50 @@ 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); } + return $this; } - /** - * @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..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 @@ -185,13 +182,15 @@ 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'); + + 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..e14056dc 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,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/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;