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