Пример #1
0
 /**
  * Determines the most appropriate int type
  * @param string $intStr String containing an integer value
  */
 public static function parse($intStr)
 {
     $int = new static(\strlen($intStr));
     if (Number::between($int, MySQL::getConstant('TINYINT_MIN'), MySQL::getConstant('TINYINT_MAX'))) {
         $int->_IntType = static::TINY_INT;
     } else {
         if (Number::between($int, MySQL::getConstant('SMALLINT_MIN'), MySQL::getConstant('SMALLINT_MAX'))) {
             $int->_IntType = static::SMALL_INT;
         } else {
             if (Number::between($int, MySQL::getConstant('MEDIUMINT_MIN'), MySQL::getConstant('MEDIUMINT_MAX'))) {
                 $int->_IntType = static::MEDIUM_INT;
             } else {
                 if (Number::between($int, MySQL::getConstant('INT_MIN'), MySQL::getConstant('INT_MAX'))) {
                     $int->_IntType = static::INT;
                 } else {
                     if (Number::between($int, MySQL::getConstant('BIGINT_MIN'), MySQL::getConstant('BIGINT_MAX'))) {
                         $int->_IntType = static::BIG_INT;
                     } else {
                         //No types can hold this
                         return null;
                     }
                 }
             }
         }
     }
     return $int;
 }
Пример #2
0
 public function setLength($len)
 {
     if (Number::between($len, MySQL::getConstant('BIT_M_MIN'), MySQL::getConstant('BIT_M_MAX'))) {
         $this->_M = $len;
     } else {
         throw new \RangeException('Bit type length invalid');
     }
 }
Пример #3
0
 /**
  * Checks whether this enum's interval value matches another
  * @param static $obj
  * @return int
  * @throws \RuntimeException
  */
 public function compareTo($obj)
 {
     if (!\is_a($obj, \get_called_class())) {
         throw new \RuntimeException('$obj is not a comparable instance');
     }
     if (\is_int($this->_Value) && \is_int($obj->_Value)) {
         return Number::compare($this->_Value, $obj->_Value);
     } else {
         if (\is_string($this->_Value) && \is_string($obj->_Value)) {
             return \strcmp($this->_Value, $obj->_Value);
         } else {
             throw new \RuntimeException('Incomparable types');
         }
     }
 }
Пример #4
0
 /**
  * Parse a PHP float/string to a MySQL decimal type
  * @link https://dev.mysql.com/doc/refman/5.6/en/precision-math-decimal-characteristics.html
  * @param mixed $object
  * @return \static
  */
 public static function parse($object)
 {
     $num = \preg_replace('/[^\\.\\d]/', '', \strval($object));
     list($m, $d) = \explode('.', $num, 2);
     $mLen = \strlen($m);
     $dLen = \strlen($d);
     if (!Number::between($mLen, MySQL::getConstant('DECIMAL_M_MIN'), MySQL::getConstant('DECIMAL_M_MAX'))) {
         return null;
     }
     if (!Number::between($dLen, MySQL::getConstant('DECIMAL_D_MIN'), MySQL::getConstant('DECIMAL_D_MAX'))) {
         return null;
     }
     if ($dLen > $mLen) {
         return null;
     }
     return new static($mLen, $dLen);
 }
Пример #5
0
 /**
  * Parse a string object to a MySQL string type
  * Prefers VARCHARs over BLOBs
  * @param mixed $object
  * @return \static
  */
 public static function parse($object)
 {
     $len = \strlen($object);
     if (Number::between($len, MySQL::getConstant('CHAR_LEN_MIN'), MySQL::getConstant('CHAR_LEN_MAX'))) {
         return new static($len, static::CHAR);
     } else {
         if (Number::between($len, MySQL::getConstant('VARCHAR_LEN_MIN'), MySQL::getConstant('VARCHAR_LEN_MAX'))) {
             return new static($len, static::VARCHAR);
         } else {
             if (Number::between($len, MySQL::getConstant('MEDIUMTEXT_LEN_MIN'), MySQL::getConstant('MEDIUMTEXT_LEN_MAX'))) {
                 return new static($len, static::MEDIUMTEXT);
             } else {
                 if (Number::between($len, MySQL::getConstant('LONGTEXT_LEN_MIN'), MySQL::getConstant('LONGTEXT_LEN_MAX'))) {
                     return new static($len, static::LONGTEXT);
                 }
             }
         }
     }
     return null;
 }
Пример #6
0
 /**
  * Converts the set to an integer string. The difference between this
  * and toInt() is that this method uses bcmath functions to handle
  * large bit sets that would fail to properly be converted to a PHP
  * integer if toInt() is used.
  * @return string
  */
 public function toLongInt()
 {
     $total = '0';
     for ($i = 0, $l = $this->count(); $i < $l; ++$i) {
         if ($this->_List[$i]) {
             $total = Number::add($total, Number::pow('2', (string) $i));
         }
     }
     return $total;
 }
Пример #7
0
 /**
  * Inverts the bits of this enum
  * @return \static
  */
 public function invert()
 {
     $this->_Value = Number::invertBits($this->_Value);
     return $this;
 }
Пример #8
0
 /**
  * Computes the population standard deviation for the numbers
  * in this list
  * @return string
  */
 public function stdDev()
 {
     Number::setScale(static::NUMERIC_FUNCTIONS_SCALE);
     $mean = $this->average();
     $squares = new static();
     foreach ($this->_Arr as $num) {
         $squares->add(Number::pow(Number::sub(\strval($num), $mean), '2'));
     }
     return Number::sqrt($squares->average());
 }
Пример #9
0
 public function trimZeros()
 {
     $this->assertEquals('0', Number::sub('1', '1'));
     $this->assertEquals('0.01', Number::add('0.00000', '0.01'));
 }