-
-
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.
imp: Group news and read links by dates
- Loading branch information
1 parent
e671774
commit a1ec6c2
Showing
9 changed files
with
254 additions
and
66 deletions.
There are no files selected for viewing
Binary file not shown.
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,52 @@ | ||
<?php | ||
|
||
namespace flusio\utils; | ||
|
||
use flusio\models; | ||
|
||
/** | ||
* A class to organise a list of links in a timeline (i.e. grouped by dates). | ||
* | ||
* @author Marien Fressinaud <dev@marienfressinaud.fr> | ||
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL | ||
*/ | ||
class LinksTimeline | ||
{ | ||
/** @var array<string, LinksTimeline\DateGroup> */ | ||
private array $dates_groups = []; | ||
|
||
/** | ||
* @param models\Link[] $links | ||
*/ | ||
public function __construct(array $links) | ||
{ | ||
foreach ($links as $link) { | ||
if (!$link->published_at) { | ||
continue; | ||
} | ||
|
||
$date_key = $link->published_at->format('Y-m-d'); | ||
if (isset($this->dates_groups[$date_key])) { | ||
$date_group = $this->dates_groups[$date_key]; | ||
} else { | ||
$date_group = new LinksTimeline\DateGroup($link->published_at); | ||
$this->dates_groups[$date_key] = $date_group; | ||
} | ||
|
||
$date_group->links[] = $link; | ||
} | ||
} | ||
|
||
/** | ||
* @return array<string, LinksTimeline\DateGroup> | ||
*/ | ||
public function datesGroups(): array | ||
{ | ||
return $this->dates_groups; | ||
} | ||
|
||
public function empty(): bool | ||
{ | ||
return empty($this->dates_groups); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace flusio\utils\LinksTimeline; | ||
|
||
use flusio\models; | ||
|
||
/** | ||
* @author Marien Fressinaud <dev@marienfressinaud.fr> | ||
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL | ||
*/ | ||
class DateGroup | ||
{ | ||
public \DateTimeImmutable $date; | ||
|
||
/** @var models\Link[] */ | ||
public array $links = []; | ||
|
||
public function __construct(\DateTimeImmutable $date) | ||
{ | ||
$this->date = $date; | ||
} | ||
|
||
public function isToday(): bool | ||
{ | ||
$today = \Minz\Time::now(); | ||
return $this->date->format('Y-m-d') === $today->format('Y-m-d'); | ||
} | ||
|
||
public function isYesterday(): bool | ||
{ | ||
$yesterday = \Minz\Time::ago(1, 'day'); | ||
return $this->date->format('Y-m-d') === $yesterday->format('Y-m-d'); | ||
} | ||
} |
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
Oops, something went wrong.