|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ShouldPHP |
|
4
|
|
|
* |
|
5
|
|
|
* @author Gabriel Jacinto <[email protected]> |
|
6
|
|
|
* @status dev |
|
7
|
|
|
* @link https://github.com/GabrielJMJ/ShouldPHP |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Gabrieljmj\Should\Assert\TheParameter\Have; |
|
12
|
|
|
|
|
13
|
|
|
use Gabrieljmj\Should\Assert\TheParameter\AbstractParameterAssert; |
|
14
|
|
|
use Gabrieljmj\Should\Options\TypeHinting; |
|
15
|
|
|
|
|
16
|
|
|
class AcceptOnly extends AbstractParameterAssert |
|
17
|
|
|
{ |
|
18
|
|
|
private $paramStr = [ |
|
19
|
|
|
TypeHinting::ARR => 'array', |
|
20
|
|
|
TypeHinting::CALL => 'callable', |
|
21
|
|
|
TypeHinting::VARIADIC => 'variadic', |
|
22
|
|
|
TypeHinting::INSTANCE_OF => 'instance' |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string|array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $type; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct($class, $method, $parameter, $type) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct($class, $method, $parameter); |
|
33
|
|
|
$this->type = $type; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Executes the assert |
|
38
|
|
|
* |
|
39
|
|
|
* @return boolean |
|
40
|
|
|
*/ |
|
41
|
|
|
public function execute() |
|
42
|
|
|
{ |
|
43
|
|
|
$ref = new \ReflectionClass($this->class); |
|
44
|
|
|
$method = $ref->getMethod($this->method); |
|
45
|
|
|
$params = $method->getParameters(); |
|
46
|
|
|
|
|
47
|
|
|
foreach ($params as $param) { |
|
48
|
|
|
if ($param->getName() === $this->parameter) { |
|
49
|
|
|
switch ($this->type) { |
|
50
|
|
|
case TypeHinting::ARR: |
|
51
|
|
|
return $param->isArray(); |
|
52
|
|
|
case TypeHinting::CALL: |
|
53
|
|
|
return $param->isCallable(); |
|
54
|
|
|
case TypeHinting::VARIADIC: |
|
55
|
|
|
return $param->isVariadic(); |
|
56
|
|
|
case TypeHinting::INSTANCE_OF: |
|
57
|
|
|
$class = $param->getClass(); |
|
58
|
|
|
$className = $class === null ? null : $class->getName(); |
|
59
|
|
|
return TypeHinting::$class === $className; |
|
60
|
|
|
default: |
|
61
|
|
|
throw new \Exception('The type of parameter specified is not valid: ' . $this->type); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Creates the fail message |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function createFailMessage() |
|
75
|
|
|
{ |
|
76
|
|
|
$class = is_object($this->class) ? get_class($this->class) : $this->class; |
|
77
|
|
|
|
|
78
|
|
|
$error = 'only '; |
|
79
|
|
|
if ($this->type === TypeHinting::INSTANCE_OF) { |
|
80
|
|
|
$error .= 'instances of ' . TypeHinting::$class; |
|
81
|
|
|
} else { |
|
82
|
|
|
if ($this->type == TypeHinting::VARIADIC) { |
|
83
|
|
|
$error = 'variadic'; |
|
84
|
|
|
} else { |
|
85
|
|
|
$error .= $this->paramStr[$this->type]; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return 'The parameter ' . $this->parameter . ' of method ' . $class . '::' . $this->method . ' should accept ' . $error . ', but does not'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns the assert description |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getDescription() |
|
98
|
|
|
{ |
|
99
|
|
|
return 'Tests if certain parameter accept determined value type.'; |
|
100
|
|
|
} |
|
101
|
|
|
} |