Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
14.29% |
1 / 7 |
CRAP | |
68.75% |
22 / 32 |
AdservingCsvRule | |
0.00% |
0 / 1 |
|
14.29% |
1 / 7 |
32.21 | |
68.75% |
22 / 32 |
__construct | |
0.00% |
0 / 1 |
5.50 | |
54.55% |
6 / 11 |
|||
passes | |
0.00% |
0 / 1 |
4.03 | |
87.50% |
7 / 8 |
|||
message | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
getColumns | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
validateLat | |
0.00% |
0 / 1 |
4.59 | |
66.67% |
2 / 3 |
|||
validateLon | |
0.00% |
0 / 1 |
4.59 | |
66.67% |
2 / 3 |
|||
validateUrl | |
0.00% |
0 / 1 |
2.15 | |
66.67% |
2 / 3 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\Adserving\Rules; |
4 | |
5 | |
6 | use Illuminate\Contracts\Validation\Rule; |
7 | use Qmp\Laravel\ToolsLaravel\FileReader\FileReader; |
8 | |
9 | class AdservingCsvRule implements Rule |
10 | { |
11 | private $headers = ['lat', 'lon', 'url']; |
12 | private $delimiter = ','; |
13 | private $errors = []; |
14 | |
15 | public function __construct(string $delimiter) |
16 | { |
17 | switch ($delimiter) { |
18 | case 'coma' : |
19 | $this->delimiter = ','; |
20 | break; |
21 | case 'semi-colon' : |
22 | $this->delimiter = ';'; |
23 | break; |
24 | case 'tab' : |
25 | $this->delimiter = "\t"; |
26 | break; |
27 | } |
28 | } |
29 | |
30 | public function passes($attribute, $file) |
31 | { |
32 | $columns = $this->getColumns($file->getRealPath()); |
33 | if(count($columns) < 4) { return false; } |
34 | |
35 | foreach ($this->headers as $key => $value) { |
36 | $method = 'validate' . ucfirst($value); |
37 | if(!method_exists($this, $method)) { |
38 | throw new \Exception($method . ' not exists ' . 'in ' . get_called_class()); |
39 | } |
40 | |
41 | $this->$method(ucfirst($value), $columns[$key]); |
42 | } |
43 | |
44 | return empty($this->errors); |
45 | } |
46 | |
47 | public function message() |
48 | { |
49 | return ':attribute is not valid. ' . implode(' ', $this->errors); |
50 | } |
51 | |
52 | private function getColumns($filename) |
53 | { |
54 | $fileReader = new FileReader($filename); |
55 | $row = $fileReader->rows(1); |
56 | return str_getcsv(trim($row[0]), $this->delimiter); |
57 | } |
58 | |
59 | private function validateLat($column, $value) |
60 | { |
61 | if(!is_numeric($value) || $value < -90 || $value > 90) { |
62 | $this->errors[] = $column . ' is not valid Lat.'; |
63 | } |
64 | } |
65 | |
66 | private function validateLon($column, $value) |
67 | { |
68 | if(!is_numeric($value) || $value < -180 || $value > 180) { |
69 | $this->errors[] = $column . ' is not valid Lon.'; |
70 | } |
71 | } |
72 | |
73 | private function validateUrl($column, $value) |
74 | { |
75 | if(!filter_var($value, FILTER_VALIDATE_URL)) { |
76 | $this->errors[] = $column . ' is not valid url.'; |
77 | } |
78 | } |
79 | |
80 | } |