Skip to content

Commit

Permalink
changed: $page->date renamed to $page->dateTime | Return value \DateT…
Browse files Browse the repository at this point in the history
…imeImmutable
  • Loading branch information
d4s6 committed Dec 23, 2022
1 parent 4e63b46 commit a575e32
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 24 deletions.
2 changes: 1 addition & 1 deletion genug_user/view/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</head>
<body>
<h1><?= $genug->requestedPage->title ?></h1>
<p><time datetime="<?= $genug->requestedPage->date ?>"><?= $genug->requestedPage->date?->format(DATE_RFC1123) ?></time></p>
<p><time datetime="<?= $genug->requestedPage->dateTime ?>"><?= $genug->requestedPage->dateTime?->format(DATE_RFC1123) ?></time></p>
<ul>
<li>PageGroup: <?= $genug->requestedPage->group; ?></li>
<?php $currentGroup = $genug->groups->fetchOrNull($genug->requestedPage->group); ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,29 @@
* License: MIT License
*/

namespace genug\Lib\ValueObject;
namespace genug\Lib;

use DateTime;
use DateTimeImmutable;
use InvalidArgumentException;

use function trim;
use Stringable;

/**
*
* @author David Ringsdorf http://davidringsdorf.de
* @license MIT License
*/
trait DateTimeTrait
abstract class AbstractDateTime extends DateTimeImmutable implements Stringable
{
protected readonly DateTime $dateTime;

public function __construct(string $dateTime)
{
if ('' === trim($dateTime)) {
throw new InvalidArgumentException('`$dateTime` is empty or consists of white space. Use `\'now\'` to use the current date and time.');
}
$this->dateTime = new DateTime($dateTime);
}

public function format(string $format): string
{
return $this->dateTime->format($format);
parent::__construct($dateTime);
}

public function __toString(): string
{
return $this->dateTime->format('c');
return $this->format('c');
}
}
5 changes: 2 additions & 3 deletions src/Page/DateInterface.php → src/Page/AbstractDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@

namespace genug\Page;

use Stringable;
use genug\Lib\AbstractDateTime as LibAbstractDateTime;

/**
*
* @author David Ringsdorf http://davidringsdorf.de
* @license MIT License
*/
interface DateInterface extends Stringable
abstract class AbstractDateTime extends LibAbstractDateTime
{
public function format(string $format): string;
}
3 changes: 1 addition & 2 deletions src/Page/Date.php → src/Page/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* @author David Ringsdorf http://davidringsdorf.de
* @license MIT License
*/
final class Date implements DateInterface
final class DateTime extends AbstractDateTime
{
use \genug\Lib\ValueObject\DateTimeTrait;
}
2 changes: 1 addition & 1 deletion src/Page/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
public readonly AbstractId $id,
public readonly ?AbstractGroup $group,
public readonly ?TitleInterface $title,
public readonly ?DateInterface $date,
public readonly ?AbstractDateTime $dateTime,
public readonly ?ContentInterface $content
) {
}
Expand Down
6 changes: 3 additions & 3 deletions src/Page/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,22 @@ protected function _parseFrontMatterString(string $str): array
return new Title($fm['title']);
})();

$date = (function () use ($_data, $idString, $logger): ?Date {
$dateTime = (function () use ($_data, $idString, $logger): ?DateTime {
$fm = $_data->frontMatter();
if (! isset($fm['date'])) {
$logger->debug(
sprintf('No date found for Page "%s"', $idString)
);
return null;
}
return new Date($fm['date']);
return new DateTime($fm['date']);
})();

$entity = new Entity(
new Id($idString),
$group,
$title,
$date,
$dateTime,
new Content($_data->content())
);

Expand Down

0 comments on commit a575e32

Please sign in to comment.