{"id":19851917,"url":"https://github.com/parsilver/php-phone-verification","last_synced_at":"2025-02-28T21:16:24.075Z","repository":{"id":44788368,"uuid":"450575231","full_name":"parsilver/php-phone-verification","owner":"parsilver","description":"ระบบที่ใช้ในการยืนยันผู้ใช้งานผ่าน SMS OTP","archived":false,"fork":false,"pushed_at":"2022-01-31T03:20:53.000Z","size":33,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-11T13:24:03.332Z","etag":null,"topics":["otp","otp-verification","php","sms"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/parsilver.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-21T17:09:28.000Z","updated_at":"2022-06-30T03:34:25.000Z","dependencies_parsed_at":"2022-09-19T06:30:18.862Z","dependency_job_id":null,"html_url":"https://github.com/parsilver/php-phone-verification","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parsilver%2Fphp-phone-verification","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parsilver%2Fphp-phone-verification/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parsilver%2Fphp-phone-verification/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parsilver%2Fphp-phone-verification/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/parsilver","download_url":"https://codeload.github.com/parsilver/php-phone-verification/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241240784,"owners_count":19932620,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["otp","otp-verification","php","sms"],"created_at":"2024-11-12T13:33:10.735Z","updated_at":"2025-02-28T21:16:24.059Z","avatar_url":"https://github.com/parsilver.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## PHP - Phone Verification\n\nเป็นระบบที่ใช้ในการยืนยันผู้ใช้งานผ่าน SMS OTP \n\nเช่น ใช้ในกรณีที่ระบบของท่านมีระบบสมาชิก และต้องการที่จะยืนยันผู้ใช้งานผ่าน SMS OTP ท่านสามารถใช้ Library ตัวนี้ช่วยท่านได้อีกแรง เพื่อความเป็นระบบเรียบร้อยของระบบของท่าน และสามารถดูแลได้ง่าย\n\n\n### ตัวอย่างการใช้งานคร่าวๆ\n\n```php\nuse Farzai\\PhoneVerification\\Adapters\\SmsAdapter;\nuse Farzai\\PhoneVerification\\SMS\\Providers\\NexmoProvider;\nuse Farzai\\PhoneVerification\\Verifier;\nuse Farzai\\PhoneVerification\\Exceptions\\InvalidVerificationException;\nuse Farzai\\PhoneVerification\\Exceptions\\VerificationHasExpired;\n\n// การใช้งาน\n// เริ่มที่ตั้งต่าการใช้งาน\n$adapter = new SmsAdapter(new NexmoProvider(), $repository);\n\n// สร้างการยืนยัน และส่ง SMS-OTP ไปยังเบอร์ปลายทาง\n$verifier = new Verifier($adapter);\n$entity = $verifier-\u003ecreate(\n    $phoneNumber = \"0988887777\"\n);\n\n// ผลลัพท์ที่ได้ ท่านสามารถนำมาใช้งานต่อได้\n$entity-\u003ephone_number;\n$entity-\u003ereference;\n$entity-\u003ecode;\n\n\n// ---------------------------\n// การยืนยัน OTP\n$phoneNumber = \"0988887777\";\n$reference = \"xxxxxx\";\n$code = \"xxxxxx\";\n\ntry {\n    $entity = $verifier-\u003everify($phoneNumber, $reference, $code);\n    \n    // ผลลัพท์ที่ได้ ท่านสามารถนำมาใช้งานต่อได้\n    $entity-\u003ephone_number;\n    $entity-\u003ereference;\n    $entity-\u003ecode;\n    \n    // ....\n} catch (InvalidVerificationException) {\n    // เกิดข้อผิดพลาด กรณีรหัสตรวสสอบไม่ถูกต้อง\n} catch (VerificationHasExpired) {\n    // เกิดข้อผิดพลาด กรณีรหัสตรวจสอบหมดอายุแล้ว\n}\n```\n\n### ความต้องการระบบ\n```json\n{\n  \"php\": \"^7.4|^8.0\",\n  \"ext-json\": \"*\"\n}\n```\n\n\n---\n\n## การใช้งาน\n\n### ติดตั้งผ่าน Composer\n```\ncomposer require farzai/phone-verification\n```\n\n### เริ่มต้นการตั้งค่า\n\nเนื่องจากการใช้งานจะมีการเก็บข้อมูล และเรียกข้อมูลที่ท่านถือไว้จากฐานข้อมูล\n\nดังนั้น ท่านจึงจำเป็นต้องบอกเราหน่อยว่าท่านเก็บข้อมูลอย่างไร และเรียกอย่างไร โดยการสร้าง Repository และ `implements` Interface เช่นตัวอย่างด้านล่าง\n```php\nuse Farzai\\PhoneVerification\\Repositories\\VerificationRepository;\nuse Farzai\\PhoneVerification\\Entities\\Verification;\n\nclass MyRepository implements VerificationRepository\n{\n     /**\n     * เมื่อระบบต้องการสร้างการยินยัน\n     * \n     * @param string $phoneNumber\n     * @return Verification\n     */\n    public function create(string $phoneNumber): Verification\n    {\n        // บันทึกลงฐานข้อมูลของท่าน ตรงนี้....\n        // -----\n        \n        // จากนั้นสร้าง Entity และคืนค่า Entity ให้เรา\n        return new Verification([\n            'phone_number' =\u003e $phoneNumber,\n            'reference' =\u003e (string)random_int(10000, 99999),\n            'code' =\u003e (string)random_int(1000, 9999),\n        ]);\n    }\n\n    /**\n     * เมื่อระบบต้องการค้นหาเพื่อยืนยันว่ารหัสที่ส่งมาถูกหรือไม่?\n     * \n     * @param string $phoneNumber\n     * @param string $reference\n     * @param string $code\n     * @return Verification|null\n     */\n    public function findForValidate(string $phoneNumber, string $reference, string $code): ?Verification\n    {\n        // ค้นหาจากฐานข้อมูลของท่าน\n        // ....\n        \n        // ถ้าไม่พบ ให้ return null ให้เรา\n        // ....\n        // return null;\n        \n        // ถ้าค้นพบให้ส่ง Entity กลับมาหาเรา\n        return new Verification([\n            'phone_number' =\u003e $phoneNumber,\n            'reference' =\u003e $reference,\n            'code' =\u003e $code,\n            \n            // (optional)\n            // ใส่ parameter: expires_at ถ้าอยากให้เราตรวจสอบว่าหมดอายุหรือยัง\n            'expires_at' =\u003e new \\DateTime('now'),\n        ])\n    }\n\n    /**\n     * เมื่อระบบต้องการทำให้การยืนยันของ record นี้หมดอายุ\n     * \n     * @param Verification $verifier\n     */\n    public function markAsExpired(Verification $verifier): Verification\n    {\n        // ทำให้ record นั้น \"หมดอายุ\"\n\n        return $verifier;\n    }\n\n    /**\n     * เมื่อระบบต้องการทำให้การยืนยันของ record นี้ถูกยืนยันไปแล้ว\n     * \n     * @param Verification $verifier\n     */\n    public function markAsVerified(Verification $verifier): Verification\n    {\n        // ทำให้ record นั้น \"ยืนยันแล้ว\"\n\n        return $verifier;\n    }\n}\n```\n\n\n\n### ขั้นตอนการใช้งาน\n### 1. ส่ง SMS-OTP\n```php\nuse Farzai\\PhoneVerification\\Adapters\\SmsAdapter;\nuse Farzai\\PhoneVerification\\SMS\\Providers\\NexmoProvider;\nuse Farzai\\PhoneVerification\\Verifier;\nuse Farzai\\PhoneVerification\\Exceptions\\InvalidVerificationException;\nuse Farzai\\PhoneVerification\\Exceptions\\VerificationHasExpired;\n\n// การใช้งาน\n// เริ่มที่ตั้งต่าการใช้งาน\n\n// ที่เก็บข้อมูลของเรา\n$repository = new MyRepository();\n\n// ตัวส่ง SMS\n// ในตัวอย่างนี้ เราเลือกใช้ Nexmo เป็นตัวอย่างก่อน\n$sms = new NexmoProvider([\n    'key'   =\u003e 'xxxxxxxx',\n    'from'  =\u003e 'xxxxxxxx',\n]);\n\n// สร้าง Adapter\n$adapter = new SmsAdapter($sms, $repository);\n\n// สร้างตัวดำเนินการ\n$verifier = new Verifier($adapter);\n\n// ส่ง SMS-OTP\n$entity = $verifier-\u003ecreate(\n    $phoneNumber = \"0988887777\"\n);\n\n// ผลลัพท์ที่ได้ ท่านสามารถนำมาใช้งานต่อได้\n$entity-\u003ephone_number;\n$entity-\u003ereference;\n$entity-\u003ecode;\n```\n\n\n### 2. ยืนยันและตรวจสอบ SMS-OTP\n```php\nuse Farzai\\PhoneVerification\\Adapters\\SmsAdapter;\nuse Farzai\\PhoneVerification\\SMS\\Providers\\NexmoProvider;\nuse Farzai\\PhoneVerification\\Verifier;\nuse Farzai\\PhoneVerification\\Exceptions\\InvalidVerificationException;\nuse Farzai\\PhoneVerification\\Exceptions\\VerificationHasExpired;\n\n// รับ Input จากระบบของท่าน\n$phoneNumber = \"0988887777\";\n$reference = \"xxxxxx\";\n$code = \"xxxxxx\";\n\n\n// ที่เก็บข้อมูลของเรา\n$repository = new MyRepository();\n\n// สร้างตัวดำเนินการ\n$sms = new NexmoProvider([\n    'key'   =\u003e 'xxxxxxxx',\n    'from'  =\u003e 'xxxxxxxx',\n]);\n\n$adapter = new SmsAdapter($sms, $repository);\n$verifier = new Verifier($adapter);\n\n// ทำการตรวจสอบ\ntry {\n    $entity = $verifier-\u003everify($phoneNumber, $reference, $code);\n    \n    // ผลลัพท์ที่ได้ ท่านสามารถนำมาใช้งานต่อได้\n    $entity-\u003ephone_number;\n    $entity-\u003ereference;\n    $entity-\u003ecode;\n    \n    // ....\n} catch (InvalidVerificationException) {\n    // เกิดข้อผิดพลาด กรณีรหัสตรวสสอบไม่ถูกต้อง\n} catch (VerificationHasExpired) {\n    // เกิดข้อผิดพลาด กรณีรหัสตรวจสอบหมดอายุแล้ว\n}\n```\n\n## ตัวอย่าง\nเราได้แนบไฟล์ตัวอย่างไว้ที่นี่เพื่อเป็นตัวอย่างประกอบ [ไฟล์ตัวอย่าง](example/README.md)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparsilver%2Fphp-phone-verification","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparsilver%2Fphp-phone-verification","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparsilver%2Fphp-phone-verification/lists"}