Skip to content

Commit 2e9d3c1

Browse files
committed
feat: add map_arrays_through config to default array inputs to collections
1 parent 0e58c2a commit 2e9d3c1

4 files changed

Lines changed: 38 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- **Mapper registry**: custom mappers are now registered on the `MapperRegistry` container singleton with an explicit priority (`register(MyMapper::class, priority: 100)`), and higher-priority mappers win over built-ins for the same input
1313
- `MappingResolved` event dispatched on every mapping resolution, carrying the winning mapper class and the mapping context — replaces the previous debug logging
1414
- Mapping context now carries the target property, a dot-notation `path` (e.g. `tags.2`) for nested mappings, and the set of provided input keys
15+
- `map_arrays_through` config option: set it to `Collection::class` to get collections instead of plain arrays when mapping array input without an explicit `->through()`; inline `->through()` always takes precedence
1516

1617
### Changed
1718

config/data-mapper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
*/
1010
'normalise_properties' => true,
1111

12+
/**
13+
* Default wrapping for array inputs when no ->through() is given.
14+
*
15+
* 'array' returns plain PHP arrays, \Illuminate\Support\Collection::class
16+
* returns collections. Overridable per-mapping with ->through().
17+
*/
18+
'map_arrays_through' => 'array',
19+
1220
/**
1321
* Types generator config, only used as defaults when no options
1422
* are passed to the command.

src/Mapper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ public function to(?string $output = null)
105105
$output ??= $this->dataClass;
106106

107107
if (! $this->throughClass && (is_array($this->data) || $this->data instanceof Collection)) {
108-
$this->throughClass = is_array($this->data) ? 'array' : Collection::class;
108+
$this->throughClass = is_array($this->data)
109+
? (app('config')->get('data-mapper.map_arrays_through') ?? 'array')
110+
: Collection::class;
109111
}
110112

111113
$mappingValue = new MappingValue(

tests/MapperTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,32 @@ public function test_map_array_of_numeric_ids_to_model_results_in_array_of_model
5252
$this->assertEquals($users->last()->email, $result[1]->email);
5353
}
5454

55+
public function test_map_array_configured_through_collection_results_in_collection_of_model_instances()
56+
{
57+
$this->app['config']->set('data-mapper.map_arrays_through', Collection::class);
58+
59+
$users = UserFactory::new()->count(2)->create();
60+
61+
$result = map([1, 2])->to(User::class);
62+
63+
$this->assertTrue(get_class($result) === Collection::class);
64+
$this->assertEquals($users->first()->email, $result->first()->email);
65+
$this->assertEquals($users->last()->email, $result->last()->email);
66+
}
67+
68+
public function test_map_array_through_array_overrides_configured_collection_default()
69+
{
70+
$this->app['config']->set('data-mapper.map_arrays_through', Collection::class);
71+
72+
$users = UserFactory::new()->count(2)->create();
73+
74+
$result = map([1, 2])->through('array')->to(User::class);
75+
76+
$this->assertIsArray($result);
77+
$this->assertEquals($users->first()->email, $result[0]->email);
78+
$this->assertEquals($users->last()->email, $result[1]->email);
79+
}
80+
5581
public function test_map_multiple_numeric_ids_to_model_through_base_collection_results_in_base_collection_of_model_instances()
5682
{
5783
$users = UserFactory::new()->count(2)->create();

0 commit comments

Comments
 (0)