Completed
Push — master ( 10806f...3a0750 )
by Jean-Christophe
01:35
created

DbTypes::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Ubiquity\db\utils;
4
5
class DbTypes {
6
	const TYPES=["tinyint"=>0,"int"=>0,"decimal"=>0,"float"=>0,"double"=>0,"smallint"=>0,"mediumint"=>0,"bigint"=>0,
7
			"date"=>"NULL","time"=>"NULL","datetime"=>"CURRENT_TIMESTAMP","timestamp"=>"CURRENT_TIMESTAMP","year"=>"'0000'",
8
			"tinytext"=>"NULL","text"=>"NULL","mediumtext"=>"NULL","longtext"=>"NULL",
9
			"tinyblob"=>"NULL","blob"=>"NULL","mediumblob"=>"NULL","longblob"=>"NULL",
10
			"char"=>"NULL","varchar"=>"NULL","binary"=>"NULL","varbinary"=>"NULL",
11
			"enum"=>"''","set"=>"''"
12
	];
13
	const DEFAULT_TYPE="varchar(30)";
14
	
15
	protected static $typeMatch='@([\s\S]*?)((?:\((?:\d)+\))*?)$@';
16
	protected static $sizeMatch='@(?:[\s\S]*?)(?:\((\d+)\))*?$@';
17
	protected static $intMatch='@^.*?int.*?((?:\((?:\d)+\))*?)$@';
18
	protected static $stringMatch='@^.*?char|text|binary.*?((?:\((?:\d)+\))*?)$@';
19
	
20
	protected static function get_($value,$regex,$pos=1){
21
		$matches=[];
22
		if (\preg_match($regex, $value,$matches)) {
23
			if(isset($matches[$pos])){
24
				return $matches[$pos];
25
			}
26
		}
27
		return null;
28
	}
29
	
30
	public static function isInt($fieldType){
31
		return \preg_match(self::$intMatch, $fieldType);
32
	}
33
	
34
	public static function isString($fieldType){
35
		return \preg_match(self::$stringMatch, $fieldType);
36
	}
37
38
	public static function getSize($dbType){
39
		return self::get_($dbType, self::$sizeMatch);
40
	}
41
	
42
	public static function getType($dbType){
43
		return self::get_($dbType, self::$typeMatch);
44
	}
45
}
46
47