示例#1
0
 /**
  * @param string $fieldType
  * @throws \Dive\Validation\ValidationException
  * @return \Dive\Validation\FieldValidator\FieldValidatorInterface
  */
 public function getDataTypeValidator($fieldType)
 {
     $validator = $this->dataTypeMapper->getOrmTypeInstance($fieldType);
     if ($validator) {
         return $validator;
     }
     throw new ValidationException("No orm data type defined for field type '{$fieldType}'!");
 }
示例#2
0
文件: Platform.php 项目: sigma-z/dive
 /**
  * gets data type
  *
  * @param  array $definition
  * @return string
  * @throws \LogicException
  */
 protected function getDataType(array $definition)
 {
     if (!isset($definition['type'])) {
         throw new \LogicException('Missing type in definition');
     }
     $length = isset($definition['length']) ? $definition['length'] : null;
     return $this->dataTypeMapper->getMappedDataType($definition['type'], $length);
 }
示例#3
0
 /**
  * @dataProvider provideGetMappedDataType
  */
 public function testGetMappedDataType($length, $ormType, $expected)
 {
     $actual = $this->dataTypeMapper->getMappedDataType($ormType, $length);
     $this->assertEquals($expected, $actual);
 }
示例#4
0
 /**
  * parses type definition
  *
  * @param string $type
  * @throws \Dive\Schema\SchemaException
  * @return array
  */
 protected function parseDbType($type)
 {
     $definition = array();
     $dataType = '';
     $length = null;
     $values = null;
     $match = array();
     // parsing type like varchar(32) OR char(2)
     if (preg_match('/^(\\w+)\\((\\d+)\\)/', $type, $match)) {
         $dataType = $match[1];
         $length = $match[2];
     } else {
         if (preg_match('/^(\\w+)\\((\\d+),(\\d+)\\)/', $type, $match)) {
             $dataType = $match[1];
             $length = $match[2] + 1;
             $definition['scale'] = $match[3];
         } else {
             if (preg_match('/^\\w+/', $type, $match)) {
                 $dataType = $match[0];
             }
         }
     }
     // parsing attributes like UNSIGNED, ZEROFILL, AND BINARY
     if (isset($match[0])) {
         $attributesString = strtolower(substr($type, strlen($match[0])));
         $attributesString = trim($attributesString);
         if (!empty($attributesString)) {
             $attributes = explode(' ', trim($attributesString));
             foreach ($attributes as $attr) {
                 $definition[$attr] = true;
             }
         }
     }
     $dataType = strtolower($dataType);
     switch ($dataType) {
         case 'time':
             $length = 5;
             break;
         case 'year':
             $length = 4;
             break;
         case 'enum':
             if (preg_match('/^enum\\((.+)\\)/i', $type, $match)) {
                 $values = self::parseInlineValues($match[1]);
             }
             break;
         case 'set':
             if (preg_match('/^set\\((.+)\\)/i', $type, $match)) {
                 $values = self::parseInlineValues($match[1]);
             }
             break;
     }
     if (!$this->dataTypeMapper->hasDataType($dataType)) {
         throw new SchemaException("Data type {$dataType} is not defined! (Found in string {$type})");
     }
     $definition['type'] = $this->dataTypeMapper->getMappedOrmType($dataType);
     // get length by longest value entry
     if (!empty($values)) {
         foreach ($values as $value) {
             $valueStringLength = strlen($value);
             if ($length < $valueStringLength) {
                 $length = $valueStringLength;
             }
         }
         $definition['values'] = $values;
     }
     if ($length !== null) {
         $definition['length'] = (int) $length;
     }
     ksort($definition);
     return $definition;
 }