• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-helpers / 8376172915

21 Mar 2024 01:57PM UTC coverage: 73.529% (+0.2%) from 73.367%
8376172915

Pull #110

github

web-flow
Merge fa471db58 into 298301db7
Pull Request #110: 77 Implement 'list_exists' validation rule

10 of 11 new or added lines in 1 file covered. (90.91%)

1 existing line in 1 file now uncovered.

875 of 1190 relevant lines covered (73.53%)

6.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/src/HelpersServiceProvider.php
1
<?php
2

3
namespace RonasIT\Support;
4

5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\ServiceProvider;
8
use Illuminate\Support\Facades\Validator;
9
use InvalidArgumentException;
10
use Maatwebsite\Excel\ExcelServiceProvider;
11
use RonasIT\Support\Middleware\SecurityMiddleware;
12
use Illuminate\Support\Arr;
13

14
class HelpersServiceProvider extends ServiceProvider
15
{
16
    public function boot()
17
    {
18
        $router = $this->app['router'];
144✔
19

20
        $router->prependMiddlewareToGroup('web', SecurityMiddleware::class);
144✔
21
        $router->prependMiddlewareToGroup('api', SecurityMiddleware::class);
144✔
22

23
        Validator::extend('unique_except_of_authorized_user', function ($attribute, $value, $parameters = []) {
144✔
24
            $table = Arr::get($parameters, 0, 'users');
4✔
25
            $keyField = Arr::get($parameters, 1, 'id');
4✔
26
            $userId = Auth::id();
4✔
27

28
            $result = DB::table($table)
4✔
29
                ->where($keyField, '<>', $userId)
4✔
30
                ->whereIn($attribute, Arr::flatten((array) $value))
4✔
31
                ->exists();
4✔
32

33
            return !$result;
4✔
34
        });
144✔
35

36
        Validator::extend('list_exists', function ($attribute, $value, $parameters) {
144✔
37

38
            if (count($parameters) < 1) {
2✔
NEW
UNCOV
39
                throw new InvalidArgumentException("Validation rule `list_exists` requires at least 1 parameters.");
×
40
            }
41

42
            $table = Arr::get($parameters, 0);
2✔
43
            $keyField = Arr::get($parameters, 1, 'id');
2✔
44

45
            if (!empty(Arr::get($parameters, 2))) {
2✔
46
                $value = collect($value)->pluck(Arr::get($parameters, 2));
1✔
47
            }
48

49
            return DB::table($table)
2✔
50
                ->whereIn($keyField, $value)
2✔
51
                ->exists();
2✔
52
        });
144✔
53

54
        app(ExcelServiceProvider::class, ['app' => app()])->boot();
144✔
55

56
        $this->loadViewsFrom(__DIR__ . '/Stubs', 'ronasit');
144✔
57
    }
58

59
    public function register()
60
    {
61
        app(ExcelServiceProvider::class, ['app' => app()])->register();
144✔
62
    }
63
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc