Skip to content

Commit

Permalink
Add new map method for query builder (#513)
Browse files Browse the repository at this point in the history
* Applying AddVoidReturnTypeWhereNoReturnRector

* Applying RemoveUselessReturnTagRector and RenameForeachValueVariableToMatchExprVariableRector rules

* Applying RemoveUnusedForeachKeyRector and RemoveDuplicatedArrayKeyRector

* Applying more
RecastingRemovalRector
RemoveAndTrueRector
SimplifyMirrorAssignRector

* Applying rules:
RemoveDeadContinueRector
RemoveUnusedNonEmptyArrayBeforeForeachRector
RemoveNullPropertyInitializationRector
RemoveUselessReturnExprInConstructRector
RemoveTypedPropertyDeadInstanceOfRector

* Applying

* Applying RemoveNonExistingVarAnnotationRector
RemoveUselessVarTagRector
RemovePhpVersionIdCheckRector
RemoveAlwaysTrueIfConditionRector
RemoveUnusedPrivateClassConstantRector
RemoveUnusedPrivatePropertyRector
RemoveDuplicatedCaseInSwitchRector
RemoveDeadInstanceOfRector
RemoveDeadTryCatchRector

* Disabling Squiz.Commenting.VariableComment.MissingVar phpcs rule

* Enabling a few more

* Working through adding type coverage

* Bumping level to 5

* Resetting to level 2

* Applying a promoted property before applying the php 8.1 rules

* Applying php 8.1 rules

* Bumping to level 5

* Increasing to 7

* Fixing shorthand function calls

* Locking into 7

* Adding rector to CI

* Provide an upgrade warning message about PHPUnit 10

* Breaking out changelog note

* Change to diff

* Include anchor in message

* CHANGELOG order

* Pushing up latest

* Add more file casing

* Fix the syntax of some of the promoted properties

* Adding new map() method for query builder
  • Loading branch information
srtfisher committed Mar 11, 2024
1 parent a4b087d commit 4215d00
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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

0 comments on commit 4215d00

Please sign in to comment.