Skip to content

Commit 0670c50

Browse files
committed
Adding test and doc for getIntStrict()
1 parent 7a95a88 commit 0670c50

3 files changed

Lines changed: 50 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.2 - WIP
4+
- - Adding `getIntStrict()` method for returning strict int.
5+
36
## 1.0.1 - 2025-10-04
47
- Adding `getInt()` method for returing casted integer value.
58
- Adding `getBooleanStrict()` method for returning strict boolean.

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,22 @@ $data1->getInt("0.author.idx"); // Returns null because the field doesn't exists
292292
$data1->getInt("0.author.idx", 44); // Returns 44 because the field doesn't exists, and you set a default, in this case 44
293293
```
294294

295+
### The `getIntStrict()` method
296+
297+
The `getIntStrict()` 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 (0 by default).
298+
Parameters:
299+
300+
- `$path` (string): The path to the field (e.g., "0.author.id").
301+
- `$default` (int): (Optional) The default value to return if the field doesn't exist. Defaults to 0.
302+
- `$charNestedKey` (string): the character separator for nested field names. The default is ".".
303+
304+
Example usage:
305+
```php
306+
$data1->getIntStrict("0.author.id"); // Returns the field value as an integer, for example 678434
307+
$data1->getIntStrict("0.author.idx"); // Returns 0 because the field doesn't exists, and the method is strict
308+
$data1->getIntStrict("0.author.idx", 44); // Returns 44 because the field doesn't exists, and you set a default, in this case 44
309+
```
310+
295311
### The `getBoolean()` method
296312

297313
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).
@@ -308,6 +324,22 @@ $data1->getBoolean("0.author.site_admin_notexists"); // Returns null because the
308324
$data1->getBoolean("0.author.site_admin_notexists", true); // Returns true because the field doesn't exists, and you set a default, in this case true
309325
```
310326

327+
### The `getBooleanStrict()` method
328+
329+
The `getBooleanStrict()` 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 strict boolean default value, which can be customized (false by default).
330+
Parameters:
331+
332+
- `$path` (string): The path to the field (e.g., "0.author.id").
333+
- `$default` (bool): (Optional) The default value to return if the field doesn't exist. Defaults to false.
334+
- `$charNestedKey` (string): the character separator for nested field names. The default is ".".
335+
336+
Example usage:
337+
```php
338+
$data1->getBooleanStrict("0.author.site_admin"); // Returns the field value as an boolean, for example true
339+
$data1->getBooleanStrict("0.author.site_admin_notexists"); // Returns false because the field doesn't exists
340+
$data1->getBooleanStrict("0.author.site_admin_notexists", true); // Returns true because the field doesn't exists, and you set a default, in this case true
341+
```
342+
311343

312344
### The `getBlock()` method
313345
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.

tests/Unit/Traits/TypeableBlockTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ function (): void {
3535
},
3636
);
3737

38+
test(
39+
'Testing getIntStrict()',
40+
function (): void {
41+
$data1 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p1.json");
42+
// $data2 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p2.json");
43+
// $data3 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p3.json");
44+
45+
expect($data1->getIntStrict("0.author.id"))->toBeInt()->toBe(678434);
46+
expect($data1->getIntStrict("0.author.idx"))->toBeInt()->toBe(0);
47+
expect($data1->getIntStrict("0.author.idx", 44))->toBeInt()->toBe(44);
48+
expect($data1->getIntStrict("0.commit.author.date"))->toBeInt()->toBe(2024);
49+
50+
},
51+
);
52+
3853
test(
3954
'Testing getBoolean()',
4055
function (): void {

0 commit comments

Comments
 (0)