Skip to content

Commit 3896dc1

Browse files
committed
Add method phpweb\News\NewsHandler::getFrontPageNews()
1 parent 300b124 commit 3896dc1

3 files changed

Lines changed: 32 additions & 12 deletions

File tree

index.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,7 @@
5858
mirror_setcookie("LAST_NEWS", $_SERVER["REQUEST_TIME"], 60 * 60 * 24 * 365);
5959

6060
$content = "<div class='home-content'>";
61-
$frontpage = [];
62-
foreach ((new NewsHandler())->getPregeneratedNews() as $entry) {
63-
foreach ($entry["category"] as $category) {
64-
if ($category["term"] == "frontpage") {
65-
$frontpage[] = $entry;
66-
if (count($frontpage) >= 25) {
67-
break 2;
68-
}
69-
}
70-
}
71-
}
72-
foreach ($frontpage as $entry) {
61+
foreach ((new NewsHandler())->getFrontPageNews() as $entry) {
7362
$link = preg_replace('~^(http://php.net/|https://www.php.net/)~', '', $entry["id"]);
7463
$id = parse_url($entry["id"], PHP_URL_FRAGMENT);
7564
$date = date_create($entry['updated']);

src/News/NewsHandler.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
final class NewsHandler
1010
{
11+
private const MAX_FRONT_PAGE_NEWS = 25;
12+
1113
public function getLastestNews(): array|null
1214
{
1315
$news = $this->getPregeneratedNews();
@@ -18,6 +20,26 @@ public function getLastestNews(): array|null
1820
return $news[0];
1921
}
2022

23+
/** @return list<array> */
24+
public function getFrontPageNews(): array
25+
{
26+
$frontPage = [];
27+
foreach ($this->getPregeneratedNews() as $entry) {
28+
foreach ($entry['category'] as $category) {
29+
if ($category['term'] !== 'frontpage') {
30+
continue;
31+
}
32+
33+
$frontPage[] = $entry;
34+
if (count($frontPage) >= self::MAX_FRONT_PAGE_NEWS) {
35+
break 2;
36+
}
37+
}
38+
}
39+
40+
return $frontPage;
41+
}
42+
2143
public function getPregeneratedNews(): array
2244
{
2345
$NEWS_ENTRIES = null;

tests/Unit/News/NewsHandlerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ public function testGetLastestNews(): void
1818
self::assertArrayHasKey(0, $news);
1919
self::assertSame($news[0], $newsHandler->getLastestNews());
2020
}
21+
22+
public function testGetFrontPageNews(): void
23+
{
24+
$frontPage = (new NewsHandler())->getFrontPageNews();
25+
self::assertCount(25, $frontPage);
26+
foreach ($frontPage as $news) {
27+
self::assertContains(['term' => 'frontpage', 'label' => 'PHP.net frontpage news'], $news['category']);
28+
}
29+
}
2130
}

0 commit comments

Comments
 (0)