diff --git a/bridges/MerchantAndMillsBridge.php b/bridges/MerchantAndMillsBridge.php new file mode 100644 index 00000000000..57d4aba0888 --- /dev/null +++ b/bridges/MerchantAndMillsBridge.php @@ -0,0 +1,100 @@ + [ + 'name' => 'Country', + 'type' => 'list', + 'values' => [ + 'European Union' => 0, + 'United Kingdom' => 1, + 'United States' => 2, + 'Other' => 3 + ] + ] + ]]; + + private function getCountryBlogPath($countryName): string + { + if ($countryName === 'European Union') { + return '/eu/blog'; + } + + if ($countryName === 'United Kingdom') { + return '/uk/blog'; + } + + if ($countryName === 'United States') { + return '/us/blog'; + } + + return '/rw/blog'; + } + + public function collectData() + { + $selectedCountryKey = $this->getKey('selected_country_id'); + $selectedCountryBlogPath = $this->getCountryBlogPath($selectedCountryKey); + $url = self::URI . $selectedCountryBlogPath; + $html = getSimpleHTMLDOM($url) + or returnServerError('Could not request ' . $url); + + foreach ($html->find('.products .post') as $post) { + $this->constructContent($post); + } + } + + /** + * @param $post simple_html_dom + */ + private function constructContent(simple_html_dom $post): void + { + $item = []; + + // Extract image + $image = $post->find('.post_image img', 0); + if ($image) { + // Resolve the relative image URL + $imageUrl = $image->src; + if (!str_starts_with($imageUrl, 'http')) { + $imageUrl = rtrim(self::URI, '/') . '/' . ltrim($imageUrl, '/'); + } + $item['image'] = $imageUrl; + } else { + $item['image'] = ''; + } + + // Extract title + $titleLink = $post->find('.post_name a', 0); + $item['title'] = $titleLink ? trim($titleLink->plaintext) : ''; + $item['uri'] = $titleLink ? self::URI . $titleLink->href : ''; + + // Extract date and views + $dateAndViews = $post->find('.post_date span'); + $item['date'] = isset($dateAndViews[0]) ? trim($dateAndViews[0]->plaintext) : ''; + $item['views'] = isset($dateAndViews[1]) ? trim(str_replace('Viewed:', '', $dateAndViews[1]->plaintext)) : ''; + + // Extract description + $description = $post->find('.post_desc', 0); + $item['content'] = ''; + + // Add the image and description to content + if (!empty($item['image'])) { + $item['content'] .= '