Skip to content

Commit 7a95a88

Browse files
Merge pull request #46 from ksanjeev284/main
Add getIntStrict method to TypeableBlock trait
2 parents 8d08986 + 9078c25 commit 7a95a88

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/Traits/TypeableBlock.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ public function getInt(int|string $key, ?int $defaultValue = null, string $charN
3737
return $defaultValue;
3838
}
3939

40+
/**
41+
* Return a forced integer value from the get() method
42+
* @param int|string $key the field key, can be nested for example "0.author.id"
43+
* @param int $defaultValue the default integer value returned if no value is found
44+
* @param non-empty-string $charNestedKey for nested field the . character is the default
45+
*/
46+
public function getIntStrict(int|string $key, int $defaultValue = 0, string $charNestedKey = "."): int
47+
{
48+
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
49+
50+
if ($returnValue === null) {
51+
return $defaultValue;
52+
}
53+
54+
if (is_scalar($returnValue)) {
55+
return intval($returnValue);
56+
}
57+
58+
return $defaultValue;
59+
}
4060
/**
4161
* Return a forced boolean value from the get() method
4262
* @param int|string $key the filed key , can be nested for example "commits.0.editable"

0 commit comments

Comments
 (0)