Skip to content

Commit

Permalink
Fix filtering of falsey values (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Aug 9, 2021
1 parent fd7c457 commit 7bb33ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PathQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function findList(Pathable $root): array
foreach ($this->segments() as $segment) {
$items = $items
->map(fn ($item) => $item->{$segment['attribute_name']} ?? null)
->filter()
->reject(fn ($item) => $item === null)
->flatten()
->when(
isset($segment['expression']),
Expand Down
25 changes: 25 additions & 0 deletions tests/PathQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use BigPictureMedical\OpenEhr\Rm\DataStructures\ItemStructure\ItemTree;
use BigPictureMedical\OpenEhr\Rm\DataStructures\Representation\Cluster;
use BigPictureMedical\OpenEhr\Rm\DataStructures\Representation\Element;
use BigPictureMedical\OpenEhr\Rm\DataTypes\Basic\DvBoolean;
use BigPictureMedical\OpenEhr\Rm\DataTypes\Quantity\DvCount;
use BigPictureMedical\OpenEhr\Rm\DataTypes\Text\CodePhrase;
use BigPictureMedical\OpenEhr\Rm\DataTypes\Text\DvCodedText;
use BigPictureMedical\OpenEhr\Rm\DataTypes\Text\DvText;
Expand Down Expand Up @@ -92,6 +94,19 @@ public function test_it_handles_missing_paths_when_finding_a_list()
$this->assertSame([], $result);
}

public function test_it_doesnt_filter_zero_and_false()
{
$composition = $this->makeComposition();

$result = (new PathQuery('content[test-EVALUATION.test.v0]/data/items[at0002]/items[at0005]/value/value'))
->find($composition);
$this->assertSame(false, $result);

$result = (new PathQuery('content[test-EVALUATION.test.v0]/data/items[at0002]/items[at0006]/value/magnitude'))
->find($composition);
$this->assertSame(0, $result);
}

public function test_it_finds_whether_a_path_exists()
{
$composition = $this->makeComposition();
Expand Down Expand Up @@ -158,6 +173,16 @@ private function makeComposition(): Composition
name: new DvText(value: 'Test element'),
value: new DvText(value: 'Test unique value')
),
new Element(
archetype_node_id: 'at0005',
name: new DvText(value: 'False'),
value: new DvBoolean(value: false),
),
new Element(
archetype_node_id: 'at0006',
name: new DvText(value: 'Falsey'),
value: new DvCount(magnitude: 0),
),
]
),
new Cluster(
Expand Down

0 comments on commit 7bb33ba

Please sign in to comment.