GermanyDetector::parse()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 0
1
<?php
2
3
namespace PBurggraf\LicensePlate\Detector;
4
5
use PBurggraf\LicensePlate\Detector\Germany\AbstractGermanyPlate;
6
use PBurggraf\LicensePlate\Response\LicensePlateResponse;
7
8
/**
9
 * @author Philip Burggraf <[email protected]>
10
 */
11
class GermanyDetector extends AbstractDetector
12
{
13
    const PLATE_TYPE_INVALID = 'invalid';
14
    const PLATE_TYPE_DEFAULT = 'default';
15
    const PLATE_TYPE_GOVERNMENT = 'government';
16
    const PLATE_TYPE_DIPLOMATIC_CORPS = 'diplomatic-corps';
17
    const PLATE_TYPE_FEDERAL = 'federal';
18
    const PLATE_TYPE_LOCAL_POLICE = 'local-police';
19
    const PLATE_TYPE_MILITARY = 'military';
20
21
    /**
22
     * @return LicensePlateResponse
23
     */
24
    public function parse()
25
    {
26
        // match default plate layout (inclusive historic)
27
        if (preg_match('/^(?:[A-ZÄÖÜ]{1,3} (?:[A-Z]{1,2} [1-9][0-9]{0,3}|(?:\d{1,4} \d{1,4}|\d{1,6}))[H]?|\d{1,2} \d{1,2}|(?:(?:0|B|BN) \d{2,3} \d{1,3}[A-Z]?|[A-Z]{1,3} 9\d{1,4}[A-Z]?))$/u', $this->normalizedLicensePlate) === 1) {
28
            $plateParts = $this->seperatedLicensePlate;
29
30
            /** @var AbstractGermanyPlate $plateClassname */
31
            $plateClassname = __NAMESPACE__ . '\\Germany\\' . 'Plate' . $this->mb_ucfirst(mb_strtolower(str_replace(['ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü'], ['ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue'], $plateParts[0])));
32
33
            if (class_exists($plateClassname)) {
34
                $licensePlate = count($plateParts) > 2 ? $plateParts[0] . ' ' . $plateParts[1] . ' ' . $plateParts[2] : $plateParts[0] . ' ' . $plateParts[1];
35
36
                /** @var AbstractGermanyPlate $plate */
37
                $plate = new $plateClassname($licensePlate);
38
39
                $this->response->setValid($plate->isValid());
40
                $this->response->setType($plate->getType());
41
                $this->response->addDetail($plate->getDescription());
42
            }
43
        }
44
45
        return $this->response;
46
    }
47
}
48