Skip to content

Commit b6f6672

Browse files
Merge pull request #43 from Hi-Folks/feat/getstring
getString() moved from Formattable traits to TYpeable, and finetuning…
2 parents 0116c65 + 1630e3f commit b6f6672

5 files changed

Lines changed: 39 additions & 35 deletions

File tree

src/Block.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function fromEncodedJson(mixed $encodedJson): self
7777
*
7878
* @param non-empty-string $charNestedKey
7979
*/
80-
public function get(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): mixed
80+
public function get(int|string $key, mixed $defaultValue = null, string $charNestedKey = "."): mixed
8181
{
8282
if (is_string($key)) {
8383
$keyString = strval($key);

src/Traits/FormattableBlock.php

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

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

8775
/**
8876
* Return a forced boolean value from the get() method

src/Traits/TypeableBlock.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,24 @@
77
trait TypeableBlock
88
{
99
/**
10-
*
11-
* @param non-empty-string $charNestedKey
10+
* Return a forced string value from the get() method
11+
* @param int|string $key the field key , can be nested for example "commits.0.name"
12+
* @param string|null $defaultValue the default value returned if no value is found
13+
* @param non-empty-string $charNestedKey for nested field the . character is the default
14+
*/
15+
public function getString(
16+
int|string $key,
17+
?string $defaultValue = null,
18+
string $charNestedKey = ".",
19+
): string {
20+
return (string) $this->get($key, $defaultValue, $charNestedKey);
21+
}
22+
23+
/**
24+
* Return a forced integer value from the get() method
25+
* @param int|string $key the field key, can be nested for example "0.author.id"
26+
* @param int|null $defaultValue the default integer value returned if no value is found
27+
* @param non-empty-string $charNestedKey for nested field the . character is the default
1228
*/
1329
public function getInt(int|string $key, ?int $defaultValue = null, string $charNestedKey = "."): ?int
1430
{

tests/Unit/Traits/FormattableBlokTest.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,6 @@ function (): void {
5858
},
5959
);
6060

61-
test(
62-
'Force field to string',
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->getString("0.commit.author.date"))->toBeString();
71-
expect($data1->getString("0.commit.author.notexist"))->toBeString();
72-
expect($data1->getString("0.commit.author.notexist"))->toEqual("");
73-
expect($data1->getString("0.commit.author.notexist", "AA"))->toEqual("AA");
74-
expect($data1->getString("0.commit.comment_count"))->toBeString()->toEqual("0");
75-
expect($data1->getString("0.commit.comment_count", 1))->toBeString()->toEqual("0");
76-
expect($data1->getString("0.commit.comment_countnotexists", 1))->toBeString()->toEqual("1");
77-
},
78-
);
79-
8061
test(
8162
'Force field to boolean',
8263
function (): void {

tests/Unit/Traits/TypeableBlockTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
use HiFolks\DataType\Block;
44

5+
test(
6+
'Testing getString()',
7+
function (): void {
8+
$data1 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p1.json");
9+
$data2 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p2.json");
10+
$data3 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p3.json");
11+
$data1->append($data2)->append($data3);
12+
expect($data1)->toHaveCount(30);
13+
expect($data2)->toHaveCount(10);
14+
expect($data1->getString("0.commit.author.date"))->toBeString();
15+
expect($data1->getString("0.commit.author.notexist"))->toBeString();
16+
expect($data1->getString("0.commit.author.notexist"))->toEqual("");
17+
expect($data1->getString("0.commit.author.notexist", "AA"))->toEqual("AA");
18+
expect($data1->getString("0.commit.comment_count"))->toBeString()->toEqual("0");
19+
expect($data1->getString("0.commit.comment_count", 1))->toBeString()->toEqual("0");
20+
expect($data1->getString("0.commit.comment_countnotexists", 1))->toBeString()->toEqual("1");
21+
},
22+
);
23+
524
test(
625
'Testing getInt()',
726
function (): void {

0 commit comments

Comments
 (0)