Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
4 / 6
CRAP
60.00% covered (warning)
60.00%
9 / 15
ListDedupRule
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
4 / 6
21.22
60.00% covered (warning)
60.00%
9 / 15
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 passes
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 message
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 validateMd5
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
3 / 3
 validateSha1
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 3
 validateSha256
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 3
1<?php
2
3namespace Qmp\Laravel\Deduplication\Rules;
4
5
6use Illuminate\Contracts\Validation\Rule;
7
8class ListDedupRule implements Rule
9{
10    private $hashType;
11
12    private $errors = '';
13
14    /**
15     * ListDedupRule constructor.
16     * @param string $hashType
17     */
18    public function __construct(string $hashType)
19    {
20        $this->hashType = $hashType;
21    }
22
23    /**
24     * @param string $attribute
25     * @param mixed $line
26     * @return bool
27     */
28    public function passes($attribute, $line)
29    {
30        $method = 'validate' . ucfirst($this->hashType);
31        $this->$method($line);
32
33        return empty($this->errors);
34    }
35
36    /**
37     * @return array|string
38     */
39    public function message()
40    {
41        return $this->errors;
42    }
43
44    /**
45     * @param $value
46     */
47    private function validateMd5($value)
48    {
49        if(!preg_match('/^[a-fA-F0-9]{32}$/', $value) && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
50            $this->errors = 'Not md5 values.';
51        }
52    }
53
54    /**
55     * @param $value
56     */
57    private function validateSha1($value)
58    {
59        if(!preg_match('/^[a-fA-F0-9]{40}$/', $value) && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
60            $this->errors = 'Not sha1 values.';
61        }
62    }
63
64    /**
65     * @param $value
66     */
67    private function validateSha256($value)
68    {
69        if(!preg_match('/^[a-fA-F0-9]{64}$/', $value) && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
70            $this->errors = 'Not sha256 values.';
71        }
72    }
73}