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

Allow plucking of multiple items at once #147

Merged
merged 1 commit into from Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/Rx/Observable.php
Expand Up @@ -1818,6 +1818,11 @@ public function average()
*/
public function pluck($property)
{
$args = func_get_args();
if (count($args) > 1) {
return call_user_func_array([$this->pluck(array_shift($args)), 'pluck'], $args);
}

return $this->map(function ($x) use ($property) {
if (is_array($x) && isset($x[$property])) {
return $x[$property];
Expand Down
62 changes: 62 additions & 0 deletions test/Rx/Functional/Operator/PluckTest.php
Expand Up @@ -190,4 +190,66 @@ public function pluck_array_numeric_index()
subscribe(200, 400)
], $xs->getSubscriptions());
}

/**
* @test
*/
public function pluck_nested()
{
$xs = $this->createHotObservable([
onNext(180, [-1,-1,-1,-1]),
onNext(210, [[1],[2],[3]]),
onNext(240, [[4],[5],[6]]),
onNext(290, [[7],[8],[9]]),
onNext(350, [[10],[11],[12]]),
onCompleted(400)
]);

$results = $this->scheduler->startWithCreate(function () use ($xs) {
return $xs->pluck(1, 0);
});

$this->assertMessages([
onNext(210, 2),
onNext(240, 5),
onNext(290, 8),
onNext(350, 11),
onCompleted(400)
], $results->getMessages());

$this->assertSubscriptions([
subscribe(200, 400)
], $xs->getSubscriptions());
}

/**
* @test
*/
public function pluck_nested_numeric_key_with_object_properties()
{
$xs = $this->createHotObservable([
onNext(180, [-1,-1,-1,-1]),
onNext(210, [[1],(object)['prop' => 2],[3]]),
onNext(240, [[4],(object)['prop' => 5],[6]]),
onNext(290, [[7],(object)['prop' => 8],[9]]),
onNext(350, [[10],(object)['prop' => 11],[12]]),
onCompleted(400)
]);

$results = $this->scheduler->startWithCreate(function () use ($xs) {
return $xs->pluck(1, 'prop');
});

$this->assertMessages([
onNext(210, 2),
onNext(240, 5),
onNext(290, 8),
onNext(350, 11),
onCompleted(400)
], $results->getMessages());

$this->assertSubscriptions([
subscribe(200, 400)
], $xs->getSubscriptions());
}
}