Skip to content
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog

## 1.0.1 - WIP
- Adding getInt() method for returing casted integer value
- Adding getInt() method for returing casted integer value.
- Adding getBooleanStrict() method for returning strict boolean.

## 1.0.0 - 2025-06-13
- Upgrade dev package PestPHP 3
Expand Down
20 changes: 20 additions & 0 deletions src/Traits/TypeableBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,24 @@ public function getBoolean(

return $defaultValue;
}

/**
* Return a forced boolean value from the get() method
* @param int|string $key the filed key , can be nested for example "commits.0.editable"
* @param bool $defaultValue the default value returned if no value is found
* @param non-empty-string $charNestedKey for nested field the . character is the default
*/
public function getBooleanStrict(
int|string $key,
bool $defaultValue = false,
string $charNestedKey = ".",
): ?bool {
$returnValue = $this->get($key, $defaultValue, $charNestedKey);

if (is_scalar($returnValue)) {
return boolval($returnValue);
}

return $defaultValue;
}
}
20 changes: 20 additions & 0 deletions tests/Unit/Traits/TypeableBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,23 @@ function (): void {
expect($data1->getBoolean("0.commit.comment_countnotexists", true))->toBeBool()->toEqual(true);
},
);

test(
'Testing getBooleanStrict()',
function (): void {
$data1 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p1.json");
$data2 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p2.json");
$data3 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p3.json");
$data1->append($data2)->append($data3);
expect($data1)->toHaveCount(30);
expect($data2)->toHaveCount(10);
expect($data1->getBooleanStrict("0.author.site_admin"))->toBeBool();
expect($data1->getBooleanStrict("0.author.notexist"))->toBeBool()->toBeFalse();
expect($data1->getBooleanStrict("0.author.notexist"))->toBeBool();
expect($data1->getBooleanStrict("0.author.notexist", true))->toBeBool()->toEqual(true);
expect($data1->getBooleanStrict("0.author.notexist", false))->toBeBool()->toEqual(false);
expect($data1->getBooleanStrict("0.author.site_admin"))->toBeBool()->toEqual(false);
expect($data1->getBooleanStrict("0.author.site_admin", true))->toBeBool()->toEqual(false);
expect($data1->getBooleanStrict("0.commit.comment_countnotexists", true))->toBeBool()->toEqual(true);
},
);