Completed
Push — master ( 43dc73...0c99ca )
by Roberto
08:10 queued 05:22
created

Barcode1DAnalysis::validate()   C

Complexity

Conditions 11
Paths 21

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
rs 5.2653
cc 11
eloc 28
nc 21
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Posprint\Printers\Barcodes;
4
5
class Barcode1DAnalysis
6
{
7
    protected static $barcode1D = [
8
        'EAN13' => ['len' => '12', 'oddeven' => '', 'code' => '', 'regex' => '/[^0-9]/'],
9
        'EAN8' => ['len' => '7',  'oddeven' => '', 'code' => '', 'regex' => '/[^0-9]/'],
10
        'S25' => ['len' => '1-255', 'oddeven' => 'even', 'code' => '', 'regex' => '/[^0-9]/'],
11
        'I25' => ['len' => '1-255', 'oddeven' => 'even', 'code' => '', 'regex' => '/[^0-9]/'],
12
        'CODE128' => ['len' => '2-255', 'oddeven' => '', 'code' => 'ASCII', 'regex' => ''],
13
        'CODE39' => ['len' => '1-255', 'oddeven' => '', 'code' => '', 'regex' => '/[^0-9A-Z-,.%\/$ +]/'],
14
        'CODE93' => ['len' => '1-255', 'oddeven' => '', 'code' => '', 'regex' => '/[^0-9A-Z-,.%\/$ +]/'],
15
        'UPC_A' => ['len' => '11', 'oddeven' => '', 'code' => '', 'regex' => '/[^0-9]/'],
16
        'UPC-E' => ['len' => '12', 'oddeven' => '', 'code' => '', 'regex' => '/[^0-9]/'],
17
        'CODABAR' => ['len' => '1-255', 'oddeven' => '', 'code' => '', 'regex' => '/[^0-9$-:\/.+/'],
18
        'MSI' => ['len' => '1-20', 'oddeven' => '', 'code' => '', 'regex' => '/[^0-9]/'],
19
        'CODE11' => ['len' => '1-20', 'oddeven' => '', 'code' => '', 'regex' => '/[^0-9]/'],
20
        'GS1128' => ['len' => '2-255', 'oddeven' => '', 'code' => 'ASCII', 'regex' => ''],
21
        'GS1DATABAROMINI' => ['len' => '13', 'code' => '', 'oddeven' => '', 'regex' => '/[^0-9]/'],
22
        'GS1DATABARTRUNC' => ['len' => '13', 'oddeven' => '', 'code' => '', 'regex' => '/[^0-9]/'],
23
        'GS1DATABARLIMIT' => ['len' => '13', 'oddeven' => '', 'code' => '', 'regex' => '/[^0-9]/'],
24
        'GS1DATABAREXPAN' => ['len' => '2-41', 'oddeven' => '', 'code' => '', 'regex' => '/[^0-9A-Za-z]/']
25
    ];
26
    
27
    /**
28
     * Adjust data to barcode parameters
29
     *
30
     * @param string $data barcode data
31
     * @param string $type type of barcode
32
     * @return string|boolean
33
     */
34
    public static function validate($data, $type)
35
    {
36
        if (!array_key_exists($type, self::$barcode1D)) {
37
            return false;
38
        }
39
        $bcSpks = self::$barcode1D[$type];
40
        $len = $bcSpks['len'];
41
        $oddeven = $bcSpks['oddeven'];
42
        $regex = $bcSpks['regex'];
43
        $code = $bcSpks['code'];
44
        //apply rules over barcode data
45
        if ($code == 'ASCII') {
46
            $data = iconv('utf-8', 'us-ascii//TRANSLIT', $data);
47
        }
48
        if ($regex != '') {
49
            $data = preg_replace($regex, '', $data);
50
        }
51
        $data = trim($data);
52
        if (empty($data)) {
53
            return false;
54
        }
55
        $dlen = strlen($data);
56
        if (($oddeven == 'even' && $dlen % 2 != 0)
57
            || ($oddeven == 'odd' && $dlen % 2 == 0)
58
        ) {
59
                return false;
60
        }
61
        $al = explode('-', $len);
62
        if (count($al) > 1) {
63
            if ($dlen < $al[0]) {
64
                return false;
65
            }
66
            $alen = $al[1];
67
        } else {
68
            $alen = $al[0];
69
        }
70
        $data = substr($data, 0, $alen);
71
        return $data;
72
    }
73
}
74