From 6e2927c76776cd47afd662441334fc774c4d3a96 Mon Sep 17 00:00:00 2001 From: aB0t Date: Thu, 10 Aug 2023 07:21:17 +0200 Subject: [PATCH 1/4] update 2023-08-10 07:21:14 --- src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php b/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php index 7bbc782..1385775 100644 --- a/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php +++ b/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php @@ -132,7 +132,7 @@ abstract class FileHelper elseif (!self::$curlError) { // set the notice - Factory::getApplication()->enqueueMessage(Text::_('COM_GETBIBLE_HTWOCURL_NOT_FOUNDHTWOPPLEASE_SETUP_CURL_ON_YOUR_SYSTEM_OR_BGETBIBLEB_WILL_NOT_FUNCTION_CORRECTLYP'), 'Error'); + Factory::getApplication()->enqueueMessage(Text::_('COM_COMPONENTBUILDER_HTWOCURL_NOT_FOUNDHTWOPPLEASE_SETUP_CURL_ON_YOUR_SYSTEM_OR_BCOMPONENTBUILDERB_WILL_NOT_FUNCTION_CORRECTLYP'), 'Error'); // load this notice only once self::$curlError = true; } -- 2.45.1 From 6c0416194ea161ead6c21b40b99d00084add8776 Mon Sep 17 00:00:00 2001 From: aB0t Date: Thu, 10 Aug 2023 09:21:16 +0200 Subject: [PATCH 2/4] update 2023-08-10 09:21:16 --- src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php b/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php index 1385775..7bbc782 100644 --- a/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php +++ b/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php @@ -132,7 +132,7 @@ abstract class FileHelper elseif (!self::$curlError) { // set the notice - Factory::getApplication()->enqueueMessage(Text::_('COM_COMPONENTBUILDER_HTWOCURL_NOT_FOUNDHTWOPPLEASE_SETUP_CURL_ON_YOUR_SYSTEM_OR_BCOMPONENTBUILDERB_WILL_NOT_FUNCTION_CORRECTLYP'), 'Error'); + Factory::getApplication()->enqueueMessage(Text::_('COM_GETBIBLE_HTWOCURL_NOT_FOUNDHTWOPPLEASE_SETUP_CURL_ON_YOUR_SYSTEM_OR_BGETBIBLEB_WILL_NOT_FUNCTION_CORRECTLYP'), 'Error'); // load this notice only once self::$curlError = true; } -- 2.45.1 From 6a59495c488a282ea83541232636e205bc4c6703 Mon Sep 17 00:00:00 2001 From: aB0t Date: Thu, 10 Aug 2023 10:21:20 +0200 Subject: [PATCH 3/4] update 2023-08-10 10:21:17 --- src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php | 2 +- src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.power | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php b/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php index 7bbc782..0dd04d8 100644 --- a/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php +++ b/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.php @@ -132,7 +132,7 @@ abstract class FileHelper elseif (!self::$curlError) { // set the notice - Factory::getApplication()->enqueueMessage(Text::_('COM_GETBIBLE_HTWOCURL_NOT_FOUNDHTWOPPLEASE_SETUP_CURL_ON_YOUR_SYSTEM_OR_BGETBIBLEB_WILL_NOT_FUNCTION_CORRECTLYP'), 'Error'); + Factory::getApplication()->enqueueMessage('

Curl Not Found!

Please setup curl on your system, or the Joomla Component will not function correctly!

', 'Error'); // load this notice only once self::$curlError = true; } diff --git a/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.power b/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.power index c89d1b7..bca3ddc 100644 --- a/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.power +++ b/src/a223b31e-ea1d-4cdf-92ae-5f9becffaff0/code.power @@ -101,7 +101,7 @@ elseif (!self::$curlError) { // set the notice - Factory::getApplication()->enqueueMessage(Text::_('

Curl Not Found!

Please setup curl on your system, or [[[component]]] will not function correctly!

'), 'Error'); + Factory::getApplication()->enqueueMessage('

Curl Not Found!

Please setup curl on your system, or the Joomla Component will not function correctly!

', 'Error'); // load this notice only once self::$curlError = true; } -- 2.45.1 From 3d5c34a922fe985d1dc1f0b00f1be8ac6bec4866 Mon Sep 17 00:00:00 2001 From: aB0t Date: Fri, 18 Aug 2023 07:49:54 +0200 Subject: [PATCH 4/4] update 2023-08-18 07:49:54 --- .../README.md | 9 ++- .../code.php | 63 +++++++++++++------ .../code.power | 63 +++++++++++++------ 3 files changed, 97 insertions(+), 38 deletions(-) diff --git a/src/152c8793-8b75-4715-996a-257b9f65451c/README.md b/src/152c8793-8b75-4715-996a-257b9f65451c/README.md index 44df714..f240c11 100644 --- a/src/152c8793-8b75-4715-996a-257b9f65451c/README.md +++ b/src/152c8793-8b75-4715-996a-257b9f65451c/README.md @@ -11,7 +11,8 @@ ```uml @startuml abstract MathHelper #Orange { - + {static} bc(string $type, int $val1, ...) : int + + {static} bc(string $type, int $val1, ...) : string|int|null|bool + + {static} sum(array $array, int $scale = 4) : float + {static} sum(array $array, int $scale = 4) : float } @@ -19,7 +20,7 @@ note right of MathHelper::bc bc math wrapper (very basic not for accounting) since: 3.0.9 - return: int + return: string|int|null|bool arguments: string $type @@ -31,6 +32,10 @@ end note note right of MathHelper::sum Basic sum of an array with more precision + since: 3.0.9 + return: float + Basic sum of an array with more precision + since: 3.0.9 return: float end note diff --git a/src/152c8793-8b75-4715-996a-257b9f65451c/code.php b/src/152c8793-8b75-4715-996a-257b9f65451c/code.php index 60bc44b..a40f191 100644 --- a/src/152c8793-8b75-4715-996a-257b9f65451c/code.php +++ b/src/152c8793-8b75-4715-996a-257b9f65451c/code.php @@ -27,48 +27,75 @@ abstract class MathHelper * @param int $val2 The second value * @param int $scale The scale value * - * @return int + * @return string|int|null|bool * * @since 3.0.9 */ public static function bc($type, $val1, $val2, $scale = 0) { - // build function name + // Validate input + if (!is_numeric($val1) || !is_numeric($val2)) + { + return null; + } + + // Build function name $function = 'bc' . $type; - // use the bcmath function if available - if (function_exists($function)) + + // Use the bcmath function if available + if (is_callable($function)) { return $function($val1, $val2, $scale); } + // if function does not exist we use +-*/ operators (fallback - not ideal) switch ($type) { - // Multiply two numbers case 'mul': return (string) round($val1 * $val2, $scale); - break; - // Divide of two numbers case 'div': + if ($val2 == 0) return null; // Avoid division by zero return (string) round($val1 / $val2, $scale); - break; - // Adding two numbers case 'add': return (string) round($val1 + $val2, $scale); - break; - // Subtract one number from the other case 'sub': return (string) round($val1 - $val2, $scale); - break; - // Raise an arbitrary precision number to another case 'pow': return (string) round(pow($val1, $val2), $scale); - break; - // Compare two arbitrary precision numbers case 'comp': - return (round($val1,2) == round($val2,2)); - break; + $diff = round($val1 - $val2, $scale); + return ($diff > 0) ? 1 : (($diff < 0) ? -1 : 0); } - return false; + + return null; + } + + /** + * Basic sum of an array with more precision + * + * @param array $array The values to sum + * @param int $scale The scale value + * + * @return float + * + * @since 3.0.9 + */ + public static function sum($array, $scale = 4) + { + // use the bcadd function if available + if (function_exists('bcadd')) + { + // set the start value + $value = 0.0; + // loop the values and run bcadd + foreach($array as $val) + { + $value = bcadd($value, (string) $val, $scale); + } + return $value; + } + // fall back on array sum + return array_sum($array); } /** diff --git a/src/152c8793-8b75-4715-996a-257b9f65451c/code.power b/src/152c8793-8b75-4715-996a-257b9f65451c/code.power index 68c8863..e33904d 100644 --- a/src/152c8793-8b75-4715-996a-257b9f65451c/code.power +++ b/src/152c8793-8b75-4715-996a-257b9f65451c/code.power @@ -6,48 +6,75 @@ * @param int $val2 The second value * @param int $scale The scale value * - * @return int + * @return string|int|null|bool * * @since 3.0.9 */ public static function bc($type, $val1, $val2, $scale = 0) { - // build function name + // Validate input + if (!is_numeric($val1) || !is_numeric($val2)) + { + return null; + } + + // Build function name $function = 'bc' . $type; - // use the bcmath function if available - if (function_exists($function)) + + // Use the bcmath function if available + if (is_callable($function)) { return $function($val1, $val2, $scale); } + // if function does not exist we use +-*/ operators (fallback - not ideal) switch ($type) { - // Multiply two numbers case 'mul': return (string) round($val1 * $val2, $scale); - break; - // Divide of two numbers case 'div': + if ($val2 == 0) return null; // Avoid division by zero return (string) round($val1 / $val2, $scale); - break; - // Adding two numbers case 'add': return (string) round($val1 + $val2, $scale); - break; - // Subtract one number from the other case 'sub': return (string) round($val1 - $val2, $scale); - break; - // Raise an arbitrary precision number to another case 'pow': return (string) round(pow($val1, $val2), $scale); - break; - // Compare two arbitrary precision numbers case 'comp': - return (round($val1,2) == round($val2,2)); - break; + $diff = round($val1 - $val2, $scale); + return ($diff > 0) ? 1 : (($diff < 0) ? -1 : 0); } - return false; + + return null; + } + + /** + * Basic sum of an array with more precision + * + * @param array $array The values to sum + * @param int $scale The scale value + * + * @return float + * + * @since 3.0.9 + */ + public static function sum($array, $scale = 4) + { + // use the bcadd function if available + if (function_exists('bcadd')) + { + // set the start value + $value = 0.0; + // loop the values and run bcadd + foreach($array as $val) + { + $value = bcadd($value, (string) $val, $scale); + } + return $value; + } + // fall back on array sum + return array_sum($array); } /** -- 2.45.1