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

Check if key/property exists in array/object #219

Merged
merged 2 commits into from
Sep 21, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/Observable.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Rx;

Expand Down Expand Up @@ -104,13 +104,13 @@ public function subscribe($onNextOrObserver = null, callable $onError = null, ca
$onNextOrObserver === null
? null
: function ($value) use ($onNextOrObserver, &$observer, &$disposable) {
try {
$onNextOrObserver($value);
} catch (\Throwable $throwable) {
$disposable->dispose();
$observer->onError($throwable);
}
},
try {
$onNextOrObserver($value);
} catch (\Throwable $throwable) {
$disposable->dispose();
$observer->onError($throwable);
}
},
$onError,
$onCompleted
);
Expand Down Expand Up @@ -1684,7 +1684,7 @@ public function bufferWithCount(int $count, int $skip = null): Observable
* @operator
* @reactivex catch
*/
public function catch (callable $selector): Observable
public function catch(callable $selector): Observable
{
return $this->lift(function () use ($selector) {
return new CatchErrorOperator(new ObservableFactoryWrapper($selector));
Expand Down Expand Up @@ -1831,7 +1831,7 @@ public function timestamp(SchedulerInterface $scheduler = null): Observable
* @operator
* @reactivex switch
*/
public function switch (): Observable
public function switch(): Observable
{
return $this->lift(function () {
return new SwitchLatestOperator();
Expand Down Expand Up @@ -1978,10 +1978,10 @@ public function pluck($property): Observable
}

return $this->map(function ($x) use ($property) {
if (is_array($x) && isset($x[$property])) {
if (is_array($x) && array_key_exists($property, $x)) {
return $x[$property];
}
if (is_object($x) && isset($x->$property)) {
if (is_object($x) && property_exists($x, $property)) {
return $x->$property;
}

Expand Down
62 changes: 62 additions & 0 deletions test/Rx/Functional/Operator/PluckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,66 @@ public function pluck_nested_numeric_key_with_object_properties()
subscribe(200, 400)
], $xs->getSubscriptions());
}

/**
* @test
*/
public function pluck_object_property_null()
{
$xs = $this->createHotObservable([
onNext(180, (object)['prop' => 1]),
onNext(210, (object)['prop' => 2]),
onNext(240, (object)['prop' => 3]),
onNext(290, (object)['prop' => null]),
onNext(350, (object)['prop' => 5]),
onError(400, new \Exception())
]);

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

$this->assertMessages([
onNext(210, 2),
onNext(240, 3),
onNext(290, null),
onNext(350, 5),
onError(400, new \Exception())
], $results->getMessages());

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

/**
* @test
*/
public function pluck_array_assoc_null()
{
$xs = $this->createHotObservable([
onNext(180, ['prop' => 1]),
onNext(210, ['prop' => 2]),
onNext(240, ['prop' => 3]),
onNext(290, ['prop' => null]),
onNext(350, ['prop' => 5]),
onError(400, new \Exception())
]);

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

$this->assertMessages([
onNext(210, 2),
onNext(240, 3),
onNext(290, null),
onNext(350, 5),
onError(400, new \Exception())
], $results->getMessages());

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