Skip to content

Commit e180056

Browse files
Merge pull request #48 from Hi-Folks/feat/getstringstrict
Implementing getStringStrict()
2 parents e1751c0 + 5e3a1a2 commit e180056

4 files changed

Lines changed: 79 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Changelog
22

3-
## 1.0.2 - WIP
4-
- - Adding `getIntStrict()` method for returning strict int.
3+
## 1.0.2 - 2025-10-05
4+
- Adding `getIntStrict()` method for returning strict int.
5+
- Adding `getStringStrict()` method for returning strict string.
56

67
## 1.0.1 - 2025-10-04
78
- Adding `getInt()` method for returing casted integer value.

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,19 +263,37 @@ Key Features:
263263
### The `getString()` method
264264

265265
The `getString()` method retrieves the value of a specified field as a string from a data block. If the field does not exist or is null, it returns a default value, which can be customized.
266+
266267
Parameters:
267268

268269
- `$path` (string): The path to the field (e.g., "0.commit.author.date").
269-
- `$default` (string): (Optional) The default value to return if the field doesn't exist. Defaults to an empty string ("").
270+
- `$default` (string|null): (Optional) The default value to return if the field doesn't exist. Defaults to null.
270271

271272
Example Usage:
272273
```php
273274
$data1->getString("0.commit.author.date"); // Returns the field value as a string
274-
$data1->getString("0.commit.author.notexists"); // Returns ""
275+
$data1->getString("0.commit.author.notexists"); // Returns null
275276
$data1->getString("0.commit.author.notexists", "AA"); // Returns "AA"
276277
$data1->getString("0.commit.comment_count"); // Returns "0" as a string even if the field value is an integer
277278
```
278279

280+
### The `getStringStrict()` method
281+
282+
The `getStringStrict()` method retrieves the value of a specified field as a string from a data block. If the field does not exist or is null, it returns a default string value, which can be customized ("" by default).
283+
284+
Parameters:
285+
286+
- `$path` (string): The path to the field (e.g., "0.commit.author.date").
287+
- `$default` (string): (Optional) The default value to return if the field doesn't exist. Defaults to "".
288+
289+
Example Usage:
290+
```php
291+
$data1->getStringStrict("0.commit.author.date"); // Returns the field value as a string
292+
$data1->getStringStrict("0.commit.author.notexists"); // Returns an empty string ""
293+
$data1->getStringStrict("0.commit.author.notexists", "AA"); // Returns "AA"
294+
$data1->getStringStrict("0.commit.comment_count"); // Returns "0" as a string even if the field value is an integer
295+
```
296+
279297
### The `getInt()` method
280298

281299
The `getInt()` 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 (null by default).

src/Traits/TypeableBlock.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
trait TypeableBlock
88
{
99
/**
10-
* Return a forced string value from the get() method
10+
* Return a string value from the get() method
1111
* @param int|string $key the field key , can be nested for example "commits.0.name"
1212
* @param string|null $defaultValue the default value returned if no value is found
1313
* @param non-empty-string $charNestedKey for nested field the . character is the default
@@ -16,8 +16,42 @@ public function getString(
1616
int|string $key,
1717
?string $defaultValue = null,
1818
string $charNestedKey = ".",
19+
): ?string {
20+
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
21+
if ($returnValue === null) {
22+
return $defaultValue;
23+
}
24+
25+
if (is_scalar($returnValue)) {
26+
return strval($returnValue);
27+
}
28+
29+
return $defaultValue;
30+
31+
}
32+
33+
/**
34+
* Return a forced string value from the get() method
35+
* @param int|string $key the field key , can be nested for example "commits.0.name"
36+
* @param string $defaultValue the default value returned if no value is found, by default is ""
37+
* @param non-empty-string $charNestedKey for nested field the . character is the default
38+
*/
39+
public function getStringStrict(
40+
int|string $key,
41+
string $defaultValue = "",
42+
string $charNestedKey = ".",
1943
): string {
20-
return (string) $this->get($key, $defaultValue, $charNestedKey);
44+
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
45+
if ($returnValue === null) {
46+
return $defaultValue;
47+
}
48+
49+
if (is_scalar($returnValue)) {
50+
return strval($returnValue);
51+
}
52+
53+
return $defaultValue;
54+
2155
}
2256

2357
/**

tests/Unit/Traits/TypeableBlockTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,32 @@ function (): void {
1212
expect($data1)->toHaveCount(30);
1313
expect($data2)->toHaveCount(10);
1414
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");
15+
expect($data1->getString("0.commit.author.notexist"))->toBeNull();
16+
expect($data1->getString("0.commit.author.notexist", "AA"))->toBeString()->toEqual("AA");
1817
expect($data1->getString("0.commit.comment_count"))->toBeString()->toEqual("0");
1918
expect($data1->getString("0.commit.comment_count", 1))->toBeString()->toEqual("0");
2019
expect($data1->getString("0.commit.comment_countnotexists", 1))->toBeString()->toEqual("1");
2120
},
2221
);
2322

23+
test(
24+
'Testing getStringStrict()',
25+
function (): void {
26+
$data1 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p1.json");
27+
$data2 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p2.json");
28+
$data3 = Block::fromJsonFile(__DIR__ . "/../../data/commits-json/commits-10-p3.json");
29+
$data1->append($data2)->append($data3);
30+
expect($data1)->toHaveCount(30);
31+
expect($data2)->toHaveCount(10);
32+
expect($data1->getStringStrict("0.commit.author.date"))->toBeString();
33+
expect($data1->getStringStrict("0.commit.author.notexist"))->toBeString()->toBe("");
34+
expect($data1->getStringStrict("0.commit.author.notexist", "AA"))->toBeString()->toEqual("AA");
35+
expect($data1->getStringStrict("0.commit.comment_count"))->toBeString()->toEqual("0");
36+
expect($data1->getStringStrict("0.commit.comment_count", 1))->toBeString()->toEqual("0");
37+
expect($data1->getStringStrict("0.commit.comment_countnotexists", 1))->toBeString()->toEqual("1");
38+
},
39+
);
40+
2441
test(
2542
'Testing getInt()',
2643
function (): void {

0 commit comments

Comments
 (0)