Skip to content

Commit d83308f

Browse files
committed
fix tests and segmentation fault (infinite loop on collection map)
1 parent d686e25 commit d83308f

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

src/Mapper.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
use Illuminate\Http\Request;
88
use Illuminate\Pipeline\Pipeline;
99
use Illuminate\Support\Collection;
10+
use Illuminate\Support\Traits\Conditionable;
1011
use ReflectionClass;
1112
use ReflectionProperty;
1213

1314
final class Mapper
1415
{
16+
use Conditionable;
17+
1518
protected mixed $data;
1619

1720
protected ?string $dataClass = null;

src/Mappers/CollectionDataMapper.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
66
use Illuminate\Support\Collection;
77
use OpenSoutheners\LaravelDataMapper\MappingValue;
8-
use Symfony\Component\TypeInfo\Type;
98

109
use function OpenSoutheners\ExtendedPhp\Strings\is_json_structure;
1110
use function OpenSoutheners\LaravelDataMapper\map;
@@ -17,11 +16,11 @@ final class CollectionDataMapper extends DataMapper
1716
*/
1817
public function assert(MappingValue $mappingValue): bool
1918
{
20-
return ($mappingValue->collectClass === Collection::class && is_array($mappingValue->data))
19+
return $mappingValue->collectClass === 'array'
20+
|| ($mappingValue->collectClass === Collection::class && is_array($mappingValue->data))
2121
|| ($mappingValue->collectClass === Collection::class && is_string($mappingValue->data) && str_contains($mappingValue->data, ','))
22-
|| $mappingValue->preferredType instanceof Type\CollectionType
23-
|| $mappingValue->preferredTypeClass === Collection::class
24-
|| $mappingValue->preferredTypeClass === EloquentCollection::class;
22+
|| $mappingValue->objectClass === Collection::class
23+
|| $mappingValue->objectClass === EloquentCollection::class;
2524
}
2625

2726
/**
@@ -40,14 +39,18 @@ public function resolve(MappingValue $mappingValue): void
4039
is_string($mappingValue->data) => Collection::make(explode(',', $mappingValue->data)),
4140
default => Collection::make($mappingValue->data),
4241
};
43-
44-
if ($mappingValue->preferredTypeClass) {
45-
$collection = $collection->map(fn ($value) => map($value)->to($mappingValue->preferredTypeClass));
42+
43+
$collection = $collection->filter();
44+
45+
if ($mappingValue->collectClass === 'array') {
46+
$mappingValue->data = $collection->all();
47+
48+
return;
4649
}
4750

48-
// if ($mappingValue->preferredType->getBuiltinType() === Type::BUILTIN_TYPE_ARRAY) {
49-
// $collection = $collection->all();
50-
// }
51+
if ($mappingValue->objectClass && $mappingValue->objectClass !== Collection::class) {
52+
$collection = $collection->map(fn ($value) => map($value)->to($mappingValue->objectClass));
53+
}
5154

5255
$mappingValue->data = $collection;
5356
}

src/Mappers/ObjectDataMapper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Collection;
77
use Illuminate\Support\Str;
88
use OpenSoutheners\LaravelDataMapper\Attributes\NormaliseProperties;
9+
use OpenSoutheners\LaravelDataMapper\Mapper;
910
use OpenSoutheners\LaravelDataMapper\MappingValue;
1011
use OpenSoutheners\LaravelDataMapper\PropertyInfoExtractor;
1112
use ReflectionAttribute;
@@ -83,9 +84,11 @@ public function resolve(MappingValue $mappingValue): void
8384
}
8485

8586
if ($type instanceof Type\CollectionType) {
87+
$collectionValueType = $type->getCollectionValueType();
88+
8689
$data[$key] = map($value)
8790
->through((string) $unwrappedType)
88-
->to($type->getCollectionValueType());
91+
->to((string) $collectionValueType);
8992

9093
continue;
9194
}

tests/Unit/DataTransferObjectTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class DataTransferObjectTest extends TestCase
1616
{
17-
public function test_data_transfer_object_from_array()
17+
public function test_object_as_data_transfer_object_from_array()
1818
{
1919
$data = map([
2020
'title' => 'Hello world',

workbench/app/DataTransferObjects/CreatePostData.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CreatePostData implements RouteTransferableObject
2020
*/
2121
public function __construct(
2222
public string $title,
23-
public ?array $tags,
23+
public ?array $tags = null,
2424
public PostStatus $postStatus,
2525
public ?Post $post = null,
2626
public array|string|null $country = null,
@@ -33,7 +33,9 @@ public function __construct(
3333
public ?Collection $dates = null,
3434
$authorEmail = null
3535
) {
36-
$this->tags ??= ['generic', 'post'];
36+
if (count($this->tags) === 0) {
37+
$this->tags = ['generic', 'post'];
38+
}
3739

3840
$this->authorEmail = $authorEmail;
3941
}

0 commit comments

Comments
 (0)