ModelParams::getType()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 3
b 0
f 0
nc 4
nop 1
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 4
rs 9.9
1
<?php
2
/**
3
 * Class for testing model params
4
 *
5
 * @file      ModelParams.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Yancharuk Alexander <alex at itvault dot info>
10
 * @copyright © 2012-2021 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
use Model\Type;
19
20
/**
21
 * Class   ModelParams
22
 *
23
 * @author Yancharuk Alexander <alex at itvault dot info>
24
 */
25
class ModelParams
26
{
27 21
	public static function getType($type)
28
	{
29 21
		$string_pattern = static::getStringPattern();
30 21
		$int_pattern    = '/.*int(?:eger)?\(\d+\).*/i';
31 21
		$float_pattern  = static::getFloatPattern();
32
33
		switch (1) {
34 21
			case preg_match($string_pattern, $type):
35 10
				return Type::STRING;
36 11
			case preg_match($int_pattern, $type):
37 5
				return Type::INT;
38 6
			case preg_match($float_pattern, $type):
39 5
				return Type::FLOAT;
40
		}
41
42 1
		throw new \RuntimeException('Unknown data type');
43
	}
44
45
	/**
46
	 * Getting string pattern
47
	 *
48
	 * @return string
49
	 */
50 21
	protected static function getStringPattern()
51
	{
52 21
		return '/(:?(?:var)?char\(\d+\)'
53 21
			. '|(?:medium)?text'
54 21
			. '|(?:enum).*'
55 21
			. '|(?:date)?time(?:stamp)?'
56 21
			. '|date'
57 21
			. '|bit\(\d+\)'
58 21
			. ')/i';
59
	}
60
61
	/**
62
	 * Getting float pattern
63
	 *
64
	 * @return string
65
	 */
66 21
	protected static function getFloatPattern()
67
	{
68 21
		return '/(?:dec(?:imal)?'
69 21
			. '|float'
70 21
			. '|real'
71 21
			. '|double'
72 21
			. ')/i';
73
	}
74
}
75