summaryrefslogtreecommitdiffstats
path: root/library/vendor/Zend/Locale/Math
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--library/vendor/Zend/Locale/Math.php354
-rw-r--r--library/vendor/Zend/Locale/Math/Exception.php52
-rw-r--r--library/vendor/Zend/Locale/Math/PhpMath.php239
3 files changed, 645 insertions, 0 deletions
diff --git a/library/vendor/Zend/Locale/Math.php b/library/vendor/Zend/Locale/Math.php
new file mode 100644
index 0000000..1b61b07
--- /dev/null
+++ b/library/vendor/Zend/Locale/Math.php
@@ -0,0 +1,354 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category Zend
+ * @package Zend_Locale
+ * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id$
+ */
+
+
+/**
+ * Utility class for proxying math function to bcmath functions, if present,
+ * otherwise to PHP builtin math operators, with limited detection of overflow conditions.
+ * Sampling of PHP environments and platforms suggests that at least 80% to 90% support bcmath.
+ * Thus, this file should be as light as possible.
+ *
+ * @category Zend
+ * @package Zend_Locale
+ * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+class Zend_Locale_Math
+{
+ // support unit testing without using bcmath functions
+ public static $_bcmathDisabled = false;
+
+ public static $add = array('Zend_Locale_Math', 'Add');
+ public static $sub = array('Zend_Locale_Math', 'Sub');
+ public static $pow = array('Zend_Locale_Math', 'Pow');
+ public static $mul = array('Zend_Locale_Math', 'Mul');
+ public static $div = array('Zend_Locale_Math', 'Div');
+ public static $comp = array('Zend_Locale_Math', 'Comp');
+ public static $sqrt = array('Zend_Locale_Math', 'Sqrt');
+ public static $mod = array('Zend_Locale_Math', 'Mod');
+ public static $scale = 'bcscale';
+
+ public static function isBcmathDisabled()
+ {
+ return self::$_bcmathDisabled;
+ }
+
+ /**
+ * Surprisingly, the results of this implementation of round()
+ * prove better than the native PHP round(). For example, try:
+ * round(639.795, 2);
+ * round(267.835, 2);
+ * round(0.302515, 5);
+ * round(0.36665, 4);
+ * then try:
+ * Zend_Locale_Math::round('639.795', 2);
+ */
+ public static function round($op1, $precision = 0)
+ {
+ if (self::$_bcmathDisabled) {
+ $op1 = round($op1, $precision);
+ if (strpos((string) $op1, 'E') === false) {
+ return self::normalize(round($op1, $precision));
+ }
+ }
+
+ if (strpos($op1, 'E') !== false) {
+ $op1 = self::floatalize($op1);
+ }
+
+ $op1 = trim(self::normalize($op1));
+ $length = strlen($op1);
+ if (($decPos = strpos($op1, '.')) === false) {
+ $op1 .= '.0';
+ $decPos = $length;
+ $length += 2;
+ }
+ if ($precision < 0 && abs($precision) > $decPos) {
+ return '0';
+ }
+
+ $digitsBeforeDot = $length - ($decPos + 1);
+ if ($precision >= ($length - ($decPos + 1))) {
+ return $op1;
+ }
+
+ if ($precision === 0) {
+ $triggerPos = 1;
+ $roundPos = -1;
+ } elseif ($precision > 0) {
+ $triggerPos = $precision + 1;
+ $roundPos = $precision;
+ } else {
+ $triggerPos = $precision;
+ $roundPos = $precision -1;
+ }
+
+ $triggerDigit = $op1[$triggerPos + $decPos];
+ if ($precision < 0) {
+ // zero fill digits to the left of the decimal place
+ $op1 = substr($op1, 0, $decPos + $precision) . str_pad('', abs($precision), '0');
+ }
+
+ if ($triggerDigit >= '5') {
+ if ($roundPos + $decPos == -1) {
+ return str_pad('1', $decPos + 1, '0');
+ }
+
+ $roundUp = str_pad('', $length, '0');
+ $roundUp[$decPos] = '.';
+ $roundUp[$roundPos + $decPos] = '1';
+
+ if ($op1 > 0) {
+ if (self::$_bcmathDisabled) {
+ return Zend_Locale_Math_PhpMath::Add($op1, $roundUp, $precision);
+ }
+ return self::Add($op1, $roundUp, $precision);
+ } else {
+ if (self::$_bcmathDisabled) {
+ return Zend_Locale_Math_PhpMath::Sub($op1, $roundUp, $precision);
+ }
+ return self::Sub($op1, $roundUp, $precision);
+ }
+ } elseif ($precision >= 0) {
+ return substr($op1, 0, $decPos + ($precision ? $precision + 1: 0));
+ }
+
+ return (string) $op1;
+ }
+
+ /**
+ * Convert a scientific notation to float
+ * Additionally fixed a problem with PHP <= 5.2.x with big integers
+ *
+ * @param string $value
+ */
+ public static function floatalize($value)
+ {
+ $value = strtoupper($value);
+ if (strpos($value, 'E') === false) {
+ return $value;
+ }
+
+ $number = substr($value, 0, strpos($value, 'E'));
+ if (strpos($number, '.') !== false) {
+ $post = strlen(substr($number, strpos($number, '.') + 1));
+ $mantis = substr($value, strpos($value, 'E') + 1);
+ if ($mantis < 0) {
+ $post += abs((int) $mantis);
+ }
+
+ $value = number_format($value, $post, '.', '');
+ } else {
+ $value = number_format($value, 0, '.', '');
+ }
+
+ return $value;
+ }
+
+ /**
+ * Normalizes an input to standard english notation
+ * Fixes a problem of BCMath with setLocale which is PHP related
+ *
+ * @param integer $value Value to normalize
+ * @return string Normalized string without BCMath problems
+ */
+ public static function normalize($value)
+ {
+ $convert = localeconv();
+ $value = str_replace($convert['thousands_sep'], "",(string) $value);
+ $value = str_replace($convert['positive_sign'], "", $value);
+ $value = str_replace($convert['decimal_point'], ".",$value);
+ if (!empty($convert['negative_sign']) and (strpos($value, $convert['negative_sign']))) {
+ $value = str_replace($convert['negative_sign'], "", $value);
+ $value = "-" . $value;
+ }
+
+ return $value;
+ }
+
+ /**
+ * Localizes an input from standard english notation
+ * Fixes a problem of BCMath with setLocale which is PHP related
+ *
+ * @param integer $value Value to normalize
+ * @return string Normalized string without BCMath problems
+ */
+ public static function localize($value)
+ {
+ $convert = localeconv();
+ $value = str_replace(".", $convert['decimal_point'], (string) $value);
+ if (!empty($convert['negative_sign']) and (strpos($value, "-"))) {
+ $value = str_replace("-", $convert['negative_sign'], $value);
+ }
+ return $value;
+ }
+
+ /**
+ * Changes exponential numbers to plain string numbers
+ * Fixes a problem of BCMath with numbers containing exponents
+ *
+ * @param integer $value Value to erase the exponent
+ * @param integer $scale (Optional) Scale to use
+ * @return string
+ */
+ public static function exponent($value, $scale = null)
+ {
+ if (!extension_loaded('bcmath')) {
+ return $value;
+ }
+
+ $split = explode('e', $value);
+ if (count($split) == 1) {
+ $split = explode('E', $value);
+ }
+
+ if (count($split) > 1) {
+ $value = bcmul($split[0], bcpow(10, $split[1], $scale), $scale);
+ }
+
+ return $value;
+ }
+
+ /**
+ * BCAdd - fixes a problem of BCMath and exponential numbers
+ *
+ * @param string $op1
+ * @param string $op2
+ * @param integer $scale
+ * @return string
+ */
+ public static function Add($op1, $op2, $scale = null)
+ {
+ $op1 = self::exponent($op1, $scale);
+ $op2 = self::exponent($op2, $scale);
+
+ return bcadd($op1, $op2, $scale);
+ }
+
+ /**
+ * BCSub - fixes a problem of BCMath and exponential numbers
+ *
+ * @param string $op1
+ * @param string $op2
+ * @param integer $scale
+ * @return string
+ */
+ public static function Sub($op1, $op2, $scale = null)
+ {
+ $op1 = self::exponent($op1, $scale);
+ $op2 = self::exponent($op2, $scale);
+ return bcsub($op1, $op2, $scale);
+ }
+
+ /**
+ * BCPow - fixes a problem of BCMath and exponential numbers
+ *
+ * @param string $op1
+ * @param string $op2
+ * @param integer $scale
+ * @return string
+ */
+ public static function Pow($op1, $op2, $scale = null)
+ {
+ $op1 = self::exponent($op1, $scale);
+ $op2 = self::exponent($op2, $scale);
+ return bcpow($op1, $op2, $scale);
+ }
+
+ /**
+ * BCMul - fixes a problem of BCMath and exponential numbers
+ *
+ * @param string $op1
+ * @param string $op2
+ * @param integer $scale
+ * @return string
+ */
+ public static function Mul($op1, $op2, $scale = null)
+ {
+ $op1 = self::exponent($op1, $scale);
+ $op2 = self::exponent($op2, $scale);
+ return bcmul($op1, $op2, $scale);
+ }
+
+ /**
+ * BCDiv - fixes a problem of BCMath and exponential numbers
+ *
+ * @param string $op1
+ * @param string $op2
+ * @param integer $scale
+ * @return string
+ */
+ public static function Div($op1, $op2, $scale = null)
+ {
+ $op1 = self::exponent($op1, $scale);
+ $op2 = self::exponent($op2, $scale);
+ return bcdiv($op1, $op2, $scale);
+ }
+
+ /**
+ * BCSqrt - fixes a problem of BCMath and exponential numbers
+ *
+ * @param string $op1
+ * @param integer $scale
+ * @return string
+ */
+ public static function Sqrt($op1, $scale = null)
+ {
+ $op1 = self::exponent($op1, $scale);
+ return bcsqrt($op1, $scale);
+ }
+
+ /**
+ * BCMod - fixes a problem of BCMath and exponential numbers
+ *
+ * @param string $op1
+ * @param string $op2
+ * @return string
+ */
+ public static function Mod($op1, $op2)
+ {
+ $op1 = self::exponent($op1);
+ $op2 = self::exponent($op2);
+ return bcmod($op1, $op2);
+ }
+
+ /**
+ * BCComp - fixes a problem of BCMath and exponential numbers
+ *
+ * @param string $op1
+ * @param string $op2
+ * @param integer $scale
+ * @return string
+ */
+ public static function Comp($op1, $op2, $scale = null)
+ {
+ $op1 = self::exponent($op1, $scale);
+ $op2 = self::exponent($op2, $scale);
+ return bccomp($op1, $op2, $scale);
+ }
+}
+
+if (!extension_loaded('bcmath')
+ || (defined('TESTS_ZEND_LOCALE_BCMATH_ENABLED') && !TESTS_ZEND_LOCALE_BCMATH_ENABLED)
+) {
+ Zend_Locale_Math_PhpMath::disable();
+}
diff --git a/library/vendor/Zend/Locale/Math/Exception.php b/library/vendor/Zend/Locale/Math/Exception.php
new file mode 100644
index 0000000..fbf6c55
--- /dev/null
+++ b/library/vendor/Zend/Locale/Math/Exception.php
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category Zend
+ * @package Zend_Locale
+ * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
+ * @version $Id$
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+
+/**
+ * Zend_Exception
+ */
+
+
+/**
+ * @category Zend
+ * @package Zend_Locale
+ * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class Zend_Locale_Math_Exception extends Zend_Locale_Exception
+{
+ protected $op1 = null;
+ protected $op2 = null;
+ protected $result = null;
+
+ public function __construct($message, $op1 = null, $op2 = null, $result = null)
+ {
+ $this->op1 = $op1;
+ $this->op2 = $op2;
+ $this->result = $result;
+ parent::__construct($message);
+ }
+
+ public function getResults()
+ {
+ return array($this->op1, $this->op2, $this->result);
+ }
+}
diff --git a/library/vendor/Zend/Locale/Math/PhpMath.php b/library/vendor/Zend/Locale/Math/PhpMath.php
new file mode 100644
index 0000000..6d774c2
--- /dev/null
+++ b/library/vendor/Zend/Locale/Math/PhpMath.php
@@ -0,0 +1,239 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category Zend
+ * @package Zend_Locale
+ * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id$
+ */
+
+
+/**
+ * Utility class for proxying math function to bcmath functions, if present,
+ * otherwise to PHP builtin math operators, with limited detection of overflow conditions.
+ * Sampling of PHP environments and platforms suggests that at least 80% to 90% support bcmath.
+ * This file should only be loaded for the 10% to 20% lacking access to the bcmath extension.
+ *
+ * @category Zend
+ * @package Zend_Locale
+ * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class Zend_Locale_Math_PhpMath extends Zend_Locale_Math
+{
+ public static function disable()
+ {
+ self::$_bcmathDisabled = true;
+ self::$add = array('Zend_Locale_Math_PhpMath', 'Add');
+ self::$sub = array('Zend_Locale_Math_PhpMath', 'Sub');
+ self::$pow = array('Zend_Locale_Math_PhpMath', 'Pow');
+ self::$mul = array('Zend_Locale_Math_PhpMath', 'Mul');
+ self::$div = array('Zend_Locale_Math_PhpMath', 'Div');
+ self::$comp = array('Zend_Locale_Math_PhpMath', 'Comp');
+ self::$sqrt = array('Zend_Locale_Math_PhpMath', 'Sqrt');
+ self::$mod = array('Zend_Locale_Math_PhpMath', 'Mod');
+ self::$scale = array('Zend_Locale_Math_PhpMath', 'Scale');
+
+ self::$defaultScale = 0;
+ self::$defaultPrecision = 1;
+ }
+
+ public static $defaultScale;
+ public static $defaultPrecision;
+
+
+ public static function Add($op1, $op2, $scale = null)
+ {
+ if ($scale === null) {
+ $scale = Zend_Locale_Math_PhpMath::$defaultScale;
+ $precision = Zend_Locale_Math_PhpMath::$defaultPrecision;
+ } else {
+ $precision = pow(10, -$scale);
+ }
+
+ if (empty($op1)) {
+ $op1 = 0;
+ }
+ $op1 = self::normalize($op1);
+ $op2 = self::normalize($op2);
+ $result = $op1 + $op2;
+ if (is_infinite($result) or (abs($result - $op2 - $op1) > $precision)) {
+ throw new Zend_Locale_Math_Exception("addition overflow: $op1 + $op2 != $result", $op1, $op2, $result);
+ }
+
+ return self::round(self::normalize($result), $scale);
+ }
+
+ public static function Sub($op1, $op2, $scale = null)
+ {
+ if ($scale === null) {
+ $scale = Zend_Locale_Math_PhpMath::$defaultScale;
+ $precision = Zend_Locale_Math_PhpMath::$defaultPrecision;
+ } else {
+ $precision = pow(10, -$scale);
+ }
+
+ if (empty($op1)) {
+ $op1 = 0;
+ }
+ $op1 = self::normalize($op1);
+ $op2 = self::normalize($op2);
+ $result = $op1 - $op2;
+ if (is_infinite($result) or (abs($result + $op2 - $op1) > $precision)) {
+ throw new Zend_Locale_Math_Exception("subtraction overflow: $op1 - $op2 != $result", $op1, $op2, $result);
+ }
+
+ return self::round(self::normalize($result), $scale);
+ }
+
+ public static function Pow($op1, $op2, $scale = null)
+ {
+ if ($scale === null) {
+ $scale = Zend_Locale_Math_PhpMath::$defaultScale;
+ }
+
+ $op1 = self::normalize($op1);
+ $op2 = self::normalize($op2);
+
+ // BCMath extension doesn't use decimal part of the power
+ // Provide the same behavior
+ $op2 = ($op2 > 0) ? floor($op2) : ceil($op2);
+
+ $result = pow($op1, $op2);
+ if (is_infinite($result) or is_nan($result)) {
+ throw new Zend_Locale_Math_Exception("power overflow: $op1 ^ $op2", $op1, $op2, $result);
+ }
+
+ return self::round(self::normalize($result), $scale);
+ }
+
+ public static function Mul($op1, $op2, $scale = null)
+ {
+ if ($scale === null) {
+ $scale = Zend_Locale_Math_PhpMath::$defaultScale;
+ }
+
+ if (empty($op1)) {
+ $op1 = 0;
+ }
+ $op1 = self::normalize($op1);
+ $op2 = self::normalize($op2);
+ $result = $op1 * $op2;
+ if (is_infinite($result) or is_nan($result)) {
+ throw new Zend_Locale_Math_Exception("multiplication overflow: $op1 * $op2 != $result", $op1, $op2, $result);
+ }
+
+ return self::round(self::normalize($result), $scale);
+ }
+
+ public static function Div($op1, $op2, $scale = null)
+ {
+ if ($scale === null) {
+ $scale = Zend_Locale_Math_PhpMath::$defaultScale;
+ }
+
+ if (empty($op2)) {
+ throw new Zend_Locale_Math_Exception("can not divide by zero", $op1, $op2, null);
+ }
+ if (empty($op1)) {
+ $op1 = 0;
+ }
+ $op1 = self::normalize($op1);
+ $op2 = self::normalize($op2);
+ $result = $op1 / $op2;
+ if (is_infinite($result) or is_nan($result)) {
+ throw new Zend_Locale_Math_Exception("division overflow: $op1 / $op2 != $result", $op1, $op2, $result);
+ }
+
+ return self::round(self::normalize($result), $scale);
+ }
+
+ public static function Sqrt($op1, $scale = null)
+ {
+ if ($scale === null) {
+ $scale = Zend_Locale_Math_PhpMath::$defaultScale;
+ }
+
+ if (empty($op1)) {
+ $op1 = 0;
+ }
+ $op1 = self::normalize($op1);
+ $result = sqrt($op1);
+ if (is_nan($result)) {
+ return NULL;
+ }
+
+ return self::round(self::normalize($result), $scale);
+ }
+
+ public static function Mod($op1, $op2)
+ {
+ if (empty($op1)) {
+ $op1 = 0;
+ }
+ if (empty($op2)) {
+ return NULL;
+ }
+ $op1 = self::normalize($op1);
+ $op2 = self::normalize($op2);
+ if ((int)$op2 == 0) {
+ return NULL;
+ }
+ $result = $op1 % $op2;
+ if (is_nan($result) or (($op1 - $result) % $op2 != 0)) {
+ throw new Zend_Locale_Math_Exception("modulus calculation error: $op1 % $op2 != $result", $op1, $op2, $result);
+ }
+
+ return self::normalize($result);
+ }
+
+ public static function Comp($op1, $op2, $scale = null)
+ {
+ if ($scale === null) {
+ $scale = Zend_Locale_Math_PhpMath::$defaultScale;
+ }
+
+ if (empty($op1)) {
+ $op1 = 0;
+ }
+ $op1 = self::normalize($op1);
+ $op2 = self::normalize($op2);
+ if ($scale <> 0) {
+ $op1 = self::round($op1, $scale);
+ $op2 = self::round($op2, $scale);
+ } else {
+ $op1 = ($op1 > 0) ? floor($op1) : ceil($op1);
+ $op2 = ($op2 > 0) ? floor($op2) : ceil($op2);
+ }
+ if ($op1 > $op2) {
+ return 1;
+ } else if ($op1 < $op2) {
+ return -1;
+ }
+ return 0;
+ }
+
+ public static function Scale($scale)
+ {
+ if ($scale > 9) {
+ throw new Zend_Locale_Math_Exception("can not scale to precision $scale", $scale, null, null);
+ }
+ self::$defaultScale = $scale;
+ self::$defaultPrecision = pow(10, -$scale);
+ return true;
+ }
+}
+
+Zend_Locale_Math_PhpMath::disable(); // disable use of bcmath functions