diff --git a/src/Traits/TypeableBlock.php b/src/Traits/TypeableBlock.php index 4f8ca6e..a77d84f 100644 --- a/src/Traits/TypeableBlock.php +++ b/src/Traits/TypeableBlock.php @@ -37,6 +37,26 @@ public function getInt(int|string $key, ?int $defaultValue = null, string $charN return $defaultValue; } + /** + * Return a forced integer value from the get() method + * @param int|string $key the field key, can be nested for example "0.author.id" + * @param int $defaultValue the default integer value returned if no value is found + * @param non-empty-string $charNestedKey for nested field the . character is the default + */ + public function getIntStrict(int|string $key, int $defaultValue = 0, string $charNestedKey = "."): int + { + $returnValue = $this->get($key, $defaultValue, $charNestedKey); + + if ($returnValue === null) { + return $defaultValue; + } + + if (is_scalar($returnValue)) { + return intval($returnValue); + } + + 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"