Skip to content

Commit

Permalink
fix filtering reset keys to numeric when string needed
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Jun 12, 2024
1 parent ef0047a commit 19c5ebd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/PropertiesMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ protected function mapIntoCollection(array $propertyTypes, string $propertyKey,
$preferredCollectionTypeClass = $preferredCollectionType ? $preferredCollectionType->getClassName() : null;

$collection = $collection->map(fn ($value) => is_string($value) ? trim($value) : $value)
->filter(fn($item) => !blank($item))
->values();
->filter(fn($item) => !blank($item));

if ($preferredCollectionType && $preferredCollectionType->getBuiltinType() === Type::BUILTIN_TYPE_OBJECT) {
if (is_subclass_of($preferredCollectionTypeClass, Model::class)) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/DataTransferObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Collection;
use Mockery;
use PHPUnit\Framework\TestCase;
use Workbench\App\DataTransferObjects\CreateComment;
use Workbench\App\DataTransferObjects\CreateManyPostData;
use Workbench\App\DataTransferObjects\CreatePostData;
use Workbench\App\Enums\PostStatus;
Expand Down Expand Up @@ -270,4 +271,18 @@ public function testDataTransferObjectSentIntoAnotherAsCollectedWillBeMappedFrom
$this->assertInstanceOf(Collection::class, $data->posts->last()->dates);
$this->assertInstanceOf(Carbon::class, $data->posts->last()->dates->first());
}

public function testDataTransferObjectRetainKeysFromNestedObjectsOrArrays()
{
$data = CreateComment::fromArray([
'content' => 'hello world',
'tags' => [
'hello' => 'world',
'foo' => 'bar'
]
]);

$this->assertArrayHasKey('hello', $data->tags);
$this->assertArrayHasKey('foo', $data->tags);
}
}
16 changes: 16 additions & 0 deletions workbench/app/DataTransferObjects/CreateComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Workbench\App\DataTransferObjects;

use Illuminate\Support\Collection;
use OpenSoutheners\LaravelDto\DataTransferObject;

class CreateComment extends DataTransferObject
{
public function __construct(
public string $content,
public array $tags
) {
//
}
}

0 comments on commit 19c5ebd

Please sign in to comment.