1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill; |
4
|
|
|
|
5
|
|
|
use A17\Twill\Repositories\BlockRepository; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
class ValidationServiceProvider extends ServiceProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Registers the package additional validation rules. |
14
|
|
|
* |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
59 |
|
public function boot() |
18
|
|
|
{ |
19
|
|
|
Validator::extend('absolute_or_relative_url', function ($attribute, $value, $parameters, $validator) { |
|
|
|
|
20
|
1 |
|
return Str::startsWith($value, '/') || Validator::make([$attribute => $value], [$attribute => 'url'])->passes(); |
21
|
59 |
|
}, 'The :attribute should be a valid url (absolute or relative)'); |
22
|
|
|
|
23
|
|
|
Validator::extend('relative_or_secure_url', function ($attribute, $value, $parameters) { |
|
|
|
|
24
|
1 |
|
return Str::startsWith($value, '/') || filter_var($value, FILTER_VALIDATE_URL) !== false && Str::startsWith($value, 'https'); |
25
|
59 |
|
}, 'The :attribute should be a valid url (relative or https)'); |
26
|
|
|
|
27
|
|
|
Validator::extend('web_color', function ($attribute, $value, $parameters, $validator) { |
|
|
|
|
28
|
1 |
|
return preg_match('/^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i', $value); |
29
|
59 |
|
}); |
30
|
|
|
|
31
|
|
|
Validator::extend('phone_number', function ($attribute, $value, $parameters) { |
|
|
|
|
32
|
1 |
|
return preg_match("/^[+]?[0-9\-\ ]*$/", $value); |
33
|
59 |
|
}); |
34
|
|
|
|
35
|
|
|
Validator::extend('validBlocks', function ($attribute, $value, $parameters, $validator) { |
36
|
|
|
$blockMessages = []; |
37
|
|
|
|
38
|
|
|
foreach ($value as $block) { |
39
|
|
|
$cmsBlock = $this->app->make(BlockRepository::class)->buildFromCmsArray($block, false); |
40
|
|
|
|
41
|
|
|
$rules = config('twill.block_editor.blocks.' . $cmsBlock['type'] . '.rules') ?? []; |
42
|
|
|
|
43
|
|
|
unset($cmsBlock['content']); |
44
|
|
|
|
45
|
|
|
$blockValidator = Validator::make(array_merge($block['content'], $cmsBlock), $rules); |
46
|
|
|
|
47
|
|
|
if (!$blockValidator->passes()) { |
48
|
|
|
foreach ($blockValidator->errors()->all() as $error) { |
49
|
|
|
$blockMessages[] = $error; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!empty($blockMessages ?? [])) { |
54
|
|
|
array_unshift($blockMessages, 'This block has validation issues:'); |
55
|
|
|
$validator->errors()->add('block.' . $block['id'], join('<br>', $blockMessages)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$blockMessages = []; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return true; |
62
|
59 |
|
}); |
63
|
59 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
59 |
|
public function register() |
69
|
|
|
{ |
70
|
|
|
|
71
|
59 |
|
} |
72
|
|
|
} |
73
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.