|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PBurggraf\LicensePlate\Detector; |
|
4
|
|
|
|
|
5
|
|
|
use PBurggraf\LicensePlate\Response\LicensePlateResponse; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @author Philip Burggraf <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class AbstractDetector implements DetectorInterface |
|
11
|
|
|
{ |
|
12
|
|
|
const PLATE_TYPE_UNKNOWN = GermanyDetector::PLATE_TYPE_INVALID; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $licensePlate; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $normalizedLicensePlate; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string[] |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $seperatedLicensePlate; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var LicensePlateResponse |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $response; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* AbstractDetector constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param $licensePlate |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct($licensePlate) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->licensePlate = $licensePlate; |
|
42
|
|
|
$this->normalizedLicensePlate = $this->normalize($licensePlate); |
|
43
|
|
|
$this->seperatedLicensePlate = $this->seperate($this->normalizedLicensePlate); |
|
44
|
|
|
|
|
45
|
|
|
$this->response = new LicensePlateResponse($this->licensePlate); |
|
46
|
|
|
$this->response->setCountry(strrev(strstr(strrev(str_replace('Detector', '', get_called_class())), '\\', true))); |
|
47
|
|
|
|
|
48
|
|
|
$this->parse(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return LicensePlateResponse |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getResponse() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->response; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $licensePlate |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function normalize($licensePlate) |
|
65
|
|
|
{ |
|
66
|
|
|
// remove dashes |
|
67
|
|
|
$result = str_replace([ |
|
68
|
|
|
'-', |
|
69
|
|
|
], ' ', $licensePlate); |
|
70
|
|
|
|
|
71
|
|
|
// uppercase only |
|
72
|
|
|
$result = mb_strtoupper($result); |
|
73
|
|
|
|
|
74
|
|
|
return $result; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $licensePlate |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function seperate($licensePlate) |
|
83
|
|
|
{ |
|
84
|
|
|
// split plate |
|
85
|
|
|
return explode(' ', $licensePlate); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $string |
|
90
|
|
|
* @param string $encoding |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function mb_ucfirst($string, $encoding = null) |
|
95
|
|
|
{ |
|
96
|
|
|
if (null === $encoding) { |
|
97
|
|
|
$encoding = mb_internal_encoding(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$strlen = mb_strlen($string, $encoding); |
|
101
|
|
|
$firstChar = $string[0]; |
|
102
|
|
|
$then = mb_substr($string, 1, $strlen - 1, $encoding); |
|
103
|
|
|
|
|
104
|
|
|
return mb_strtoupper($firstChar, $encoding) . $then; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return LicensePlateResponse |
|
109
|
|
|
*/ |
|
110
|
|
|
abstract public function parse(); |
|
111
|
|
|
} |
|
112
|
|
|
|