Skip to content

Commit

Permalink
Check if key/property exists in array/object (#219)
Browse files Browse the repository at this point in the history
* Check if key/property exists in array/object

* Add tests for plucking null values

---------

Co-authored-by: Matt Bonneau <[email protected]>
  • Loading branch information
mCassy and mbonneau authored Sep 21, 2024
1 parent eee8eb2 commit 02e0b2a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 12 deletions.
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());
}
}

0 comments on commit 02e0b2a

Please sign in to comment.