Skip to content

Commit 0116c65

Browse files
Merge pull request #42 from Hi-Folks/feat/typeable
Adding getInt() method
2 parents 97ebf71 + 2a0b194 commit 0116c65

5 files changed

Lines changed: 62 additions & 0 deletions

File tree

CHANGELOG.md

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

3+
## 1.0.1 - WIP
4+
- Adding getInt() method for returing casted integer value
5+
36
## 1.0.0 - 2025-06-13
47
- Upgrade dev package PestPHP 3
58
- Upgrade dev package PHPstan 2

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,23 @@ $data1->getString("0.commit.author.notexists", "AA"); // Returns "AA"
276276
$data1->getString("0.commit.comment_count"); // Returns "0" as a string even if the field value is an integer
277277
```
278278

279+
### The `getInt()` method
280+
281+
The `getInt()` method retrieves the value of a specified field as a integer 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).
282+
Parameters:
283+
284+
- `$path` (string): The path to the field (e.g., "0.author.id").
285+
- `$default` (null|int): (Optional) The default value to return if the field doesn't exist. Defaults to null.
286+
- `$charNestedKey` (string): the character separator for nested field names. The default is ".".
287+
288+
Example usage:
289+
```php
290+
$data1->getInt("0.author.id"); // Returns the field value as an integer, for example 678434
291+
$data1->getInt("0.author.idx"); // Returns null because the field doesn't exists
292+
$data1->getInt("0.author.idx", 44); // Returns 44 because the field doesn't exists, and you set a default, in this case 44
293+
```
294+
295+
279296
### The `getBlock()` method
280297
If you need to manage a complex array (nested array) or an array obtained from a complex JSON structure, you can access a portion of the array and obtain the `Block` object via the `getBlock()` method.
281298

src/Block.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use HiFolks\DataType\Traits\FormattableBlock;
1313
use HiFolks\DataType\Traits\IteratableBlock;
1414
use HiFolks\DataType\Traits\LoadableBlock;
15+
use HiFolks\DataType\Traits\TypeableBlock;
1516
use HiFolks\DataType\Traits\ValidableBlock;
1617
use Iterator;
1718

@@ -31,6 +32,7 @@ final class Block implements Iterator, ArrayAccess, Countable
3132
use IteratableBlock;
3233
use ValidableBlock;
3334
use FormattableBlock;
35+
use TypeableBlock;
3436

3537

3638
/** @var array<int|string, mixed> */

src/Traits/TypeableBlock.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HiFolks\DataType\Traits;
6+
7+
trait TypeableBlock
8+
{
9+
/**
10+
*
11+
* @param non-empty-string $charNestedKey
12+
*/
13+
public function getInt(int|string $key, ?int $defaultValue = null, string $charNestedKey = "."): ?int
14+
{
15+
$returnValue = $this->get($key, null, $charNestedKey);
16+
17+
if (is_scalar($returnValue)) {
18+
return intval($returnValue);
19+
}
20+
21+
return $defaultValue;
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use HiFolks\DataType\Block;
4+
5+
test(
6+
'Testing getInt()',
7+
function (): void {
8+
$data1 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p1.json");
9+
// $data2 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p2.json");
10+
// $data3 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p3.json");
11+
12+
expect($data1->getInt("0.author.id"))->toBeInt()->toBe(678434);
13+
expect($data1->getInt("0.author.idx"))->toBeNull();
14+
expect($data1->getInt("0.author.idx", 44))->toBeInt()->toBe(44);
15+
16+
},
17+
);

0 commit comments

Comments
 (0)