From d3e552b24b3a83d4c1c8c38e291f23c2f3405e81 Mon Sep 17 00:00:00 2001 From: Quark Wei Date: Sat, 27 May 2023 23:38:26 -0700 Subject: [PATCH] Add ability to prefix units to currency amounts --- workflow/calculateanything.php | 6 ++++++ workflow/tools/currency.php | 7 ++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/workflow/calculateanything.php b/workflow/calculateanything.php index 211ee31..2950449 100644 --- a/workflow/calculateanything.php +++ b/workflow/calculateanything.php @@ -95,6 +95,12 @@ public function processQuery() // the passed query must have at least 3 characters // being the first one a number if ($lenght < 3 || ($query[0] !== '-' && !is_numeric($query[0])) || ($query[0] === '-' && !is_numeric($query[1]))) { + self::$currencyCalculator = new Currency($query); + $currency = self::$currencyCalculator; + self::$cryptocurrencyCalculator = new Cryptocurrency($query); + if($currency->shouldProcess($lenght)){ + return $currency->processQuery(); + } return false; } diff --git a/workflow/tools/currency.php b/workflow/tools/currency.php index 19a6f85..9f91892 100644 --- a/workflow/tools/currency.php +++ b/workflow/tools/currency.php @@ -249,7 +249,7 @@ public function shouldProcess(int $strlenght = 0) $currencies = $this->matchRegex(); $stopwords = $this->getStopWordsString($this->stop_words); - return preg_match('/^([-\d+\.,\s]*) ?' . $currencies . ' ?' . $stopwords . '?/i', $query, $matches); + return preg_match('/^' . $currencies . '? ?' . '([-\d+\.,\s]*) ?' . $currencies . '? ?' . $stopwords . '?/i', $query, $matches); } @@ -541,13 +541,14 @@ private function extractQueryData($query) $to = ''; $default_currency = $this->getBaseCurrency(); $stopwords = $this->getStopWordsString($this->stop_words, ' %s '); + $currencies = $this->matchRegex(); - preg_match('/^([0-9,.\s]+)[^\d]/i', $query, $amount_match); + preg_match('/^' . $currencies . '? ?' . '([0-9,.\s]+)/i', $query, $amount_match); if (empty($amount_match)) { return false; } - $amount = \Alfred\getArgument($amount_match, 1); + $amount = \Alfred\getArgument($amount_match, count($amount_match) - 1); $amount = trim($amount); $string = str_replace($amount, '', $query); $string = trim($string);