|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Web\Supports; |
|
4
|
|
|
|
|
5
|
|
|
use Rudra\RouterException; |
|
6
|
|
|
|
|
7
|
|
|
trait HttpErrors |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
public function handle404($data, string $type = 'db', array $page = []) |
|
11
|
|
|
{ |
|
12
|
|
|
if ($type == 'db') { |
|
13
|
|
|
if (count($data) < 1 or !$data) { |
|
|
|
|
|
|
14
|
|
|
throw new RouterException('404'); |
|
15
|
|
|
} |
|
16
|
|
|
} elseif ($type == 'pagination') { |
|
17
|
|
|
if ($page['id'] > count($data)) { |
|
18
|
|
|
throw new RouterException('404'); |
|
19
|
|
|
} |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function handleApi404($data, string $type = 'db', array $page = []) |
|
24
|
|
|
{ |
|
25
|
|
|
if ($type == 'db') { |
|
26
|
|
|
if (count($data) < 1 or !$data) { |
|
|
|
|
|
|
27
|
|
|
$this->jsonResponse(['status' => 'Not Found']); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
} elseif ($type == 'pagination') { |
|
30
|
|
|
if ($page['id'] > count($data)) { |
|
31
|
|
|
$this->jsonResponse(['status' => 'Not Found']); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
View Code Duplication |
public function error404() |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
$this->redirect()->responseCode('404'); |
|
39
|
|
|
|
|
40
|
|
|
return $this->twig('errors/404.html.twig', [ |
|
41
|
|
|
'title' => '404 Page Not Found :: ' . $this->data('title'), |
|
42
|
|
|
]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
View Code Duplication |
public function error503() |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$this->redirect()->responseCode('503'); |
|
48
|
|
|
|
|
49
|
|
|
return $this->twig('errors/503.html.twig', [ |
|
50
|
|
|
'title' => '503 Service Unavailable :: ' . $this->data('title'), |
|
51
|
|
|
]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
View Code Duplication |
public function error500() |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
$this->redirect()->responseCode('503'); |
|
57
|
|
|
|
|
58
|
|
|
return $this->twig('errors/503.html.twig', [ |
|
59
|
|
|
'title' => '503 Service Unavailable :: ' . $this->data('title'), |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param null $target |
|
65
|
|
|
* |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
|
|
public abstract function redirect($target = null); |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $template |
|
72
|
|
|
* @param array $params |
|
73
|
|
|
*/ |
|
74
|
|
|
public abstract function twig(string $template, array $params = []): void; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $key |
|
78
|
|
|
* |
|
79
|
|
|
* @return string|array |
|
80
|
|
|
*/ |
|
81
|
|
|
public abstract function data(string $key = null); |
|
82
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.