aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar sysadminstory <sysadminstory@users.noreply.github.com> 2024-06-13 05:03:20 +0200
committerGravatar GitHub <noreply@github.com> 2024-06-13 05:03:20 +0200
commitbb1e308057b7e8740d67d0fb0fa9c6298e9b9730 (patch)
tree83efdb0b522179e0d0005c9a191a3a85698644c8
parente1b74aeb1bd120ac0c337fae9f55943cf7d00a0d (diff)
downloadrss-bridge-bb1e308057b7e8740d67d0fb0fa9c6298e9b9730.tar.gz
rss-bridge-bb1e308057b7e8740d67d0fb0fa9c6298e9b9730.tar.zst
rss-bridge-bb1e308057b7e8740d67d0fb0fa9c6298e9b9730.zip
[IdealoBridge] Fix price comparison and some PHP Notice (#4130)
* [IdealoBridge] Fix price comparison and some PHP Notice - The prices were compared as String and the comparison was wrong in some case : now the price are converted to float before the comparison, so the logic works really. - Don't show a new or used product price if it does not exist : this prevents a PHP Notice to be thrown * [IdealoBridge] Fix price conversion in case the price is null The conversion as float of the text price won't work if the price is null : we retunr null in this case now.
-rw-r--r--bridges/IdealoBridge.php43
1 files changed, 32 insertions, 11 deletions
diff --git a/bridges/IdealoBridge.php b/bridges/IdealoBridge.php
index 4eb66dcb..f426a45c 100644
--- a/bridges/IdealoBridge.php
+++ b/bridges/IdealoBridge.php
@@ -81,6 +81,25 @@ class IdealoBridge extends BridgeAbstract
return $title . ' - ' . $this::NAME;
}
+ /**
+ * Returns the Price as float
+ * @return float rhe price converted in float
+ */
+ private function convertPriceToFloat($price)
+ {
+ // Every price is stored / displayed as "xxx,xx €", but PHP can't convert it as float
+
+ if ($price !== null) {
+ // Convert comma as dot
+ $price = str_replace(',', '.', $price);
+ // Remove the '€' char
+ $price = str_replace('€', '', $price);
+ // Convert to float
+ return floatval($price);
+ } else {
+ return $price;
+ }
+ }
/**
* Returns the Price Trend emoji
@@ -88,8 +107,10 @@ class IdealoBridge extends BridgeAbstract
*/
private function getPriceTrend($NewPrice, $OldPrice)
{
- // In case there is no old PRice, then show no trend
- if ($OldPrice === null) {
+ $NewPrice = $this->convertPriceToFloat($NewPrice);
+ $OldPrice = $this->convertPriceToFloat($OldPrice);
+ // In case there is no old Price, then show no trend
+ if ($OldPrice === null || $OldPrice == 0) {
$trend = '';
} else if ($NewPrice > $OldPrice) {
$trend = '&#x2197;';
@@ -125,7 +146,7 @@ class IdealoBridge extends BridgeAbstract
$OldPriceNew = $this->loadCacheValue($KeyNEW);
$OldPriceUsed = $this->loadCacheValue($KeyUSED);
- // First button is new. Found at oopStage-conditionButton-wrapper-text class (.)
+ // First button contains the new price. Found at oopStage-conditionButton-wrapper-text class (.)
$FirstButton = $html->find('.oopStage-conditionButton-wrapper-text', 0);
if ($FirstButton) {
$PriceNew = $FirstButton->find('strong', 0)->plaintext;
@@ -133,7 +154,7 @@ class IdealoBridge extends BridgeAbstract
$this->saveCacheValue($KeyNEW, $PriceNew);
}
- // Second Button is used
+ // Second Button contains the used product price
$SecondButton = $html->find('.oopStage-conditionButton-wrapper-text', 1);
if ($SecondButton) {
$PriceUsed = $SecondButton->find('strong', 0)->plaintext;
@@ -149,7 +170,7 @@ class IdealoBridge extends BridgeAbstract
$content = '';
// Generate Content
- if (isset($PriceNew) && $PriceNew > 1) {
+ if (isset($PriceNew) && $this->convertPriceToFloat($PriceNew) > 0) {
$content .= sprintf('<p><b>Price New:</b><br>%s %s</p>', $PriceNew, $this->getPriceTrend($PriceNew, $OldPriceNew));
$content .= "<p><b>Price New before:</b><br>$OldPriceNew</p>";
}
@@ -158,7 +179,7 @@ class IdealoBridge extends BridgeAbstract
$content .= sprintf('<p><b>Max Price New:</b><br>%s,00 €</p>', $this->getInput('MaxPriceNew'));
}
- if (isset($PriceUsed) && $PriceUsed > 1) {
+ if (isset($PriceUsed) && $this->convertPriceToFloat($PriceUsed) > 0) {
$content .= sprintf('<p><b>Price Used:</b><br>%s %s</p>', $PriceUsed, $this->getPriceTrend($PriceUsed, $OldPriceUsed));
$content .= "<p><b>Price Used before:</b><br>$OldPriceUsed</p>";
}
@@ -176,7 +197,7 @@ class IdealoBridge extends BridgeAbstract
// Currently under Max new price
if ($this->getInput('MaxPriceNew') != '') {
- if (isset($PriceNew) && $PriceNew < $this->getInput('MaxPriceNew')) {
+ if (isset($PriceNew) && $this->convertPriceToFloat($PriceNew) < $this->getInput('MaxPriceNew')) {
$title = sprintf($Pricealarm, 'New', $PriceNew, $Productname, $now);
$item = [
'title' => $title,
@@ -190,7 +211,7 @@ class IdealoBridge extends BridgeAbstract
// Currently under Max used price
if ($this->getInput('MaxPriceUsed') != '') {
- if (isset($PriceUsed) && $PriceUsed < $this->getInput('MaxPriceUsed')) {
+ if (isset($PriceUsed) && $this->convertPriceToFloat($PriceUsed) < $this->getInput('MaxPriceUsed')) {
$title = sprintf($Pricealarm, 'Used', $PriceUsed, $Productname, $now);
$item = [
'title' => $title,
@@ -202,7 +223,7 @@ class IdealoBridge extends BridgeAbstract
}
}
- // General Priceupdate
+ // General Priceupdate Without any Max Price for new and Used product
if ($this->getInput('MaxPriceUsed') == '' && $this->getInput('MaxPriceNew') == '') {
// check if a relevant pricechange happened
if (
@@ -211,11 +232,11 @@ class IdealoBridge extends BridgeAbstract
) {
$title = 'Priceupdate! ';
- if (!$this->getInput('ExcludeNew')) {
+ if (!$this->getInput('ExcludeNew') && isset($PriceNew)) {
$title .= 'NEW' . $this->getPriceTrend($PriceNew, $OldPriceNew) . ' ';
}
- if (!$this->getInput('ExcludeUsed')) {
+ if (!$this->getInput('ExcludeUsed') && isset($PriceUsed)) {
$title .= 'USED' . $this->getPriceTrend($PriceUsed, $OldPriceUsed) . ' ';
}
$title .= $Productname;