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
2 changes: 1 addition & 1 deletion src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 1 addition & 13 deletions src/Traits/FormattableBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions src/Traits/TypeableBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
19 changes: 0 additions & 19 deletions tests/Unit/Traits/FormattableBlokTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Traits/TypeableBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down