forked from Hi-Folks/data-block
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeableBlock.php
More file actions
99 lines (84 loc) · 3.36 KB
/
TypeableBlock.php
File metadata and controls
99 lines (84 loc) · 3.36 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?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;
}
/**
* 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"
* @param bool|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 getBoolean(
int|string $key,
?bool $defaultValue = null,
string $charNestedKey = ".",
): ?bool {
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
if (is_scalar($returnValue)) {
return boolval($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"
* @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;
}
}