|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Supports; |
|
4
|
|
|
|
|
5
|
|
|
trait CommonHelper |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @param $page |
|
10
|
|
|
* @param $perPage |
|
11
|
|
|
* @param null $numRows |
|
12
|
|
|
* |
|
13
|
|
|
* @return array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected function getPaginationLinks($page, $perPage, $numRows = null) :array |
|
16
|
|
|
{ |
|
17
|
|
|
$numRows = isset($numRows) ? $numRows : $this->model()->numRows(); |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
$this->setPagination($page); |
|
|
|
|
|
|
20
|
|
|
$this->pagination()->setPerPage($perPage); |
|
|
|
|
|
|
21
|
|
|
$this->pagination()->setCount($numRows); |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
return $this->pagination()->getLinks(); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param $page |
|
28
|
|
|
* @param $limit |
|
29
|
|
|
* @param $uri |
|
30
|
|
|
* @param $links |
|
31
|
|
|
* |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function getPaginationViewData($page, $limit, $uri, &$links) :array |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
'limit' => $limit, |
|
38
|
|
|
'page' => $page['id'], |
|
39
|
|
|
'uri' => $uri, |
|
40
|
|
|
'links' => $links |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param array $result |
|
46
|
|
|
* @param string $type |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function validationErrors(array $result, string $type = 'alert') :void |
|
49
|
|
|
{ |
|
50
|
|
|
if ($type == 'API') { |
|
51
|
|
|
exit($this->container()->jsonResponse($this->validation()->flash($result, []))); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$data = ($type == 'value') ? $this->validation()->get($result, ['csrf_field']) |
|
|
|
|
|
|
55
|
|
|
: $this->validation()->flash($result, ['csrf_field']); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
foreach ($data as $key => $message) { |
|
58
|
|
|
$this->setSession($type, $message, $key); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $slug |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getIdFromSlug(string $slug) :string |
|
68
|
|
|
{ |
|
69
|
|
|
$slug = strip_tags($slug); |
|
70
|
|
|
|
|
71
|
|
|
return (strpos($slug, '_') !== false) ? strstr($slug, '_', true) : $slug; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $uri |
|
76
|
|
|
* |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function redirectToSelf(string $uri) |
|
80
|
|
|
{ |
|
81
|
|
|
$page = $this->validation()->sanitize($this->post('page'))->required()->run(); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
if ($this->validation()->access($page)) { |
|
|
|
|
|
|
84
|
|
|
return $this->redirect($uri . '/page/' . $page[0]); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->redirect($uri); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param $array |
|
92
|
|
|
* @param $arrayKey |
|
93
|
|
|
* |
|
94
|
|
|
* @return int|string |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function searchArrayKey($array, $arrayKey) |
|
97
|
|
|
{ |
|
98
|
|
|
foreach ($array as $key => $value) { |
|
99
|
|
|
if (stristr($key, $arrayKey) !== false) { |
|
100
|
|
|
return $key; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param $value |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function dateFormat($value) |
|
111
|
|
|
{ |
|
112
|
|
|
$date = \DateTime::createFromFormat('d.m.Y H:i', $value); |
|
113
|
|
|
$value = $date->format('Y-m-d H:i'); |
|
114
|
|
|
|
|
115
|
|
|
return $value; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param $value |
|
120
|
|
|
* |
|
121
|
|
|
* @return int |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function checkbox($value) |
|
124
|
|
|
{ |
|
125
|
|
|
return ($value) ? '1' : '0'; |
|
126
|
|
|
} |
|
127
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.