Exemplo n.º 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;
 }
Exemplo n.º 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');
     }
 }
Exemplo n.º 3
0
 public function testBetween()
 {
     $min = '90483760434562';
     $max = '85443160876456823458976843568';
     $n = '34765017345073624875342346';
     $this->assertTrue(Number::between($n, $min, $max));
     $min = '3784659837456';
     $max = '8976345348576';
     $n = '436544';
     $this->assertFalse(Number::between($n, $min, $max));
 }
Exemplo n.º 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);
 }
Exemplo n.º 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;
 }