Completed
Push — master ( 8d2cfe...005c3c )
by Alexander
03:15
created

ModelParams::getStringPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 9
ccs 2
cts 2
cp 1
crap 1
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class for testing model params
4
 *
5
 * @file      ModelParams.php
6
 *
7
 * PHP version 7.0+
8
 *
9
 * @author    Yancharuk Alexander <alex at itvault dot info>
10
 * @copyright © 2012-2018 Alexander Yancharuk
11
 * @date      2015-12-07 17:12
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Tools;
17
18
/**
19
 * Class   ModelParams
20
 *
21
 * @author Yancharuk Alexander <alex at itvault dot info>
22
 */
23
class ModelParams
24
{
25 42
	public static function getType($type)
26
	{
27 42
		$string_pattern = static::getStringPattern();
28 42
		$int_pattern    = '/.*int(?:eger)?\(\d+\).*/i';
29
		$float_pattern  = '/(?:dec(?:imal)?'
30
			. '|float'
31
			. '|real'
32
			. '|double'
33 42
			. ')/i';
34
35
		switch (1) {
36 42
			case preg_match($string_pattern, $type):
37 20
				$result = 'string';
38 20
				break;
39 22
			case preg_match($int_pattern, $type):
40 10
				$result = 'int';
41 10
				break;
42 12
			case preg_match($float_pattern, $type):
43 10
				$result = 'float';
44 10
				break;
45
			default:
46 2
				throw new \RuntimeException('Unknown data type');
47
		}
48
49 40
		return $result;
50
	}
51
52
	/**
53
	 * Get string pattern
54
	 *
55
	 * @return string
56
	 */
57 40
	protected static function getStringPattern()
58
	{
59
		return '/(:?(?:var)?char\(\d+\)'
60
			. '|(?:medium)?text'
61
			. '|(?:enum).*'
62
			. '|(?:date)?time(?:stamp)?'
63
			. '|date'
64
			. '|bit\(\d+\)'
65 40
			. ')/i';
66
	}
67
}
68