Skip to content

Commit

Permalink
Merge pull request #285 from knuckleswtf/collection
Browse files Browse the repository at this point in the history
Support for spatie/data-transfer-object 3
  • Loading branch information
shalvah authored Jul 22, 2021
2 parents b9cb188 + 87b8f23 commit 0538797
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 43 deletions.
2 changes: 0 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
23 changes: 22 additions & 1 deletion camel/BaseDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
35 changes: 18 additions & 17 deletions camel/BaseDTOCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
11 changes: 5 additions & 6 deletions camel/Extraction/ExtractedEndpointData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions camel/Extraction/ResponseCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion composer.dingo.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Tools/Globals.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 4 additions & 4 deletions tests/Strategies/Responses/ResponseCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/PostmanCollectionWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0538797

Please sign in to comment.