dimtrovich /
validation
| 1 | <?php |
||||
| 2 | |||||
| 3 | /** |
||||
| 4 | * This file is part of Dimtrovich/Validation. |
||||
| 5 | * |
||||
| 6 | * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]> |
||||
| 7 | * |
||||
| 8 | * For the full copyright and license information, please view |
||||
| 9 | * the LICENSE file that was distributed with this source code. |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | namespace Dimtrovich\Validation\Rules; |
||||
| 13 | |||||
| 14 | class Imei extends AbstractRule |
||||
| 15 | { |
||||
| 16 | /** |
||||
| 17 | * Check if a given value is a valid IMEI. |
||||
| 18 | * |
||||
| 19 | * @credit <a href="https://github.com/milwad-dev/laravel-validate">milwad/laravel-validate - Milwad\LaravelValidate\Rules\ValidImei</a> |
||||
| 20 | * |
||||
| 21 | * @param mixed $value |
||||
| 22 | */ |
||||
| 23 | public function check($value): bool |
||||
| 24 | { |
||||
| 25 | 2 | $imei = $value; |
|||
| 26 | |||||
| 27 | if (strlen($imei) !== 15 || ! ctype_digit($imei)) { |
||||
| 28 | 2 | return false; |
|||
| 29 | } |
||||
| 30 | |||||
| 31 | 2 | $digits = str_split($imei); // Get digits |
|||
| 32 | 2 | $imei_last = array_pop($digits); // Remove last digit, and store it |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 33 | 2 | $log = []; |
|||
| 34 | |||||
| 35 | foreach ($digits as $key => $n) { |
||||
| 36 | if ($key & 1) { |
||||
| 37 | 2 | $double = str_split($n * 2); // Get double digits |
|||
| 38 | 2 | $n = array_sum($double); // Sum double digits |
|||
|
0 ignored issues
–
show
It seems like
$double can also be of type true; however, parameter $array of array_sum() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 39 | } |
||||
| 40 | |||||
| 41 | 2 | $log[] = $n; // Append log |
|||
| 42 | } |
||||
| 43 | 2 | $sum = array_sum($log) * 9; // Sum log & multiply by 9 |
|||
| 44 | |||||
| 45 | 2 | return substr($sum, -1) === $imei_last; |
|||
| 46 | } |
||||
| 47 | } |
||||
| 48 |