From 0670c50cc35db234eea5f2c731c0f50c6bab0e2b Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Sun, 5 Oct 2025 09:01:27 +0200 Subject: [PATCH] Adding test and doc for getIntStrict() --- CHANGELOG.md | 3 +++ README.md | 32 +++++++++++++++++++++++++ tests/Unit/Traits/TypeableBlockTest.php | 15 ++++++++++++ 3 files changed, 50 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a06f8f0..14c94f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 8b35f13..1b1ad5f 100644 --- a/README.md +++ b/README.md @@ -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). @@ -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. diff --git a/tests/Unit/Traits/TypeableBlockTest.php b/tests/Unit/Traits/TypeableBlockTest.php index 97840f6..5ae77c7 100644 --- a/tests/Unit/Traits/TypeableBlockTest.php +++ b/tests/Unit/Traits/TypeableBlockTest.php @@ -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 {