1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\HeyMan; |
4
|
|
|
|
5
|
|
|
class ResponderFactory |
6
|
|
|
{ |
7
|
|
|
private $chain; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* ResponderFactory constructor. |
11
|
|
|
* |
12
|
|
|
* @param \Imanghafoori\HeyMan\Chain $chain |
13
|
|
|
*/ |
14
|
56 |
|
public function __construct(Chain $chain) |
15
|
|
|
{ |
16
|
56 |
|
$this->chain = $chain; |
17
|
56 |
|
} |
18
|
|
|
|
19
|
56 |
|
public function make() |
20
|
|
|
{ |
21
|
56 |
|
if ($this->chain->abort) { |
22
|
1 |
|
return $this->abortCallback($this->chain->abort); |
23
|
56 |
|
} elseif ($this->chain->exception) { |
|
|
|
|
24
|
53 |
|
return $this->exceptionCallback($this->chain->exception); |
25
|
4 |
|
} elseif ($this->chain->response) { |
|
|
|
|
26
|
1 |
|
return $this->responseCallback($this->chain->response); |
27
|
3 |
|
} elseif ($this->chain->redirect) { |
|
|
|
|
28
|
3 |
|
return $this->redirectCallback($this->chain->redirect); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
public function abortCallback($abort) |
33
|
|
|
{ |
34
|
|
|
$responder = function () use ($abort) { |
35
|
1 |
|
abort(...$abort); |
|
|
|
|
36
|
1 |
|
}; |
37
|
|
|
|
38
|
1 |
|
return $responder; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $e |
43
|
|
|
* @param $cb |
44
|
|
|
* |
45
|
|
|
* @return \Closure |
46
|
|
|
*/ |
47
|
53 |
|
public function exceptionCallback($e): \Closure |
48
|
|
|
{ |
49
|
|
|
$responder = function () use ($e) { |
50
|
37 |
|
$exClass = $e['class']; |
51
|
37 |
|
throw new $exClass($e['message']); |
52
|
53 |
|
}; |
53
|
|
|
|
54
|
53 |
|
return $responder; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $resp |
59
|
|
|
* @param $cb |
60
|
|
|
* |
61
|
|
|
* @return \Closure |
62
|
|
|
*/ |
63
|
1 |
|
public function responseCallback($resp): \Closure |
64
|
|
|
{ |
65
|
|
|
$responder = function () use ($resp) { |
66
|
1 |
|
$respObj = response(); |
67
|
1 |
|
foreach ($resp as $call) { |
68
|
1 |
|
list($method, $args) = $call; |
69
|
1 |
|
$respObj = $respObj->{$method}(...$args); |
70
|
|
|
} |
71
|
1 |
|
respondWith($respObj); |
72
|
1 |
|
}; |
73
|
|
|
|
74
|
1 |
|
return $responder; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
public function redirectCallback($resp): \Closure |
78
|
|
|
{ |
79
|
|
|
$responder = function () use ($resp) { |
80
|
3 |
|
$respObj = redirect(); |
81
|
3 |
|
foreach ($resp as $call) { |
82
|
3 |
|
list($method, $args) = $call; |
83
|
3 |
|
$respObj = $respObj->{$method}(...$args); |
84
|
|
|
} |
85
|
3 |
|
respondWith($respObj); |
86
|
3 |
|
}; |
87
|
|
|
|
88
|
3 |
|
return $responder; |
89
|
|
|
} |
90
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.