Type::validate()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 9
nc 4
nop 3
crap 5
1
<?php
2
/**
3
 * @package   Fuel\Validation
4
 * @version   2.0
5
 * @author    Fuel Development Team
6
 * @license   MIT License
7
 * @copyright 2010 - 2013 Fuel Development Team
8
 * @link      http://fuelphp.com
9
 */
10
11
namespace Fuel\Validation\Rule;
12
13
use Fuel\Validation\AbstractRule;
14
15
/**
16
 * Checks if a value is a typeof or instanceof parameter
17
 *
18
 * @package Fuel\Validation\Rule
19
 * @author  Fuel Development Team
20
 *
21
 * @since   2.0
22
 */
23
class Type extends AbstractRule
24
{
25
26
	/**
27
	 * Contains the rule failure message
28
	 *
29
	 * @var string
30
	 */
31
	protected $message = 'The field is not one of the given type(s).';
32
33
	/**
34
	 * Performs validation on the given value
35
	 *
36
	 * @param mixed  $value Value to validate
37
	 * @param string $field Unused by this rule
38
	 * @param array  $allFields Unused by this rule
39
	 *
40
	 * @return bool
41
	 *
42
	 * @since 2.0
43
	 */
44 12
	public function validate($value, $field = null, $allFields = null)
45
	{
46 12
		$allowedTypes = (array) $this->getParameter();
47
48 12
		foreach ($allowedTypes as $type)
49
		{
50 11
			$isFunction = 'is_'.$type;
51
52 11
			if (function_exists($isFunction) and $isFunction($value))
53
			{
54 5
				return true;
55
			}
56 8
			elseif ($value instanceof $type)
57
			{
58 8
				return true;
59
			}
60
		}
61
62 6
		return false;
63
	}
64
65
}
66