Skip to content

Commit 9078c25

Browse files
committed
Add getIntStrict method to TypeableBlock trait
Introduces getIntStrict to enforce integer return values from the get() method, handling nested keys and default values. This improves type safety when retrieving integer fields.
1 parent 8d08986 commit 9078c25

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)