From 1630e3f5650f0697a2d6a2ba97da346ac405b398 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Sat, 4 Oct 2025 22:44:00 +0200 Subject: [PATCH] getString() moved from Formattable traits to TYpeable, and finetuning params --- src/Block.php | 2 +- src/Traits/FormattableBlock.php | 14 +------------- src/Traits/TypeableBlock.php | 20 ++++++++++++++++++-- tests/Unit/Traits/FormattableBlokTest.php | 19 ------------------- tests/Unit/Traits/TypeableBlockTest.php | 19 +++++++++++++++++++ 5 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/Block.php b/src/Block.php index d276a7b..11e1219 100644 --- a/src/Block.php +++ b/src/Block.php @@ -77,7 +77,7 @@ public static function fromEncodedJson(mixed $encodedJson): self * * @param non-empty-string $charNestedKey */ - public function get(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): mixed + public function get(int|string $key, mixed $defaultValue = null, string $charNestedKey = "."): mixed { if (is_string($key)) { $keyString = strval($key); diff --git a/src/Traits/FormattableBlock.php b/src/Traits/FormattableBlock.php index fad15dc..ec4305e 100644 --- a/src/Traits/FormattableBlock.php +++ b/src/Traits/FormattableBlock.php @@ -70,19 +70,7 @@ public function getFormattedByte( return number_format($bytes / $terabyte, $precision) . ' TB'; } - /** - * Return a forced string value from the get() method - * @param mixed $key the filed key , can be nested for example "commits.0.name" - * @param string|null $defaultValue the default value returned if no value is found - * @param non-empty-string $charNestedKey for nested field the . character is the default - */ - public function getString( - mixed $key, - ?string $defaultValue = null, - string $charNestedKey = ".", - ): string { - return (string) $this->get($key, $defaultValue, $charNestedKey); - } + /** * Return a forced boolean value from the get() method diff --git a/src/Traits/TypeableBlock.php b/src/Traits/TypeableBlock.php index 593c9bd..41558e4 100644 --- a/src/Traits/TypeableBlock.php +++ b/src/Traits/TypeableBlock.php @@ -7,8 +7,24 @@ trait TypeableBlock { /** - * - * @param non-empty-string $charNestedKey + * Return a forced string value from the get() method + * @param int|string $key the field key , can be nested for example "commits.0.name" + * @param string|null $defaultValue the default value returned if no value is found + * @param non-empty-string $charNestedKey for nested field the . character is the default + */ + public function getString( + int|string $key, + ?string $defaultValue = null, + string $charNestedKey = ".", + ): string { + return (string) $this->get($key, $defaultValue, $charNestedKey); + } + + /** + * Return a forced integer value from the get() method + * @param int|string $key the field key, can be nested for example "0.author.id" + * @param int|null $defaultValue the default integer value returned if no value is found + * @param non-empty-string $charNestedKey for nested field the . character is the default */ public function getInt(int|string $key, ?int $defaultValue = null, string $charNestedKey = "."): ?int { diff --git a/tests/Unit/Traits/FormattableBlokTest.php b/tests/Unit/Traits/FormattableBlokTest.php index 16e90eb..758433f 100644 --- a/tests/Unit/Traits/FormattableBlokTest.php +++ b/tests/Unit/Traits/FormattableBlokTest.php @@ -58,25 +58,6 @@ function (): void { }, ); -test( - 'Force field to string', - 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"); - $data1->append($data2)->append($data3); - expect($data1)->toHaveCount(30); - expect($data2)->toHaveCount(10); - expect($data1->getString("0.commit.author.date"))->toBeString(); - expect($data1->getString("0.commit.author.notexist"))->toBeString(); - expect($data1->getString("0.commit.author.notexist"))->toEqual(""); - expect($data1->getString("0.commit.author.notexist", "AA"))->toEqual("AA"); - expect($data1->getString("0.commit.comment_count"))->toBeString()->toEqual("0"); - expect($data1->getString("0.commit.comment_count", 1))->toBeString()->toEqual("0"); - expect($data1->getString("0.commit.comment_countnotexists", 1))->toBeString()->toEqual("1"); - }, -); - test( 'Force field to boolean', function (): void { diff --git a/tests/Unit/Traits/TypeableBlockTest.php b/tests/Unit/Traits/TypeableBlockTest.php index 89b9724..add624c 100644 --- a/tests/Unit/Traits/TypeableBlockTest.php +++ b/tests/Unit/Traits/TypeableBlockTest.php @@ -2,6 +2,25 @@ use HiFolks\DataType\Block; +test( + 'Testing getString()', + 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"); + $data1->append($data2)->append($data3); + expect($data1)->toHaveCount(30); + expect($data2)->toHaveCount(10); + expect($data1->getString("0.commit.author.date"))->toBeString(); + expect($data1->getString("0.commit.author.notexist"))->toBeString(); + expect($data1->getString("0.commit.author.notexist"))->toEqual(""); + expect($data1->getString("0.commit.author.notexist", "AA"))->toEqual("AA"); + expect($data1->getString("0.commit.comment_count"))->toBeString()->toEqual("0"); + expect($data1->getString("0.commit.comment_count", 1))->toBeString()->toEqual("0"); + expect($data1->getString("0.commit.comment_countnotexists", 1))->toBeString()->toEqual("1"); + }, +); + test( 'Testing getInt()', function (): void {