Skip to content

Commit 6223ce4

Browse files
committed
Fine-tuning of default values for getBoolean()
1 parent b6f6672 commit 6223ce4

5 files changed

Lines changed: 56 additions & 34 deletions

File tree

README.md

Lines changed: 16 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 `getBoolean()` method
296+
297+
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).
298+
Parameters:
299+
300+
- `$path` (string): The path to the field (e.g., "0.author.id").
301+
- `$default` (null|bool): (Optional) The default value to return if the field doesn't exist. Defaults to null.
302+
- `$charNestedKey` (string): the character separator for nested field names. The default is ".".
303+
304+
Example usage:
305+
```php
306+
$data1->getBoolean("0.author.site_admin"); // Returns the field value as an boolean, for example true
307+
$data1->getBoolean("0.author.site_admin_notexists"); // Returns null because the field doesn't exists
308+
$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
309+
```
310+
295311

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

src/Traits/FormattableBlock.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,4 @@ public function getFormattedByte(
7070
return number_format($bytes / $terabyte, $precision) . ' TB';
7171
}
7272

73-
74-
75-
/**
76-
* Return a forced boolean value from the get() method
77-
* @param mixed $key the filed key , can be nested for example "commits.0.editable"
78-
* @param bool|null $defaultValue the default value returned if no value is found
79-
* @param non-empty-string $charNestedKey for nested field the . character is the default
80-
*/
81-
public function getBoolean(
82-
mixed $key,
83-
?bool $defaultValue = null,
84-
string $charNestedKey = ".",
85-
): bool {
86-
return (bool) $this->get($key, $defaultValue, $charNestedKey);
87-
}
8873
}

src/Traits/TypeableBlock.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,24 @@ public function getInt(int|string $key, ?int $defaultValue = null, string $charN
3636

3737
return $defaultValue;
3838
}
39+
40+
/**
41+
* Return a forced boolean value from the get() method
42+
* @param int|string $key the filed key , can be nested for example "commits.0.editable"
43+
* @param bool|null $defaultValue the default value returned if no value is found
44+
* @param non-empty-string $charNestedKey for nested field the . character is the default
45+
*/
46+
public function getBoolean(
47+
int|string $key,
48+
?bool $defaultValue = null,
49+
string $charNestedKey = ".",
50+
): ?bool {
51+
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
52+
53+
if (is_scalar($returnValue)) {
54+
return boolval($returnValue);
55+
}
56+
57+
return $defaultValue;
58+
}
3959
}

tests/Unit/Traits/FormattableBlokTest.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,3 @@ function (): void {
5757
expect($data1->getFormattedByte("assets.1.total_bytes", 0))->toBe("2 GB");
5858
},
5959
);
60-
61-
test(
62-
'Force field to boolean',
63-
function (): void {
64-
$data1 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p1.json");
65-
$data2 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p2.json");
66-
$data3 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p3.json");
67-
$data1->append($data2)->append($data3);
68-
expect($data1)->toHaveCount(30);
69-
expect($data2)->toHaveCount(10);
70-
expect($data1->getBoolean("0.author.site_admin"))->toBeBool();
71-
expect($data1->getBoolean("0.author.notexist"))->toBeBool();
72-
expect($data1->getBoolean("0.author.notexist"))->toBeBool()->toEqual(false);
73-
expect($data1->getBoolean("0.author.notexist", "true"))->toEqual(true);
74-
expect($data1->getBoolean("0.author.site_admin"))->toBeBool()->toEqual(false);
75-
expect($data1->getBoolean("0.author.site_admin", true))->toBeBool()->toEqual(false);
76-
expect($data1->getBoolean("0.commit.comment_countnotexists", true))->toBeBool()->toEqual(true);
77-
},
78-
);

tests/Unit/Traits/TypeableBlockTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,23 @@ function (): void {
3434

3535
},
3636
);
37+
38+
test(
39+
'Testing getBoolean()',
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+
$data1->append($data2)->append($data3);
45+
expect($data1)->toHaveCount(30);
46+
expect($data2)->toHaveCount(10);
47+
expect($data1->getBoolean("0.author.site_admin"))->toBeBool();
48+
expect($data1->getBoolean("0.author.notexist"))->toBeNull();
49+
expect($data1->getBoolean("0.author.notexist"))->toBeNull();
50+
expect($data1->getBoolean("0.author.notexist", true))->toBeBool()->toEqual(true);
51+
expect($data1->getBoolean("0.author.notexist", false))->toBeBool()->toEqual(false);
52+
expect($data1->getBoolean("0.author.site_admin"))->toBeBool()->toEqual(false);
53+
expect($data1->getBoolean("0.author.site_admin", true))->toBeBool()->toEqual(false);
54+
expect($data1->getBoolean("0.commit.comment_countnotexists", true))->toBeBool()->toEqual(true);
55+
},
56+
);

0 commit comments

Comments
 (0)