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

DbTypes   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 3
cbo 0
dl 0
loc 41
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_() 0 9 3
A isInt() 0 3 1
A isString() 0 3 1
A getSize() 0 3 1
A getType() 0 3 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