Skip to content

Commit

Permalink
Support spatie/dto 3
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Jul 22, 2021
1 parent b9cb188 commit c3a3100
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 39 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 [#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))
Expand Down
3 changes: 2 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 Down
34 changes: 17 additions & 17 deletions camel/BaseDTOCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
7 changes: 5 additions & 2 deletions camel/Extraction/ExtractedEndpointData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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 c3a3100

Please sign in to comment.