55namespace phpweb \News ;
66
77use DateTimeImmutable ;
8-
98use function array_filter ;
109use function array_values ;
10+ use function count ;
1111use function is_array ;
1212
13+ /**
14+ * @phpstan-type NewsEntryStruct array{
15+ * title: string,
16+ * id: string,
17+ * published: string,
18+ * updated?: string,
19+ * link?: list<array{
20+ * href: string,
21+ * rel: string,
22+ * type?: string,
23+ * }>,
24+ * category: list<array{
25+ * term: string,
26+ * label: string,
27+ * }>,
28+ * newsImage?: array{
29+ * link: string,
30+ * content: string,
31+ * },
32+ * content: string,
33+ * intro?: string,
34+ * finalTeaserDate?: string,
35+ * }
36+ */
1337final class NewsHandler
1438{
1539 private const MAX_FRONT_PAGE_NEWS = 25 ;
1640
41+ /**
42+ * @return NewsEntryStruct|null
43+ */
1744 public function getLastestNews (): array |null
1845 {
1946 $ news = $ this ->getPregeneratedNews ();
@@ -24,45 +51,48 @@ public function getLastestNews(): array|null
2451 return $ news [0 ];
2552 }
2653
27- /** @return list<array> */
28- public function getFrontPageNews (): array
54+ /**
55+ * @param list<string> $tags
56+ * @return list<NewsEntryStruct>
57+ */
58+ public function getTaggedEntries (array $ tags , ?int $ limit = null ): array
2959 {
30- $ frontPage = [];
60+ $ entries = [];
3161 foreach ($ this ->getPregeneratedNews () as $ entry ) {
32- foreach ($ entry ['category ' ] as $ category ) {
33- if ($ category ['term ' ] !== 'frontpage ' ) {
34- continue ;
35- }
36-
37- $ frontPage [] = $ entry ;
38- if (count ($ frontPage ) >= self ::MAX_FRONT_PAGE_NEWS ) {
39- break 2 ;
40- }
62+ if (!self ::isTagged ($ entry , $ tags )) {
63+ continue ;
64+ }
65+
66+ $ entries [] = $ entry ;
67+ if ($ limit !== null && count ($ entries ) >= $ limit ) {
68+ break ;
4169 }
4270 }
4371
44- return $ frontPage ;
72+ return $ entries ;
4573 }
4674
47- /** @return list<array> */
48- public function getConferences (): array
75+ /**
76+ * Looks up generated news with frontpage tags.
77+ *
78+ * @return list<NewsEntryStruct>
79+ */
80+ public function getFrontPageNews (int $ limit = self ::MAX_FRONT_PAGE_NEWS ): array
4981 {
50- $ conferences = [];
51- foreach ($ this ->getPregeneratedNews () as $ entry ) {
52- foreach ($ entry ['category ' ] as $ category ) {
53- if ($ category ['term ' ] !== 'cfp ' && $ category ['term ' ] !== 'conferences ' ) {
54- continue ;
55- }
56-
57- $ conferences [] = $ entry ;
58- break ;
59- }
60- }
82+ return $ this ->getTaggedEntries (['frontpage ' ], $ limit );
83+ }
6184
62- return $ conferences ;
85+ /**
86+ * @return list<NewsEntryStruct>
87+ */
88+ public function getConferences (?int $ limit = null ): array
89+ {
90+ return $ this ->getTaggedEntries (['cfp ' , 'conferences ' ], $ limit );
6391 }
6492
65- /** @return list<array> */
93+ /**
94+ * @return list<NewsEntryStruct>
95+ */
6696 public function getNewsByYear (int $ year ): array
6797 {
6898 return array_values (array_filter (
@@ -71,11 +101,32 @@ public function getNewsByYear(int $year): array
71101 ));
72102 }
73103
104+ /**
105+ * @return list<NewsEntryStruct>
106+ */
74107 public function getPregeneratedNews (): array
75108 {
76109 $ NEWS_ENTRIES = null ;
77110 include __DIR__ . '/../../include/pregen-news.inc ' ;
78111
112+ /** @phpstan-ignore-next-line - pregen-news sets global variable */
79113 return is_array ($ NEWS_ENTRIES ) ? $ NEWS_ENTRIES : [];
80114 }
115+
116+ /**
117+ * @param NewsEntryStruct $data
118+ * @param list<string>|string $tags
119+ */
120+ public static function isTagged (array $ data , array |string $ tags ): bool
121+ {
122+ $ tags = is_array ($ tags ) ? $ tags : [$ tags ];
123+
124+ foreach ($ data ['category ' ] as $ category ) {
125+ if (in_array ($ category ['term ' ], $ tags , true )) {
126+ return true ;
127+ }
128+ }
129+
130+ return false ;
131+ }
81132}
0 commit comments