From 1c3c85d8ff5a6d071f688ef09ca93f275b4995af Mon Sep 17 00:00:00 2001 From: Niehztog Date: Sun, 31 Mar 2024 18:46:07 +0200 Subject: [PATCH] [XPathBridge] Allow multiple categories (#4038) * [XPathAbstract] allow multiple categories * fix feed icons in two bridges * fix warning * fix linter errors --- bridges/BlizzardNewsBridge.php | 7 ++++ bridges/NiusBridge.php | 5 +++ lib/XPathAbstract.php | 74 +++++++++++++++++++++++++--------- 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/bridges/BlizzardNewsBridge.php b/bridges/BlizzardNewsBridge.php index 3930e0a4d1a..19c38152cc0 100644 --- a/bridges/BlizzardNewsBridge.php +++ b/bridges/BlizzardNewsBridge.php @@ -57,4 +57,11 @@ protected function getSourceUrl() } return 'https://news.blizzard.com/' . $locale; } + + public function getIcon() + { + return <<getItemValueOrNodeValue($typedResult, $isContent, $isContent && !$this->getSettingUseRawItemContent()); + $isCategories = 'categories' === $param; + $value = $this->getItemValueOrNodeValue($typedResult, $isContent, $isContent && !$this->getSettingUseRawItemContent(), $isCategories); $item->__set($param, $this->formatParamValue($param, $value)); } @@ -459,7 +460,7 @@ public function collectData() */ protected function formatParamValue($param, $value) { - $value = $this->fixEncoding($value); + $value = is_array($value) ? array_map([$this, 'fixEncoding'], $value) : $this->fixEncoding($value); switch ($param) { case 'title': return $this->formatItemTitle($value); @@ -572,12 +573,12 @@ protected function formatItemEnclosures($value) * formatted as array. * Can be easily overwritten for in case the values need to be transformed into something * else. - * @param string $value + * @param string|array $value * @return array */ protected function formatItemCategories($value) { - return [$value]; + return is_array($value) ? $value : [$value]; } /** @@ -596,22 +597,21 @@ protected function cleanMediaUrl($mediaUrl) /** * @param $typedResult - * @return string + * @param bool $returnXML + * @param bool $escapeHtml + * @param bool $allowMultiple + * @return string|array + * @throws Exception */ - protected function getItemValueOrNodeValue($typedResult, $returnXML = false, $escapeHtml = false) + protected function getItemValueOrNodeValue($typedResult, $returnXML = false, $escapeHtml = false, $allowMultiple = false) { - if ($typedResult instanceof \DOMNodeList) { + if ($typedResult instanceof \DOMNodeList && !$allowMultiple) { $item = $typedResult->item(0); - if ($item instanceof \DOMElement) { - // Don't escape XML - if ($returnXML) { - return ($item->ownerDocument ?? $item)->saveXML($item); - } - $text = $item->nodeValue; - } elseif ($item instanceof \DOMAttr) { - $text = $item->value; - } elseif ($item instanceof \DOMText) { - $text = $item->wholeText; + $text = $this->extractNodeListContent($item, $returnXML); + } elseif ($typedResult instanceof \DOMNodeList && $allowMultiple) { + $text = []; + foreach ($typedResult as $item) { + $text[] = $this->extractNodeListContent($item, $returnXML); } } elseif (is_string($typedResult) && strlen($typedResult) > 0) { $text = $typedResult; @@ -619,10 +619,46 @@ protected function getItemValueOrNodeValue($typedResult, $returnXML = false, $es throw new \Exception('Unknown type of XPath expression result.'); } + if (is_array($text)) { + foreach ($text as &$element) { + $element = $this->cleanExtractedText($element, $escapeHtml, $returnXML); + } + } else { + $text = $this->cleanExtractedText($text, $escapeHtml, $returnXML); + } + return $text; + } + + /** + * @param $item + * @param $returnXML + * @return false|string + * @throws Exception + */ + protected function extractNodeListContent($item, $returnXML) + { + if ($item instanceof \DOMElement) { + return $returnXML ? ($item->ownerDocument ?? $item)->saveXML($item) : $item->nodeValue; + } elseif ($item instanceof \DOMAttr) { + return $item->value; + } elseif ($item instanceof \DOMText) { + return $item->wholeText; + } + throw new \Exception('Unknown type of XPath expression result.'); + } + + /** + * @param $text + * @param $escapeHtml + * @param $returnXML + * @return string + */ + protected function cleanExtractedText($text, $escapeHtml, $returnXML) + { $text = trim($text); - if ($escapeHtml) { - return htmlspecialchars($text); + if ($escapeHtml && !$returnXML) { + $text = htmlspecialchars($text); } return $text; }