-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Hi-Folks:main' into patch-1
- Loading branch information
Showing
9 changed files
with
147 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace HiFolks\DataType\Traits; | ||
|
||
trait FormattableBlock | ||
{ | ||
/** | ||
* Get and format as date the element with $key | ||
* @param mixed $key the key, can be nested for example "some.datetime" | ||
* @param string $format the format default is "Y-m-d H:i:s" | ||
* @param mixed $defaultValue, the value returned in the case the key not exists | ||
* @param non-empty-string $charNestedKey the separator for nested keys, default is "." | ||
*/ | ||
public function getFormattedDateTime( | ||
mixed $key, | ||
string $format = "Y-m-d H:i:s", | ||
mixed $defaultValue = null, | ||
string $charNestedKey = ".", | ||
): string|null { | ||
$value = $this->get($key, $defaultValue, $charNestedKey); | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
$date = new \DateTimeImmutable($value); | ||
|
||
return $date->format($format); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
use HiFolks\DataType\Block; | ||
|
||
test( | ||
'Format field to data', | ||
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"); | ||
|
||
expect($data1)->toHaveCount(10); | ||
expect($data2)->toHaveCount(10); | ||
expect($data1->getFormattedDateTime("0.commit.author.date", "Y"))->toBe("2024"); | ||
expect($data1->getFormattedDateTime("0.commit.author.date", "Y-m-d"))->toBe("2024-06-28"); | ||
expect($data1->getFormattedDateTime("0.commit.author.dateNOTEXISTS", "Y-m-d"))->toBeNull(); | ||
|
||
}, | ||
); |