forked from 4gekkman/R4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceProvider.php
183 lines (126 loc) · 5.66 KB
/
ServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
////========================================================////
//// ////
//// Сервис-провайдер R-пакета ////
//// ////
////========================================================////
//-------------------------------------//
// Пространство имён сервис-провайдера //
//-------------------------------------//
namespace R4;
//---------------------------------//
// Подключение необходимых классов //
//---------------------------------//
use Illuminate\Support\ServiceProvider as BaseServiceProvider,
Illuminate\Contracts\Events\Dispatcher as DispatcherContract,
Illuminate\Support\Facades\Validator,
Illuminate\Support\Facades\Event;
//------------------//
// Сервис-провайдер //
//------------------//
class ServiceProvider extends BaseServiceProvider {
//------//
// Boot //
//------//
public function boot(DispatcherContract $events, \Illuminate\Contracts\Http\Kernel $kernel) {
//-------------------------------------------------//
// Регистрация библиотеки хелперов php_helpers.php //
//-------------------------------------------------//
require __DIR__ . '/php_helpers.php';
//----------------------------------------//
// Регистрация кастомных правил валидации //
//----------------------------------------//
/**
*
* r4_numpos | must be a positive integer
* r4_numnn | must be not negative positive integer
* r4_defined | Must be not undefined
* r4_true | must be true (not 1 or '1', only true)
* r4_false | must be false (not 0 or '0', only false)
* r4_min:min | number must be more or equal than min
* r4_max:max | number must be less or equal than max
* r4_between:min,max | number must be between min and max (inclusive)
*
*
*/
//===========//
// r4_numpos //
//===========//
Validator::extend('r4_numpos', function($attribute, $value, $parameters) {
return preg_match("/^[1-9]+[0-9]*$/ui", $value);
}, ":attribute must be a positive integer");
//==========//
// r4_numnn //
//==========//
Validator::extend('r4_numnn', function($attribute, $value, $parameters) {
return preg_match("/^[0-9]+$/ui", $value);
}, ":attribute must be not negative positive integer");
//============//
// r4_defined //
//============//
Validator::extend('r4_defined', function($attribute, $value, $parameters) {
return isset($value);
}, ":attribute must be not undefined");
//=========//
// r4_true //
//=========//
Validator::extend('r4_true', function($attribute, $value, $parameters) {
if($value === true) return true;
return false;
}, ":attribute must be must be true (not 1 or '1', only true)");
//==========//
// r4_false //
//==========//
Validator::extend('r4_false', function($attribute, $value, $parameters) {
if($value === false) return true;
return false;
}, ":attribute must be false (not 0 or '0', only false)");
//============//
// r4_min:min //
//============//
Validator::extend('r4_min', function($attribute, $value, $parameters) {
// 1] Если длина массива $parameters не равна 1, вернуть false
if(count($parameters) != 1) return false;
// 2] Получить min
$min = $parameters[0];
// 3] Сравнить $value с $min
if(gmp_cmp($value, $min) < 0) return false;
// 4] Вернуть true
return true;
}, ":attribute must be more or equal than min");
//============//
// r4_max:max //
//============//
Validator::extend('r4_max', function($attribute, $value, $parameters) {
// 1] Если длина массива $parameters не равна 1, вернуть false
if(count($parameters) != 1) return false;
// 2] Получить max
$max = $parameters[1];
// 3] Сравнить $value с $max
if(gmp_cmp($max, $value) < 0) return false;
// 4] Вернуть true
return true;
}, ":attribute must be less or equal than max");
//====================//
// r4_between:min,max //
//====================//
Validator::extend('r4_between', function($attribute, $value, $parameters) {
// 1] Если длина массива $parameters не равна 2, вернуть false
if(count($parameters) != 2) return false;
// 2] Получить min и max
$min = $parameters[0];
$max = $parameters[1];
// 3] Сравнить $value с $min
if(gmp_cmp($value, $min) < 0) return false;
// 4] Сравнить $value с $max
if(gmp_cmp($max, $value) < 0) return false;
// 5] Вернуть true
return true;
}, ":attribute must be between min and max (inclusive)");
}
//--------------------------------------------------//
// Register - регистрация связей в сервис-контейнер //
//--------------------------------------------------//
public function register()
{}
}