Skip to content

Commit 2f8d97c

Browse files
Merge pull request #44 from Hi-Folks/feat/getbooleanstrict
Adding `getBooleanStrict()`
2 parents 6223ce4 + c8f7bbd commit 2f8d97c

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Changelog
22

33
## 1.0.1 - WIP
4-
- Adding getInt() method for returing casted integer value
4+
- Adding getInt() method for returing casted integer value.
5+
- Adding getBooleanStrict() method for returning strict boolean.
56

67
## 1.0.0 - 2025-06-13
78
- Upgrade dev package PestPHP 3

src/Traits/TypeableBlock.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,24 @@ public function getBoolean(
5656

5757
return $defaultValue;
5858
}
59+
60+
/**
61+
* Return a forced boolean value from the get() method
62+
* @param int|string $key the filed key , can be nested for example "commits.0.editable"
63+
* @param bool $defaultValue the default value returned if no value is found
64+
* @param non-empty-string $charNestedKey for nested field the . character is the default
65+
*/
66+
public function getBooleanStrict(
67+
int|string $key,
68+
bool $defaultValue = false,
69+
string $charNestedKey = ".",
70+
): ?bool {
71+
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
72+
73+
if (is_scalar($returnValue)) {
74+
return boolval($returnValue);
75+
}
76+
77+
return $defaultValue;
78+
}
5979
}

tests/Unit/Traits/TypeableBlockTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,23 @@ function (): void {
5454
expect($data1->getBoolean("0.commit.comment_countnotexists", true))->toBeBool()->toEqual(true);
5555
},
5656
);
57+
58+
test(
59+
'Testing getBooleanStrict()',
60+
function (): void {
61+
$data1 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p1.json");
62+
$data2 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p2.json");
63+
$data3 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p3.json");
64+
$data1->append($data2)->append($data3);
65+
expect($data1)->toHaveCount(30);
66+
expect($data2)->toHaveCount(10);
67+
expect($data1->getBooleanStrict("0.author.site_admin"))->toBeBool();
68+
expect($data1->getBooleanStrict("0.author.notexist"))->toBeBool()->toBeFalse();
69+
expect($data1->getBooleanStrict("0.author.notexist"))->toBeBool();
70+
expect($data1->getBooleanStrict("0.author.notexist", true))->toBeBool()->toEqual(true);
71+
expect($data1->getBooleanStrict("0.author.notexist", false))->toBeBool()->toEqual(false);
72+
expect($data1->getBooleanStrict("0.author.site_admin"))->toBeBool()->toEqual(false);
73+
expect($data1->getBooleanStrict("0.author.site_admin", true))->toBeBool()->toEqual(false);
74+
expect($data1->getBooleanStrict("0.commit.comment_countnotexists", true))->toBeBool()->toEqual(true);
75+
},
76+
);

0 commit comments

Comments
 (0)