Completed
Push — master ( 8ba526...6ddce2 )
by Marcin
06:07
created

Regon::isValid()   C

Complexity

Conditions 8
Paths 26

Size

Total Lines 36
Code Lines 21

Duplication

Lines 3
Ratio 8.33 %

Code Coverage

Tests 16
CRAP Score 8.2518

Importance

Changes 0
Metric Value
dl 3
loc 36
ccs 16
cts 19
cp 0.8421
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 21
nc 26
nop 2
crap 8.2518
1
<?php
2
/**
3
 * Validator
4
 *
5
 * Copyright (c) 2017 pudelek.org.pl
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For the full copyright and license information, please view source file
10
 * that is bundled with this package in the file LICENSE
11
 *
12
 * @author  Marcin Pudełek <[email protected]>
13
 */
14
15
namespace mrcnpdlk\Validator\Types;
16
17
use mrcnpdlk\Validator\Exception;
18
use mrcnpdlk\Validator\TypeInterface;
19
20
class Regon extends TypeAbstract implements TypeInterface
21
{
22 2
    public static function isValid($checkedValue, bool $asEx = false): bool
23
    {
24
        try {
25 2
            static::isValidType($checkedValue, static::TYPE_STRING, true);
26
27 2
            if (preg_match('/^[0-9]{9}$/', $checkedValue) || preg_match('/^[0-9]{14}$/', $checkedValue)) {
28 2
                if (strlen($checkedValue) == 9) {
29 2
                    $weights = [8, 9, 2, 3, 4, 5, 6, 7]; //wagi stosowane dla REGONów 9-znakowych
30
                } else {
31
                    $weights = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8]; //wagi stosowane dla REGONów 14-znakowych
32
                }
33 2
                $sum          = 0;
34 2
                $countWeights = count($weights);
35 2 View Code Duplication
                for ($i = 0; $i < $countWeights; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36 2
                    $sum += $weights[$i] * intval($checkedValue[$i]);
37
                }
38 2
                $checksum = ($sum % 11) % 10;
39
40 2
                if ($checksum !== intval($checkedValue[$countWeights])) {
41
                    //jezeli suma kontrolna nie jest rowna ostatniej cyfrze w numerze REGON to numerek jest błędny
42 1
                    throw new \Exception("Checksum Error", 1);
43
                }
44
45 1
                return true;
46
            }
47
48
            throw new \Exception(sprintf("Regexp error"), 1);
49
50 1
        } catch (\Exception $e) {
51 1
            if ($asEx) {
52 1
                throw new Exception(sprintf("Invalid REGON number [%s], reason: %s", $checkedValue, $e->getMessage()));
53
            }
54
55
            return false;
56
        }
57
    }
58
59 2
    public static function clean($checkedValue)
60
    {
61 2
        static::isValidType($checkedValue, static::TYPE_STRING, true);
62
63 2
        return str_pad(preg_replace('/[\s]/', "", $checkedValue), 9, '0', \STR_PAD_LEFT);
64
    }
65
}
66