Completed
Push — master ( eb3c6f...097036 )
by Alexander
13:10 queued 09:24
created

ModelParams::getFloatPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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