Completed
Push — master ( 3d7536...3ced6b )
by Marcin
02:56
created

Pna::isValid()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

Changes 0
Metric Value
dl 18
loc 18
ccs 8
cts 9
cp 0.8889
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 7
nop 2
crap 5.0342
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
16
namespace mrcnpdlk\Validator\Types;
17
18
19
use mrcnpdlk\Validator\Exception;
20
use mrcnpdlk\Validator\TypeInterface;
21
22
/**
23
 * Class Pna
24
 *
25
 * Polish postal code validator
26
 *
27
 * @package mrcnpdlk\Validator\Types
28
 */
29
class Pna extends TypeAbstract implements TypeInterface
30
{
31
    /**
32
     * @param mixed $checkedValue
33
     * @param bool  $asEx
34
     *
35
     * @return bool
36
     * @throws Exception
37
     */
38 3 View Code Duplication
    public static function isValid($checkedValue, bool $asEx = false): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
39
    {
40
        try {
41 3
            static::isValidType($checkedValue, static::TYPE_STRING, true);
42
43 3
            if (!preg_match('/^[0-9]{5}$/', $checkedValue) && !preg_match('/^[0-9]{2}-[0-9]{3}$/', $checkedValue)) {
44 1
                throw new \Exception("Regexp error", 1);
45
            }
46
47 2
            return true;
48 1
        } catch (\Exception $e) {
49 1
            if ($asEx) {
50 1
                throw new Exception(sprintf("Invalid PNA number [%s], reason: %s", $checkedValue, $e->getMessage()));
51
            } else {
52
                return false;
53
            }
54
        }
55
    }
56
57
    /**
58
     * Usuwamy niepotrzebne separatory
59
     *
60
     * @param mixed $checkedValue
61
     *
62
     * @return string
63
     */
64 2
    public static function clean($checkedValue)
65
    {
66 2
        static::isValidType($checkedValue, static::TYPE_STRING, true);
67
68 2
        return preg_replace('/[^0-9]/', '', trim($checkedValue));
69
    }
70
71
    /**
72
     * @return string
73
     */
74 2
    public function getLong()
75
    {
76 2
        return sprintf('%s-%s', substr($this->getShort(), 0, 2), substr($this->getShort(), 2, 3));
77
    }
78
79
    /**
80
     * Return short version of PNA
81
     *
82
     * @return string
83
     */
84 2
    public function getShort()
85
    {
86 2
        return $this->get();
87
    }
88
}
89