Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
15 / 15
GeolocValidator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
15 / 15
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 validate
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 inputValidate
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 getValidator
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
1<?php
2
3namespace Qmp\Laravel\Geolocation\Services\Validations;
4
5use Illuminate\Support\Facades\Validator;
6
7class GeolocValidator
8{
9    protected $rules = [
10        'ip' => \Qmp\Laravel\Geolocation\Services\Validations\Rules\IpRule::class,
11    ];
12
13    protected $input;
14
15    protected $value;
16
17    /**
18     * GeolocValidator constructor.
19     *
20     * @param $input
21     * @param $value
22     */
23    public function __construct($input, $value)
24    {
25        $this->input = $input;
26        $this->value = $value;
27
28        $this->rules = collect($this->rules);
29    }
30
31    /**
32     * validate geolocation request
33     *
34     * @return bool
35     * @throws \Exception
36     */
37    public function validate()
38    {
39        $this->inputValidate();
40
41        $validator = $this->getValidator();
42
43        if($validator->fails()) {
44            throw new \Exception(implode(' ', $validator->errors()->get('value')));
45        }
46
47        return true;
48    }
49
50    /**
51     *  Validate input parameter
52     *
53     * @throws \Exception
54     */
55    private function inputValidate()
56    {
57        if(!$this->rules->has($this->input)) {
58            throw new \Exception('Invalid input parameter.');
59        }
60    }
61
62    /**
63     *  Create Laravel Validator
64     *
65     * @return \Illuminate\Contracts\Validation\Validator
66     */
67    private function getValidator()
68    {
69        $class = $this->rules->get($this->input);
70        return Validator::make(request()->all(), [
71            'value' => new $class()
72        ]);
73    }
74
75}