From 3192a75893e784b4f76e3fe10e0784f89a5cfa65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Thu, 3 Jul 2025 11:42:48 -0300 Subject: [PATCH 1/5] Wrap pregen-news.inc with NewsHandler::getPregeneratedNews() This removes the $NEWS_ENTRIES global variable. --- archive/2013.php | 1 - archive/2014.php | 1 - archive/2015.php | 5 +++-- archive/2016.php | 5 +++-- archive/2017.php | 5 +++-- archive/2018.php | 5 +++-- archive/2019.php | 5 +++-- archive/2020.php | 5 +++-- archive/2021.php | 5 +++-- archive/2022.php | 5 +++-- archive/2023.php | 5 +++-- archive/2024.php | 5 +++-- archive/2025.php | 5 +++-- conferences/index.php | 5 +++-- include/layout.inc | 13 +++++++++---- index.php | 5 +++-- src/News/NewsHandler.php | 18 ++++++++++++++++++ 17 files changed, 66 insertions(+), 32 deletions(-) create mode 100644 src/News/NewsHandler.php diff --git a/archive/2013.php b/archive/2013.php index a6a745885b..744b8dd584 100644 --- a/archive/2013.php +++ b/archive/2013.php @@ -2,7 +2,6 @@ $_SERVER['BASE_PAGE'] = 'archive/2013.php'; include_once __DIR__ . '/../include/prepend.inc'; -include_once __DIR__ . '/../include/pregen-news.inc'; news_archive_sidebar(); site_header("News Archive - 2013", ["cache" => true]); ?> diff --git a/archive/2014.php b/archive/2014.php index e5d456aef2..7a61874b99 100644 --- a/archive/2014.php +++ b/archive/2014.php @@ -2,7 +2,6 @@ $_SERVER['BASE_PAGE'] = 'archive/2014.php'; include_once __DIR__ . '/../include/prepend.inc'; -include_once __DIR__ . '/../include/pregen-news.inc'; news_archive_sidebar(); site_header("News Archive - 2014", ["cache" => true]); ?> diff --git a/archive/2015.php b/archive/2015.php index b42a3dcef5..d0e7aebb8c 100644 --- a/archive/2015.php +++ b/archive/2015.php @@ -1,8 +1,9 @@ @@ -17,7 +18,7 @@ getPregeneratedNews(), ["conferences", "cfp", "frontpage", "nofrontpage"], 500, 2015); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2016.php b/archive/2016.php index 10e482cec7..edd3a63494 100644 --- a/archive/2016.php +++ b/archive/2016.php @@ -1,8 +1,9 @@ @@ -17,7 +18,7 @@ getPregeneratedNews(), ["conferences", "cfp", "frontpage", "nofrontpage"], 500, 2016); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2017.php b/archive/2017.php index f49960fc1b..694362b55b 100644 --- a/archive/2017.php +++ b/archive/2017.php @@ -1,8 +1,9 @@ @@ -17,7 +18,7 @@ getPregeneratedNews(), null, 500, 2017); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2018.php b/archive/2018.php index 7a9a74989c..8584aa3398 100644 --- a/archive/2018.php +++ b/archive/2018.php @@ -1,8 +1,9 @@ @@ -17,7 +18,7 @@ getPregeneratedNews(), null, 500, 2018); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2019.php b/archive/2019.php index 9e6d3ad3fb..f970255433 100644 --- a/archive/2019.php +++ b/archive/2019.php @@ -1,8 +1,9 @@ @@ -17,7 +18,7 @@ getPregeneratedNews(), null, 500, 2019); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2020.php b/archive/2020.php index ab66d3c0ea..6917678161 100644 --- a/archive/2020.php +++ b/archive/2020.php @@ -1,8 +1,9 @@ @@ -17,7 +18,7 @@ getPregeneratedNews(), null, 500, 2020); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2021.php b/archive/2021.php index 6a57771c18..c7d99d56f1 100644 --- a/archive/2021.php +++ b/archive/2021.php @@ -1,8 +1,9 @@ @@ -17,7 +18,7 @@ getPregeneratedNews(), null, 500, 2021); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2022.php b/archive/2022.php index 34a16adefc..12a18ca11d 100644 --- a/archive/2022.php +++ b/archive/2022.php @@ -1,8 +1,9 @@ @@ -17,5 +18,5 @@ getPregeneratedNews(), null, 500, 2022); site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2023.php b/archive/2023.php index 706530a7ce..13b91f0b1f 100644 --- a/archive/2023.php +++ b/archive/2023.php @@ -1,8 +1,9 @@ @@ -17,5 +18,5 @@ getPregeneratedNews(), null, 500, 2023); site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2024.php b/archive/2024.php index 43a145ae57..11bfcaf1f0 100644 --- a/archive/2024.php +++ b/archive/2024.php @@ -1,8 +1,9 @@ @@ -17,5 +18,5 @@ getPregeneratedNews(), null, 500, 2024); site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2025.php b/archive/2025.php index abbb2f1e0a..9c329c545c 100644 --- a/archive/2025.php +++ b/archive/2025.php @@ -1,8 +1,9 @@ @@ -17,5 +18,5 @@ getPregeneratedNews(), null, 500, 2025); site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/conferences/index.php b/conferences/index.php index 4ed6dd0627..1e1e65fbab 100644 --- a/conferences/index.php +++ b/conferences/index.php @@ -1,8 +1,9 @@ "; $frontpage = []; -foreach ($NEWS_ENTRIES as $entry) { +foreach ((new NewsHandler())->getPregeneratedNews() as $entry) { foreach ($entry["category"] as $category) { if ($category["term"] == "cfp") { $frontpage[] = $entry; diff --git a/include/layout.inc b/include/layout.inc index b3dc6dbfa9..dbf8e204f1 100644 --- a/include/layout.inc +++ b/include/layout.inc @@ -2,6 +2,7 @@ use phpweb\I18n\Languages; use phpweb\Navigation\NavItem; +use phpweb\News\NewsHandler; $_SERVER['STATIC_ROOT'] = $MYSITE; $_SERVER['MYSITE'] = $MYSITE; @@ -518,8 +519,12 @@ function get_nav_items(): array { function get_news_changes() { - include __DIR__ . "/pregen-news.inc"; - $date = date_create($NEWS_ENTRIES[0]["updated"]); + $newsEntries = (new NewsHandler())->getPregeneratedNews(); + if (!isset($newsEntries[0])) { + return false; + } + + $date = date_create($newsEntries[0]["updated"]); if (isset($_COOKIE["LAST_NEWS"]) && $_COOKIE["LAST_NEWS"] >= $date->getTimestamp()) { return false; } @@ -534,8 +539,8 @@ function get_news_changes() $date->modify("+1 week"); if ($date->getTimestamp() > $_SERVER["REQUEST_TIME"]) { - $link = preg_replace('~^(http://php.net/|https://www.php.net/)~', '/', $NEWS_ENTRIES[0]["link"][0]["href"]); - $title = $NEWS_ENTRIES[0]["title"]; + $link = preg_replace('~^(http://php.net/|https://www.php.net/)~', '/', $newsEntries[0]["link"][0]["href"]); + $title = $newsEntries[0]["title"]; return "{$title}"; } return false; diff --git a/index.php b/index.php index 42c293ae5b..ee502a5031 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,7 @@ "; $frontpage = []; -foreach ($NEWS_ENTRIES as $entry) { +foreach ((new NewsHandler())->getPregeneratedNews() as $entry) { foreach ($entry["category"] as $category) { if ($category["term"] == "frontpage") { $frontpage[] = $entry; diff --git a/src/News/NewsHandler.php b/src/News/NewsHandler.php new file mode 100644 index 0000000000..7c99f4e3e0 --- /dev/null +++ b/src/News/NewsHandler.php @@ -0,0 +1,18 @@ + Date: Sun, 6 Jul 2025 17:03:26 -0300 Subject: [PATCH 2/5] Add method phpweb\News\NewsHandler::getLastestNews() --- include/layout.inc | 10 +++++----- src/News/NewsHandler.php | 10 ++++++++++ tests/Unit/News/NewsHandlerTest.php | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 tests/Unit/News/NewsHandlerTest.php diff --git a/include/layout.inc b/include/layout.inc index dbf8e204f1..b2b74057c4 100644 --- a/include/layout.inc +++ b/include/layout.inc @@ -519,12 +519,12 @@ function get_nav_items(): array { function get_news_changes() { - $newsEntries = (new NewsHandler())->getPregeneratedNews(); - if (!isset($newsEntries[0])) { + $lastNews = (new NewsHandler())->getLastestNews(); + if ($lastNews === null) { return false; } - $date = date_create($newsEntries[0]["updated"]); + $date = date_create($lastNews["updated"]); if (isset($_COOKIE["LAST_NEWS"]) && $_COOKIE["LAST_NEWS"] >= $date->getTimestamp()) { return false; } @@ -539,8 +539,8 @@ function get_news_changes() $date->modify("+1 week"); if ($date->getTimestamp() > $_SERVER["REQUEST_TIME"]) { - $link = preg_replace('~^(http://php.net/|https://www.php.net/)~', '/', $newsEntries[0]["link"][0]["href"]); - $title = $newsEntries[0]["title"]; + $link = preg_replace('~^(http://php.net/|https://www.php.net/)~', '/', $lastNews["link"][0]["href"]); + $title = $lastNews["title"]; return "{$title}"; } return false; diff --git a/src/News/NewsHandler.php b/src/News/NewsHandler.php index 7c99f4e3e0..ccce97fbff 100644 --- a/src/News/NewsHandler.php +++ b/src/News/NewsHandler.php @@ -8,6 +8,16 @@ final class NewsHandler { + public function getLastestNews(): array|null + { + $news = $this->getPregeneratedNews(); + if (!isset($news[0])) { + return null; + } + + return $news[0]; + } + public function getPregeneratedNews(): array { $NEWS_ENTRIES = null; diff --git a/tests/Unit/News/NewsHandlerTest.php b/tests/Unit/News/NewsHandlerTest.php new file mode 100644 index 0000000000..a3ec2b56aa --- /dev/null +++ b/tests/Unit/News/NewsHandlerTest.php @@ -0,0 +1,21 @@ +getPregeneratedNews(); + self::assertArrayHasKey(0, $news); + self::assertSame($news[0], $newsHandler->getLastestNews()); + } +} From 3896dc196677a0c2d33f6db814e4af9c90ade594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sun, 6 Jul 2025 17:57:12 -0300 Subject: [PATCH 3/5] Add method phpweb\News\NewsHandler::getFrontPageNews() --- index.php | 13 +------------ src/News/NewsHandler.php | 22 ++++++++++++++++++++++ tests/Unit/News/NewsHandlerTest.php | 9 +++++++++ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/index.php b/index.php index ee502a5031..5e52b8a57c 100644 --- a/index.php +++ b/index.php @@ -58,18 +58,7 @@ mirror_setcookie("LAST_NEWS", $_SERVER["REQUEST_TIME"], 60 * 60 * 24 * 365); $content = "
"; -$frontpage = []; -foreach ((new NewsHandler())->getPregeneratedNews() as $entry) { - foreach ($entry["category"] as $category) { - if ($category["term"] == "frontpage") { - $frontpage[] = $entry; - if (count($frontpage) >= 25) { - break 2; - } - } - } -} -foreach ($frontpage as $entry) { +foreach ((new NewsHandler())->getFrontPageNews() as $entry) { $link = preg_replace('~^(http://php.net/|https://www.php.net/)~', '', $entry["id"]); $id = parse_url($entry["id"], PHP_URL_FRAGMENT); $date = date_create($entry['updated']); diff --git a/src/News/NewsHandler.php b/src/News/NewsHandler.php index ccce97fbff..c6d8da0344 100644 --- a/src/News/NewsHandler.php +++ b/src/News/NewsHandler.php @@ -8,6 +8,8 @@ final class NewsHandler { + private const MAX_FRONT_PAGE_NEWS = 25; + public function getLastestNews(): array|null { $news = $this->getPregeneratedNews(); @@ -18,6 +20,26 @@ public function getLastestNews(): array|null return $news[0]; } + /** @return list */ + public function getFrontPageNews(): array + { + $frontPage = []; + foreach ($this->getPregeneratedNews() as $entry) { + foreach ($entry['category'] as $category) { + if ($category['term'] !== 'frontpage') { + continue; + } + + $frontPage[] = $entry; + if (count($frontPage) >= self::MAX_FRONT_PAGE_NEWS) { + break 2; + } + } + } + + return $frontPage; + } + public function getPregeneratedNews(): array { $NEWS_ENTRIES = null; diff --git a/tests/Unit/News/NewsHandlerTest.php b/tests/Unit/News/NewsHandlerTest.php index a3ec2b56aa..065f389661 100644 --- a/tests/Unit/News/NewsHandlerTest.php +++ b/tests/Unit/News/NewsHandlerTest.php @@ -18,4 +18,13 @@ public function testGetLastestNews(): void self::assertArrayHasKey(0, $news); self::assertSame($news[0], $newsHandler->getLastestNews()); } + + public function testGetFrontPageNews(): void + { + $frontPage = (new NewsHandler())->getFrontPageNews(); + self::assertCount(25, $frontPage); + foreach ($frontPage as $news) { + self::assertContains(['term' => 'frontpage', 'label' => 'PHP.net frontpage news'], $news['category']); + } + } } From c5e1eceae6cd8e216d823872d16b4c1199ad0827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sun, 6 Jul 2025 20:22:15 -0300 Subject: [PATCH 4/5] Add method phpweb\News\NewsHandler::getConferences() --- conferences/index.php | 15 +-------------- src/News/NewsHandler.php | 18 ++++++++++++++++++ tests/Unit/News/NewsHandlerTest.php | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/conferences/index.php b/conferences/index.php index 1e1e65fbab..3718ba5baf 100644 --- a/conferences/index.php +++ b/conferences/index.php @@ -13,22 +13,9 @@ ]); $content = "
"; -$frontpage = []; -foreach ((new NewsHandler())->getPregeneratedNews() as $entry) { - foreach ($entry["category"] as $category) { - if ($category["term"] == "cfp") { - $frontpage[] = $entry; - break; - } - if ($category["term"] == "conferences") { - $frontpage[] = $entry; - break; - } - } -} $panels = '

Want to see your conference appear here?

'; -foreach ($frontpage as $entry) { +foreach ((new NewsHandler())->getConferences() as $entry) { $link = preg_replace('~^(http://php.net/|https://www.php.net/)~', '', $entry["id"]); $id = parse_url($entry["id"], PHP_URL_FRAGMENT); $date = date_format(date_create($entry["updated"]), 'Y-m-d'); diff --git a/src/News/NewsHandler.php b/src/News/NewsHandler.php index c6d8da0344..e0e0f37680 100644 --- a/src/News/NewsHandler.php +++ b/src/News/NewsHandler.php @@ -40,6 +40,24 @@ public function getFrontPageNews(): array return $frontPage; } + /** @return list */ + public function getConferences(): array + { + $conferences = []; + foreach ($this->getPregeneratedNews() as $entry) { + foreach ($entry['category'] as $category) { + if ($category['term'] !== 'cfp' && $category['term'] !== 'conferences') { + continue; + } + + $conferences[] = $entry; + break; + } + } + + return $conferences; + } + public function getPregeneratedNews(): array { $NEWS_ENTRIES = null; diff --git a/tests/Unit/News/NewsHandlerTest.php b/tests/Unit/News/NewsHandlerTest.php index 065f389661..d613b5d956 100644 --- a/tests/Unit/News/NewsHandlerTest.php +++ b/tests/Unit/News/NewsHandlerTest.php @@ -19,6 +19,23 @@ public function testGetLastestNews(): void self::assertSame($news[0], $newsHandler->getLastestNews()); } + public function testGetConferences(): void + { + $conferences = (new NewsHandler())->getConferences(); + self::assertNotEmpty($conferences); + foreach ($conferences as $conference) { + $isConference = false; + foreach ($conference['category'] as $category) { + if ($category['term'] === 'cfp' || $category['term'] === 'conferences') { + $isConference = true; + break; + } + } + + self::assertTrue($isConference); + } + } + public function testGetFrontPageNews(): void { $frontPage = (new NewsHandler())->getFrontPageNews(); From 3066201a64c6d8a9a2f7665df6eb0c5bc5676b25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sun, 6 Jul 2025 21:39:00 -0300 Subject: [PATCH 5/5] Add method phpweb\News\NewsHandler::getNewsByYear() --- archive/2015.php | 2 +- archive/2016.php | 2 +- archive/2017.php | 2 +- archive/2018.php | 2 +- archive/2019.php | 2 +- archive/2020.php | 2 +- archive/2021.php | 2 +- archive/2022.php | 2 +- archive/2023.php | 2 +- archive/2024.php | 2 +- archive/2025.php | 2 +- include/layout.inc | 6 +----- src/News/NewsHandler.php | 13 +++++++++++++ tests/Unit/News/NewsHandlerTest.php | 10 ++++++++++ 14 files changed, 35 insertions(+), 16 deletions(-) diff --git a/archive/2015.php b/archive/2015.php index d0e7aebb8c..b71a865e4b 100644 --- a/archive/2015.php +++ b/archive/2015.php @@ -18,7 +18,7 @@ getPregeneratedNews(), ["conferences", "cfp", "frontpage", "nofrontpage"], 500, 2015); +print_news((new NewsHandler())->getNewsByYear(2015), ["conferences", "cfp", "frontpage", "nofrontpage"], 500); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2016.php b/archive/2016.php index edd3a63494..228f0ea9a2 100644 --- a/archive/2016.php +++ b/archive/2016.php @@ -18,7 +18,7 @@ getPregeneratedNews(), ["conferences", "cfp", "frontpage", "nofrontpage"], 500, 2016); +print_news((new NewsHandler())->getNewsByYear(2016), ["conferences", "cfp", "frontpage", "nofrontpage"], 500); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2017.php b/archive/2017.php index 694362b55b..26e7b5a400 100644 --- a/archive/2017.php +++ b/archive/2017.php @@ -18,7 +18,7 @@ getPregeneratedNews(), null, 500, 2017); +print_news((new NewsHandler())->getNewsByYear(2017), null, 500); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2018.php b/archive/2018.php index 8584aa3398..1db8020b7b 100644 --- a/archive/2018.php +++ b/archive/2018.php @@ -18,7 +18,7 @@ getPregeneratedNews(), null, 500, 2018); +print_news((new NewsHandler())->getNewsByYear(2018), null, 500); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2019.php b/archive/2019.php index f970255433..3538aa2daa 100644 --- a/archive/2019.php +++ b/archive/2019.php @@ -18,7 +18,7 @@ getPregeneratedNews(), null, 500, 2019); +print_news((new NewsHandler())->getNewsByYear(2019), null, 500); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2020.php b/archive/2020.php index 6917678161..5a469f6cc9 100644 --- a/archive/2020.php +++ b/archive/2020.php @@ -18,7 +18,7 @@ getPregeneratedNews(), null, 500, 2020); +print_news((new NewsHandler())->getNewsByYear(2020), null, 500); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2021.php b/archive/2021.php index c7d99d56f1..1191c7585b 100644 --- a/archive/2021.php +++ b/archive/2021.php @@ -18,7 +18,7 @@ getPregeneratedNews(), null, 500, 2021); +print_news((new NewsHandler())->getNewsByYear(2021), null, 500); /* %s/\(.*\)<\/a>//g */ site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2022.php b/archive/2022.php index 12a18ca11d..abfcee9354 100644 --- a/archive/2022.php +++ b/archive/2022.php @@ -18,5 +18,5 @@ getPregeneratedNews(), null, 500, 2022); +print_news((new NewsHandler())->getNewsByYear(2022), null, 500); site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2023.php b/archive/2023.php index 13b91f0b1f..b6ef8abde9 100644 --- a/archive/2023.php +++ b/archive/2023.php @@ -18,5 +18,5 @@ getPregeneratedNews(), null, 500, 2023); +print_news((new NewsHandler())->getNewsByYear(2023), null, 500); site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2024.php b/archive/2024.php index 11bfcaf1f0..9834117665 100644 --- a/archive/2024.php +++ b/archive/2024.php @@ -18,5 +18,5 @@ getPregeneratedNews(), null, 500, 2024); +print_news((new NewsHandler())->getNewsByYear(2024), null, 500); site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/archive/2025.php b/archive/2025.php index 9c329c545c..2787ee7d73 100644 --- a/archive/2025.php +++ b/archive/2025.php @@ -18,5 +18,5 @@ getPregeneratedNews(), null, 500, 2025); +print_news((new NewsHandler())->getNewsByYear(2025), null, 500); site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]); diff --git a/include/layout.inc b/include/layout.inc index b2b74057c4..c69d390b32 100644 --- a/include/layout.inc +++ b/include/layout.inc @@ -347,7 +347,7 @@ function news_archive_sidebar(): void } // Print news -function print_news($news, $dog, $max = 5, $onlyyear = null, $return = false) { +function print_news($news, $dog, $max = 5, $return = false) { $retval = []; $count = 0; $news = $news ?: []; // default to empty array (if no news) @@ -392,10 +392,6 @@ function print_news($news, $dog, $max = 5, $onlyyear = null, $return = false) { $published = substr($item["published"], 0, 10); $nixtimestamp = strtotime($published); $newsdate = date("d M Y", $nixtimestamp); - if ($onlyyear && date("Y", $nixtimestamp) != $onlyyear) { - $count--; - continue; - } if ($return) { $retval[] = [ diff --git a/src/News/NewsHandler.php b/src/News/NewsHandler.php index e0e0f37680..a7e97f766e 100644 --- a/src/News/NewsHandler.php +++ b/src/News/NewsHandler.php @@ -4,6 +4,10 @@ namespace phpweb\News; +use DateTimeImmutable; + +use function array_filter; +use function array_values; use function is_array; final class NewsHandler @@ -58,6 +62,15 @@ public function getConferences(): array return $conferences; } + /** @return list */ + public function getNewsByYear(int $year): array + { + return array_values(array_filter( + $this->getPregeneratedNews(), + static fn (array $entry): bool => (int) (new DateTimeImmutable($entry['published']))->format('Y') === $year, + )); + } + public function getPregeneratedNews(): array { $NEWS_ENTRIES = null; diff --git a/tests/Unit/News/NewsHandlerTest.php b/tests/Unit/News/NewsHandlerTest.php index d613b5d956..25df5f7e77 100644 --- a/tests/Unit/News/NewsHandlerTest.php +++ b/tests/Unit/News/NewsHandlerTest.php @@ -4,6 +4,7 @@ namespace News; +use DateTimeImmutable; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use phpweb\News\NewsHandler; @@ -36,6 +37,15 @@ public function testGetConferences(): void } } + public function testGetNewsByYear(): void + { + $news = (new NewsHandler())->getNewsByYear(2018); + self::assertNotEmpty($news); + foreach ($news as $entry) { + self::assertSame('2018', (new DateTimeImmutable($entry['published']))->format('Y')); + } + } + public function testGetFrontPageNews(): void { $frontPage = (new NewsHandler())->getFrontPageNews();