Skip to content

Commit 99c242d

Browse files
Merge pull request #54 from Hi-Folks/feat/get-float
Add getFloat and getFloatStrict
2 parents 212fafa + bfd03ac commit 99c242d

4 files changed

Lines changed: 280 additions & 101 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.1 - 2026-01-04
4+
- Adding `getFloat()` method for returing casted float value (nullable).
5+
- Adding `getFloatStrict()` method for returning strict float (not nullable).
6+
37
## 1.1.0 - 2026-01-02
48
- Add Symfony HttpClient support for loading JSON from URL. Thanks to @sonnymilton
59
- Update to PHP 8.5 support.

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,42 @@ $data1->getIntStrict("0.author.idx"); // Returns 0 because the field doesn't exi
326326
$data1->getIntStrict("0.author.idx", 44); // Returns 44 because the field doesn't exists, and you set a default, in this case 44
327327
```
328328

329+
### The `getFloat()` method
330+
331+
The `getFloat()` method retrieves the value of a specified field as a float from a data block. If the field does not exist or is null, it returns a default value, which can be customized (`null` by default).
332+
333+
**Parameters:**
334+
335+
- `$path` (string): The path to the field (e.g., `"0.author.score"`).
336+
- `$default` (null|float): (Optional) The default value to return if the field doesn't exist. Defaults to null.
337+
- `$charNestedKey` (string): The character separator for nested field names. The default is `"."`.
338+
339+
**Example usage:**
340+
341+
```php
342+
$data1->getFloat("0.author.score"); // Returns the field value as a float, for example 4.75
343+
$data1->getFloat("0.author.notexists"); // Returns null because the field doesn't exist
344+
$data1->getFloat("0.author.notexists", 1.5); // Returns 1.5 because the field doesn't exist, and you set a default
345+
```
346+
347+
### The `getFloatStrict()` method
348+
349+
The `getFloatStrict()` method retrieves the value of a specified field as a float from a data block. If the field does not exist or is null, it returns a default value, which can be customized (`0.0` by default).
350+
351+
**Parameters:**
352+
353+
- `$path` (string): The path to the field (e.g., `"0.author.score"`).
354+
- `$default` (float): (Optional) The default value to return if the field doesn't exist. Defaults to `0.0`.
355+
- `$charNestedKey` (string): The character separator for nested field names. The default is `"."`.
356+
357+
**Example usage:**
358+
359+
```php
360+
$data1->getFloatStrict("0.author.score"); // Returns the field value as a float, for example 4.75
361+
$data1->getFloatStrict("0.author.notexists"); // Returns 0.0 because the field doesn't exist, and the method is strict
362+
$data1->getFloatStrict("0.author.notexists", 1.5); // Returns 1.5 because the field doesn't exist, and you set a default
363+
```
364+
329365
### The `getBoolean()` method
330366

331367
The `getBoolean()` method retrieves the value of a specified field as a boolean from a data block. If the field does not exist or is null, it returns a default value, which can be customized (null by default).

src/Traits/TypeableBlock.php

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public function getString(
2727
}
2828

2929
return $defaultValue;
30-
3130
}
3231

3332
/**
@@ -51,7 +50,6 @@ public function getStringStrict(
5150
}
5251

5352
return $defaultValue;
54-
5553
}
5654

5755
/**
@@ -60,8 +58,11 @@ public function getStringStrict(
6058
* @param int|null $defaultValue the default integer value returned if no value is found
6159
* @param non-empty-string $charNestedKey for nested field the . character is the default
6260
*/
63-
public function getInt(int|string $key, ?int $defaultValue = null, string $charNestedKey = "."): ?int
64-
{
61+
public function getInt(
62+
int|string $key,
63+
?int $defaultValue = null,
64+
string $charNestedKey = ".",
65+
): ?int {
6566
$returnValue = $this->get($key, null, $charNestedKey);
6667

6768
if (is_scalar($returnValue)) {
@@ -77,8 +78,11 @@ public function getInt(int|string $key, ?int $defaultValue = null, string $charN
7778
* @param int $defaultValue the default integer value returned if no value is found
7879
* @param non-empty-string $charNestedKey for nested field the . character is the default
7980
*/
80-
public function getIntStrict(int|string $key, int $defaultValue = 0, string $charNestedKey = "."): int
81-
{
81+
public function getIntStrict(
82+
int|string $key,
83+
int $defaultValue = 0,
84+
string $charNestedKey = ".",
85+
): int {
8286
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
8387

8488
if ($returnValue === null) {
@@ -130,4 +134,48 @@ public function getBooleanStrict(
130134

131135
return $defaultValue;
132136
}
137+
138+
/**
139+
* Return a forced float value from the get() method
140+
* @param int|string $key the field key, can be nested for example "0.author.score"
141+
* @param float|null $defaultValue the default float value returned if no value is found
142+
* @param non-empty-string $charNestedKey for nested field the . character is the default
143+
*/
144+
public function getFloat(
145+
int|string $key,
146+
?float $defaultValue = null,
147+
string $charNestedKey = ".",
148+
): ?float {
149+
$returnValue = $this->get($key, null, $charNestedKey);
150+
151+
if (is_scalar($returnValue)) {
152+
return floatval($returnValue);
153+
}
154+
155+
return $defaultValue;
156+
}
157+
158+
/**
159+
* Return a forced float value from the get() method
160+
* @param int|string $key the field key, can be nested for example "0.author.score"
161+
* @param float $defaultValue the default float value returned if no value is found
162+
* @param non-empty-string $charNestedKey for nested field the . character is the default
163+
*/
164+
public function getFloatStrict(
165+
int|string $key,
166+
float $defaultValue = 0.0,
167+
string $charNestedKey = ".",
168+
): float {
169+
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
170+
171+
if ($returnValue === null) {
172+
return $defaultValue;
173+
}
174+
175+
if (is_scalar($returnValue)) {
176+
return floatval($returnValue);
177+
}
178+
179+
return $defaultValue;
180+
}
133181
}

0 commit comments

Comments
 (0)