-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTypeableBlock.php
More file actions
39 lines (33 loc) · 1.28 KB
/
TypeableBlock.php
File metadata and controls
39 lines (33 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
declare(strict_types=1);
namespace HiFolks\DataType\Traits;
trait TypeableBlock
{
/**
* Return a forced string value from the get() method
* @param int|string $key the field key , can be nested for example "commits.0.name"
* @param string|null $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 getString(
int|string $key,
?string $defaultValue = null,
string $charNestedKey = ".",
): string {
return (string) $this->get($key, $defaultValue, $charNestedKey);
}
/**
* 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|null $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 getInt(int|string $key, ?int $defaultValue = null, string $charNestedKey = "."): ?int
{
$returnValue = $this->get($key, null, $charNestedKey);
if (is_scalar($returnValue)) {
return intval($returnValue);
}
return $defaultValue;
}
}