From f3539185ab0093f4efe1cdc013930ea3f2abfc70 Mon Sep 17 00:00:00 2001 From: Llewellyn van der Merwe Date: Thu, 30 Aug 2018 23:22:48 +0200 Subject: [PATCH] Moved the bcmath wrapper method to the helper class for all over use --- README.md | 2 +- admin/README.txt | 2 +- admin/helpers/compiler.php | 6 ++-- admin/helpers/compiler/a_Get.php | 42 ---------------------- admin/helpers/compiler/f_Infusion.php | 4 +-- admin/helpers/componentbuilder.php | 52 +++++++++++++++++++++++++++ site/helpers/componentbuilder.php | 52 +++++++++++++++++++++++++++ 7 files changed, 111 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index d16bbcd5f..c3d6191d2 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ Watch the [proposed development workflow](https://vdm.bz/proposed-development-wo + *Version*: 2.9.0 + *Copyright*: Copyright (C) 2015 - 2018 Vast Development Method. All rights reserved. + *License*: GNU General Public License version 2 or later; see LICENSE.txt -+ *Line count*: **192775** ++ *Line count*: **192879** + *Field count*: **1081** + *File count*: **1273** + *Folder count*: **201** diff --git a/admin/README.txt b/admin/README.txt index d16bbcd5f..c3d6191d2 100644 --- a/admin/README.txt +++ b/admin/README.txt @@ -129,7 +129,7 @@ Watch the [proposed development workflow](https://vdm.bz/proposed-development-wo + *Version*: 2.9.0 + *Copyright*: Copyright (C) 2015 - 2018 Vast Development Method. All rights reserved. + *License*: GNU General Public License version 2 or later; see LICENSE.txt -+ *Line count*: **192775** ++ *Line count*: **192879** + *Field count*: **1081** + *File count*: **1273** + *Folder count*: **201** diff --git a/admin/helpers/compiler.php b/admin/helpers/compiler.php index 496fba6d9..f3773cb0f 100644 --- a/admin/helpers/compiler.php +++ b/admin/helpers/compiler.php @@ -523,7 +523,7 @@ class Compiler extends Infusion $lineBites[$lineNumber] = (int) mb_strlen($lineContent, '8bit'); if (!$found) { - $bites = (int) $this->bcmath('add', $lineBites[$lineNumber], $bites); + $bites = (int) ComponentbuilderHelper::bcmath('add', $lineBites[$lineNumber], $bites); } if ($found && !$foundEnd) { @@ -659,12 +659,12 @@ class Compiler extends Infusion // Add the data fwrite($fpFile, $data); // truncate file at the end of the data that was added - $remove = $this->bcmath('add', $position, mb_strlen($data, '8bit')); + $remove = ComponentbuilderHelper::bcmath('add', $position, mb_strlen($data, '8bit')); ftruncate($fpFile, $remove); // check if this was a replacement of data if ($replace) { - $position = $this->bcmath('add', $position, $replace); + $position = ComponentbuilderHelper::bcmath('add', $position, $replace); } // move to the position of the data that should remain below the new data fseek($fpTemp, $position); diff --git a/admin/helpers/compiler/a_Get.php b/admin/helpers/compiler/a_Get.php index c1d240ab7..6d9e43359 100644 --- a/admin/helpers/compiler/a_Get.php +++ b/admin/helpers/compiler/a_Get.php @@ -6158,46 +6158,4 @@ class Get return false; } - /** - * bc math wrapper (very basic not for accounting) - * - * @param string $type The type bc math - * @param int $val1 The first value - * @param int $val2 The second value - * - * @return int - * - */ - public function bcmath($type, $val1, $val2) - { - // build function name - $function = 'bc' . $type; - // use the bcmath function of available - if (function_exists($function)) - { - return $function($val1, $val2); - } - // if function does not exist we use +-*/ operators (since it realy not that serious) - switch ($type) - { - // Multiply two numbers - case 'mul': - return round($val1 * $val2); - break; - // Divide of two numbers - case 'div': - return round($val1 / $val2); - break; - // Adding two numbers - case 'add': - return round($val1 + $val2); - break; - // Subtract one number from the other - case 'sub': - return round($val1 - $val2); - break; - } - return false; - } - } diff --git a/admin/helpers/compiler/f_Infusion.php b/admin/helpers/compiler/f_Infusion.php index b63de20ab..7e9b286fc 100644 --- a/admin/helpers/compiler/f_Infusion.php +++ b/admin/helpers/compiler/f_Infusion.php @@ -1122,8 +1122,8 @@ class Infusion extends Interpretation if ($this->langTag !== $tag) { $langStringNr = count($languageStrings); - $langStringSum = $this->bcmath('mul', $langStringNr, 100); - $percentage = $this->bcmath('div', $langStringSum, $mainLangLoader[$area]); + $langStringSum = ComponentbuilderHelper::bcmath('mul', $langStringNr, 100); + $percentage = ComponentbuilderHelper::bcmath('div', $langStringSum, $mainLangLoader[$area]); $stringNAme = ($langStringNr == 1) ? '(string ' . $tag . ' translated)' : '(strings ' . $tag . ' translated)'; // force load if debug lines are added if (!$this->debugLinenr) diff --git a/admin/helpers/componentbuilder.php b/admin/helpers/componentbuilder.php index 93af39f22..7edbf62af 100644 --- a/admin/helpers/componentbuilder.php +++ b/admin/helpers/componentbuilder.php @@ -2508,6 +2508,58 @@ abstract class ComponentbuilderHelper } } + /** + * bc math wrapper (very basic not for accounting) + * + * @param string $type The type bc math + * @param int $val1 The first value + * @param int $val2 The second value + * @param int $scale The scale value + * + * @return int + * + */ + public static function bcmath($type, $val1, $val2, $scale = 0) + { + // build function name + $function = 'bc' . $type; + // use the bcmath function of available + if (function_exists($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': + 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; + } + return false; + } + + /** * the locker * diff --git a/site/helpers/componentbuilder.php b/site/helpers/componentbuilder.php index 4b99d3e48..b7d95799b 100644 --- a/site/helpers/componentbuilder.php +++ b/site/helpers/componentbuilder.php @@ -2508,6 +2508,58 @@ abstract class ComponentbuilderHelper } } + /** + * bc math wrapper (very basic not for accounting) + * + * @param string $type The type bc math + * @param int $val1 The first value + * @param int $val2 The second value + * @param int $scale The scale value + * + * @return int + * + */ + public static function bcmath($type, $val1, $val2, $scale = 0) + { + // build function name + $function = 'bc' . $type; + // use the bcmath function of available + if (function_exists($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': + 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; + } + return false; + } + + /** * the locker *