Update #4

Merged
Llewellyn merged 4 commits from dev into master 2023-08-18 05:54:05 +00:00
5 changed files with 99 additions and 40 deletions

View File

@ -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

View File

@ -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);
}
/**

View File

@ -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);
}
/**

View File

@ -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('<h2>Curl Not Found!</h2><p>Please setup curl on your system, or the <b>Joomla Component</b> will not function correctly!</p>', 'Error');
// load this notice only once
self::$curlError = true;
}

View File

@ -101,7 +101,7 @@
elseif (!self::$curlError)
{
// set the notice
Factory::getApplication()->enqueueMessage(Text::_('<h2>Curl Not Found!</h2><p>Please setup curl on your system, or <b>[[[component]]]</b> will not function correctly!</p>'), 'Error');
Factory::getApplication()->enqueueMessage('<h2>Curl Not Found!</h2><p>Please setup curl on your system, or the <b>Joomla Component</b> will not function correctly!</p>', 'Error');
// load this notice only once
self::$curlError = true;
}