From db83565c54a2b97ad9cf3e83314775193876be43 Mon Sep 17 00:00:00 2001 From: Li Zhineng Date: Mon, 12 May 2025 19:27:10 +0800 Subject: determine if clean energy --- src/RegistrationNumber.php | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'src/RegistrationNumber.php') diff --git a/src/RegistrationNumber.php b/src/RegistrationNumber.php index 6e729e3..3f6edcc 100644 --- a/src/RegistrationNumber.php +++ b/src/RegistrationNumber.php @@ -134,10 +134,23 @@ final readonly class RegistrationNumber 'F', 'G', 'H', 'J', 'K', ]; + /** + * @var string[] + */ + private const array BATTERY_ELECTRIC_LETTERS = [ + 'D', 'A', 'B', 'C', 'E', + ]; + + public string $region; + + public string $authority; + + public string $sequence; + private function __construct( public string $registrationNumber ) { - // + $this->parse($registrationNumber); } public static function make(string $registrationNumber): static @@ -250,4 +263,27 @@ final readonly class RegistrationNumber 'The registration number "%s" is invalid.', $registrationNumber )); } + + private function parse(string $registrationNumber): void + { + $this->region = mb_substr($registrationNumber, 0, 1); + $this->authority = mb_substr($registrationNumber, 1, 1); + $this->sequence = mb_substr($registrationNumber, 2); + } + + public function isCleanEnergy(): bool + { + return strlen($this->sequence) === 6; + } + + public function isBatteryElectric(): bool + { + if (! $this->isCleanEnergy()) { + return false; + } + + $code = mb_substr($this->sequence, 0, 1); + + return in_array($code, self::BATTERY_ELECTRIC_LETTERS); + } } -- cgit v1.2.3