Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new map method for query builder #513

Merged
merged 32 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7715a0b
Applying AddVoidReturnTypeWhereNoReturnRector
srtfisher Mar 6, 2024
ced985c
Applying RemoveUselessReturnTagRector and RenameForeachValueVariableT…
srtfisher Mar 6, 2024
6936541
Applying RemoveUnusedForeachKeyRector and RemoveDuplicatedArrayKeyRector
srtfisher Mar 6, 2024
57e30c3
Applying more
srtfisher Mar 6, 2024
a2f5d5e
Applying rules:
srtfisher Mar 6, 2024
f9bc048
Merginging in 1.x
srtfisher Mar 6, 2024
5b21294
Applying
srtfisher Mar 6, 2024
646eed3
Applying RemoveNonExistingVarAnnotationRector
srtfisher Mar 6, 2024
b2bf6d1
Disabling Squiz.Commenting.VariableComment.MissingVar phpcs rule
srtfisher Mar 6, 2024
f38c5a1
Enabling a few more
srtfisher Mar 6, 2024
8599001
Working through adding type coverage
srtfisher Mar 6, 2024
15c0484
Merging in 1.x
srtfisher Mar 6, 2024
e533388
Bumping level to 5
srtfisher Mar 6, 2024
6a148af
Resetting to level 2
srtfisher Mar 6, 2024
2a80b2e
Applying a promoted property before applying the php 8.1 rules
srtfisher Mar 6, 2024
85ba887
Applying php 8.1 rules
srtfisher Mar 6, 2024
5179f5f
Bumping to level 5
srtfisher Mar 6, 2024
00df201
Increasing to 7
srtfisher Mar 6, 2024
a5e8215
Fixing shorthand function calls
srtfisher Mar 6, 2024
803e5eb
Locking into 7
srtfisher Mar 6, 2024
7ab9ce3
Adding rector to CI
srtfisher Mar 6, 2024
949dc56
Provide an upgrade warning message about PHPUnit 10
srtfisher Mar 8, 2024
8cbd50f
Breaking out changelog note
srtfisher Mar 8, 2024
5e1875d
Change to diff
srtfisher Mar 8, 2024
235e4d0
Include anchor in message
srtfisher Mar 8, 2024
014239f
CHANGELOG order
srtfisher Mar 8, 2024
b9e8812
Merge branch 'feature/phpunit-10-upgrade-warning' into feature/rector…
srtfisher Mar 8, 2024
89d7a0b
Pushing up latest
srtfisher Mar 8, 2024
04828c0
Add more file casing
srtfisher Mar 8, 2024
dc8bc9f
Fix the syntax of some of the promoted properties
srtfisher Mar 8, 2024
2050416
Adding new map() method for query builder
srtfisher Mar 8, 2024
e112427
Merge branch '1.x' into feature/model-map
srtfisher Mar 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/mantle/database/query/class-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,27 @@ public function each_by_id( callable $callback, int $count = 100, string $attrib
}, $attribute );
}

/**
* Map the query results to a new collection.
*
* @template TMapValue
*
* @param callable(TModel): TMapValue $callback Callback to run on each chunk.
* @param int $count Number of items to chunk by.
* @return Collection<int, TMapValue>
*/
public function map( callable $callback, int $count = 100 ) {
$results = new Collection();

$this->chunk( $count, function ( Collection $items ) use ( $callback, $results ): bool {
$results->push( ...$items->map( $callback ) );

return true;
} );

return $results;
}

/**
* Magic method to proxy to the appropriate query method.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/Database/Query/PostQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,21 @@ public function test_each_by_id() {
$this->assertNull( get_post( collect( $post_ids )->last() ) );
}

public function test_map() {
$post_ids = static::factory()->post->create_many( 100 );

$ids = Testable_Post::map( function ( Testable_Post $post ) {
return [ 'id' => $post->id() ];
} );

$this->assertEquals(
collect( $post_ids )->map( function ( $id ) {
return [ 'id' => $id ];
} )->sort()->values(),
$ids->sort()->values()
);
}

public function test_where_raw() {
$post_id = static::get_random_post_id();

Expand Down