zam3858/bemykad
Composer 安装命令:
composer require zam3858/bemykad
包简介
A PHP package to validate and extract information from Malaysian MyKad numbers.
README 文档
README
BeMyKad
BeMyKad is a PHP package for extracting information from Malaysian MyKad numbers. This package helps validate MyKad numbers, retrieve the date of birth, gender, and identify the state or country of birth from the MyKad number.
Installation
To install the package, use Composer:
composer require zam3858/bemykad
Usage
Below is a simple example of how to use the BeMyKad class.
Basic Example
use BeMyKad\BeMyKad; $mykadNumber = "960101-14-1234"; $mykad = new BeMyKad($mykadNumber); // Validate the MyKad number if ($mykad->isValid()) { echo "MyKad is valid\n"; // Get date of birth echo "Date of Birth: " . $mykad->getDateOfBirth() . "\n"; // Get gender $gender = $mykad->getGender() === BeMyKad::MALE ? 'Male' : 'Female'; echo "Gender: " . $gender . "\n"; // Get state or country of birth echo "Place of Birth: " . $mykad->getState() . "\n"; /** * Outputs * { * "mykad": "960101141234", * "formatted": "960101-14-1234", * "isValid": true, * "dateOfBirth": "1996-01-01", * "gender": "Male", * "state": "Johor" * } */ echo $mykad; } else { echo "Invalid MyKad number."; }
Methods
__construct(string $mykadNumber): Initializes theBeMyKadinstance with a MyKad number, automatically removing dashes for uniformity.isValid(): bool: Returnstrueif the MyKad number format and date of birth are valid.getDateOfBirth(): ?string: Returns the date of birth inY-m-dformat if valid, ornullif invalid.getGender(): ?int: ReturnsBeMyKad::MALEorBeMyKad::FEMALEif valid, ornullif invalid.getState(): ?string: Returns the place of birth (state or country) if valid. For codes that don't map to a specific place, returns'Unknown'.
Validation Details
- Date of Birth Validation: Ensures the date part of the MyKad number is valid by verifying it corresponds to a real calendar date and matches the expected century.
- Format Validation: Ensures the MyKad number is in one of two acceptable formats:
YYMMDD-SS-SSSSorYYMMDDSSSSSS.
Handling Unmapped Codes
Certain codes, such as '99', represent categories rather than specific locations and return 'Unknown'. This ensures a consistent output for codes that indicate unspecified regions.
Exceptions
The BeMyKad class does not throw exceptions by default for invalid MyKad numbers. Instead, validation methods (isValid, getDateOfBirth, getGender, getState) return null or false when the MyKad number is invalid.
Additional Class: BirthCountry
The BirthCountry class contains the mappings for MyKad place-of-birth codes to their respective locations. You can retrieve the mapped location directly using the following method:
getPlaceOfBirth(string $code): string: Returns the place of birth corresponding to the provided code. If the code is not recognized, it returns'Unknown'.
Example Usage of BirthCountry
use BeMyKad\BirthCountry; echo BirthCountry::getPlaceOfBirth('10'); // Outputs: "Selangor" echo BirthCountry::getPlaceOfBirth('99'); // Outputs: "Unknown"
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
We welcome contributions! Please see CONTRIBUTING for details on how to get involved.
Security Vulnerabilities
If you discover any security issues, please review our security policy for reporting vulnerabilities.
Credits
License
This package is open-sourced software licensed under the MIT License.
zam3858/bemykad 适用场景与选型建议
zam3858/bemykad 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 zam3858/bemykad 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zam3858/bemykad 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-04