Passed
Push — master ( a357e7...195a07 )
by Anton
04:22 queued 01:11
created

Validator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 38
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C get() 0 31 11
1
<?php
2
3
/**
4
 * @package Framework\Dataset
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2016, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Dataset {
11
12
	class Validator {
13
14
		/**
15
		 * Get a validator for a given value type
16
		 */
17
18
		public static function get($default) : callable {
19
20
			# Check basic types
21
22
			if (is_string   ($default)) return function(string      $value) { return $value; };
23
24
			if (is_bool     ($default)) return function(bool        $value) { return $value; };
25
26
			if (is_int      ($default)) return function(int         $value) { return $value; };
27
28
			if (is_float    ($default)) return function(float       $value) { return $value; };
29
30
			if (is_array    ($default)) return function(array       $value) { return $value; };
31
32
			if (is_callable ($default)) return function(callable    $value) { return $value; };
33
34
			# Check object type
35
36
			if (is_object($default)) return function($value) use($default) {
37
38
				return (is_a($value, get_class($default)) ? $value : null);
39
			};
40
41
			# Check resource type
42
43
			if (is_resource($default)) return function($value) { return (is_resource($value) ? $value : null); };
44
45
			# ------------------------
46
47
			return function() { return null; };
48
		}
49
	}
50
}
51