DboSource::length PHP Method

length() public method

Gets the length of a database-native column description, or null if no length
public length ( string $real ) : mixed
$real string Real database-layer column type (i.e. "varchar(255)")
return mixed An integer or string representing the length of the column, or null for unknown length.
    public function length($real)
    {
        if (!preg_match_all('/([\\w\\s]+)(?:\\((\\d+)(?:,(\\d+))?\\))?(\\sunsigned)?(\\szerofill)?/', $real, $result)) {
            $col = str_replace(array(')', 'unsigned'), '', $real);
            $limit = null;
            if (strpos($col, '(') !== false) {
                list($col, $limit) = explode('(', $col);
            }
            if ($limit !== null) {
                return intval($limit);
            }
            return null;
        }
        $types = array('int' => 1, 'tinyint' => 1, 'smallint' => 1, 'mediumint' => 1, 'integer' => 1, 'bigint' => 1);
        list($real, $type, $length, $offset, $sign) = $result;
        $typeArr = $type;
        $type = $type[0];
        $length = $length[0];
        $offset = $offset[0];
        $isFloat = in_array($type, array('dec', 'decimal', 'float', 'numeric', 'double'));
        if ($isFloat && $offset) {
            return $length . ',' . $offset;
        }
        if ($real[0] == $type && count($real) === 1) {
            return null;
        }
        if (isset($types[$type])) {
            $length += $types[$type];
            if (!empty($sign)) {
                $length--;
            }
        } elseif (in_array($type, array('enum', 'set'))) {
            $length = 0;
            foreach ($typeArr as $key => $enumValue) {
                if ($key === 0) {
                    continue;
                }
                $tmpLength = strlen($enumValue);
                if ($tmpLength > $length) {
                    $length = $tmpLength;
                }
            }
        }
        return intval($length);
    }

Usage Example

 /**
  * testLength method
  *
  * @return void
  */
 public function testLength()
 {
     $result = $this->Dbo->length('varchar(255)');
     $expected = 255;
     $this->assertSame($expected, $result);
     $result = $this->Dbo->length('int(11)');
     $expected = 11;
     $this->assertSame($expected, $result);
     $result = $this->Dbo->length('float(5,3)');
     $expected = '5,3';
     $this->assertSame($expected, $result);
     $result = $this->Dbo->length('decimal(5,2)');
     $expected = '5,2';
     $this->assertSame($expected, $result);
     $result = $this->Dbo->length("enum('test','me','now')");
     $expected = 4;
     $this->assertSame($expected, $result);
     $result = $this->Dbo->length("set('a','b','cd')");
     $expected = 2;
     $this->assertSame($expected, $result);
     $result = $this->Dbo->length(false);
     $this->assertTrue($result === null);
     $result = $this->Dbo->length('datetime');
     $expected = null;
     $this->assertSame($expected, $result);
     $result = $this->Dbo->length('text');
     $expected = null;
     $this->assertSame($expected, $result);
 }
All Usage Examples Of DboSource::length
DboSource