summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLi Zhineng <[email protected]>2025-05-12 21:25:25 +0800
committerLi Zhineng <[email protected]>2025-05-12 21:25:25 +0800
commit54bf2e23550c784bd650b99c4c8f7f824a5be1a5 (patch)
tree7874f7f32750c68e3398b4e968704f45c9e179b9 /src
parentd07cb71f53a8344fe420e32f4664d3d92dc05a07 (diff)
downloadvehicle-license-china-54bf2e23550c784bd650b99c4c8f7f824a5be1a5.tar.gz
vehicle-license-china-54bf2e23550c784bd650b99c4c8f7f824a5be1a5.zip
support large vehicle with clean energy
Diffstat (limited to 'src')
-rw-r--r--src/RegistrationNumber.php68
1 files changed, 65 insertions, 3 deletions
diff --git a/src/RegistrationNumber.php b/src/RegistrationNumber.php
index 3f6edcc..3e284ed 100644
--- a/src/RegistrationNumber.php
+++ b/src/RegistrationNumber.php
@@ -224,6 +224,42 @@ final readonly class RegistrationNumber
private static function checkSequenceForCleanEnergy(string $sequence): bool
{
$sequence = mb_strtoupper($sequence);
+
+ $last = mb_substr($sequence, -1);
+ $codepoint = mb_ord($last);
+
+ if (is_int($codepoint) && $codepoint >= ord('A') && $codepoint <= ord('Z')) {
+ return static::checkSequenceForLargeCleanEnergy($sequence);
+ }
+
+ return static::checkSequenceForSmallCleanEnergy($sequence);
+ }
+
+ private static function checkSequenceForLargeCleanEnergy(string $sequence): bool
+ {
+ $last = mb_substr($sequence, -1);
+
+ if (! in_array($last, self::CLEAN_ENERGY_FIRST_LETTERS)) {
+ return false;
+ }
+
+ for ($i = 0; $i < mb_strlen($sequence) - 1; $i++) {
+ $codepoint = mb_ord($sequence[$i]);
+
+ if ($codepoint === false) {
+ return false;
+ }
+
+ if ($codepoint < ord('0') || $codepoint > ord('9')) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private static function checkSequenceForSmallCleanEnergy(string $sequence): bool
+ {
$first = mb_substr($sequence, 0, 1);
if (! in_array($first, self::CLEAN_ENERGY_FIRST_LETTERS)) {
@@ -276,14 +312,40 @@ final readonly class RegistrationNumber
return strlen($this->sequence) === 6;
}
- public function isBatteryElectric(): bool
+ public function isSmallCleanEnergy(): bool
+ {
+ if (! $this->isCleanEnergy()) {
+ return false;
+ }
+
+ $first = mb_substr($this->sequence, 0, 1);
+
+ return in_array($first, self::CLEAN_ENERGY_FIRST_LETTERS, strict: true);
+ }
+
+ public function isLargeCleanEnergy(): bool
{
if (! $this->isCleanEnergy()) {
return false;
}
- $code = mb_substr($this->sequence, 0, 1);
+ $last = mb_substr($this->sequence, -1);
+
+ return in_array($last, self::CLEAN_ENERGY_FIRST_LETTERS, strict: true);
+ }
+
+ public function isBatteryElectric(): bool
+ {
+ $letter = match (true) {
+ $this->isSmallCleanEnergy() => mb_substr($this->sequence, 0, 1),
+ $this->isLargeCleanEnergy() => mb_substr($this->sequence, -1),
+ default => false,
+ };
+
+ if ($letter === false) {
+ return false;
+ }
- return in_array($code, self::BATTERY_ELECTRIC_LETTERS);
+ return in_array($letter, self::BATTERY_ELECTRIC_LETTERS, strict: true);
}
}