Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.0.2 - WIP
- - Adding `getIntStrict()` method for returning strict int.

## 1.0.1 - 2025-10-04
- Adding `getInt()` method for returing casted integer value.
- Adding `getBooleanStrict()` method for returning strict boolean.
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,22 @@ $data1->getInt("0.author.idx"); // Returns null because the field doesn't exists
$data1->getInt("0.author.idx", 44); // Returns 44 because the field doesn't exists, and you set a default, in this case 44
```

### The `getIntStrict()` method

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).
Parameters:

- `$path` (string): The path to the field (e.g., "0.author.id").
- `$default` (int): (Optional) The default value to return if the field doesn't exist. Defaults to 0.
- `$charNestedKey` (string): the character separator for nested field names. The default is ".".

Example usage:
```php
$data1->getIntStrict("0.author.id"); // Returns the field value as an integer, for example 678434
$data1->getIntStrict("0.author.idx"); // Returns 0 because the field doesn't exists, and the method is strict
$data1->getIntStrict("0.author.idx", 44); // Returns 44 because the field doesn't exists, and you set a default, in this case 44
```

### The `getBoolean()` method

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).
Expand All @@ -308,6 +324,22 @@ $data1->getBoolean("0.author.site_admin_notexists"); // Returns null because the
$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
```

### The `getBooleanStrict()` method

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).
Parameters:

- `$path` (string): The path to the field (e.g., "0.author.id").
- `$default` (bool): (Optional) The default value to return if the field doesn't exist. Defaults to false.
- `$charNestedKey` (string): the character separator for nested field names. The default is ".".

Example usage:
```php
$data1->getBooleanStrict("0.author.site_admin"); // Returns the field value as an boolean, for example true
$data1->getBooleanStrict("0.author.site_admin_notexists"); // Returns false because the field doesn't exists
$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
```


### The `getBlock()` method
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.
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Traits/TypeableBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ function (): void {
},
);

test(
'Testing getIntStrict()',
function (): void {
$data1 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p1.json");
// $data2 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p2.json");
// $data3 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p3.json");

expect($data1->getIntStrict("0.author.id"))->toBeInt()->toBe(678434);
expect($data1->getIntStrict("0.author.idx"))->toBeInt()->toBe(0);
expect($data1->getIntStrict("0.author.idx", 44))->toBeInt()->toBe(44);
expect($data1->getIntStrict("0.commit.author.date"))->toBeInt()->toBe(2024);

},
);

test(
'Testing getBoolean()',
function (): void {
Expand Down