{"id":18847939,"url":"https://github.com/settermjd/laminas-phone-number-validator","last_synced_at":"2025-07-02T21:33:55.433Z","repository":{"id":258894273,"uuid":"875870601","full_name":"settermjd/laminas-phone-number-validator","owner":"settermjd","description":"A custom phone number validator for laminas-validator that uses Twilio's Lookup (V2) API to verify if the phone number is valid.","archived":false,"fork":false,"pushed_at":"2025-01-28T23:13:26.000Z","size":233,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T12:21:58.210Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/settermjd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/funding.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"settermjd","buy_me_a_coffee":"settermjd"}},"created_at":"2024-10-21T02:23:31.000Z","updated_at":"2025-03-06T11:39:25.000Z","dependencies_parsed_at":"2024-10-22T23:39:01.237Z","dependency_job_id":null,"html_url":"https://github.com/settermjd/laminas-phone-number-validator","commit_stats":null,"previous_names":["settermjd/laminas-phone-number-validator"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/settermjd/laminas-phone-number-validator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/settermjd%2Flaminas-phone-number-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/settermjd%2Flaminas-phone-number-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/settermjd%2Flaminas-phone-number-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/settermjd%2Flaminas-phone-number-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/settermjd","download_url":"https://codeload.github.com/settermjd/laminas-phone-number-validator/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/settermjd%2Flaminas-phone-number-validator/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263218734,"owners_count":23432545,"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":[],"created_at":"2024-11-08T03:10:18.700Z","updated_at":"2025-07-02T21:33:55.354Z","avatar_url":"https://github.com/settermjd.png","language":"PHP","funding_links":["https://github.com/sponsors/settermjd","https://buymeacoffee.com/settermjd"],"categories":[],"sub_categories":[],"readme":"# Phone Number Validator that uses Twilio's Lookup (V2) API\n\n![Build Status](https://github.com/settermjd/laminas-phone-number-validator/actions/workflows/php.yml/badge.svg)\n\nThis is a custom laminas-validator class that checks if a phone number is valid by using Twilio's Lookup API.\n\n## Overview\n\nThe package provides a custom laminas-validator class that checks if a phone number is valid by using Twilio's Lookup API, providing a simple way of validating phone numbers are valid, based on communications provider data, accessed through Twilio.\n\n## Requirements\n\nTo use the application, you'll need the following:\n\n- A Twilio account (free or paid).\n  [Create an account][twilio-referral-url] if you don't already have one.\n- PHP 8.3\n- [Composer][composer-url] installed globally\n- [Git][git-url]\n\n## Getting Started\n\n### Add the Package as a Project Dependency\n\nTo use the package in your project, first, either add it in _composer.json_'s `require` attribute, as in the example below.\n\n```json\n\"require\": {\n    \"settermjd/laminas-twilio-phone-number-validator\": \"^1.0\"\n}\n```\n\nOr, use `composer require` to add it:\n\n```bash\ncomposer require settermjd/laminas-twilio-phone-number-validator\n```\n\n### How to Use the Validator\n\nYou can use it directly, as in the following example, to validate a phone number.\n\n```php\nuse Settermjd\\Validator\\VerifyPhoneNumber;\nuse Twilio\\Rest\\Client;\n\n$validator = new VerifyPhoneNumber(new Client(\n    `\u003cYOUR_TWILIO_ACCOUNT_SID\u003e`,\n    `\u003cYOUR_TWILIO_AUTH_TOKEN\u003e`,\n));\n\nif ($validator-\u003eisValid($email)) {\n    // The phone number is valid, so do what you want knowing that.\n} else {\n    // The phone number is not valid, so show the reasons why.\n    foreach ($validator-\u003egetMessages() as $messageId =\u003e $message) {\n        printf(\"Validation failure '%s': %s\\n\", $messageId, $message);\n    }\n}\n```\n\nOr, you can use it in conjunction with [laminas-inputfilter][laminas-inputfilter-url], as in the following example.\n\n```php\nuse Laminas\\InputFilter\\InputFilter;\nuse Laminas\\InputFilter\\Input;\nuse Laminas\\Validator;\nuse Settermjd\\Validator\\VerifyPhoneNumber;\nuse Twilio\\Rest\\Client;\n\n$phoneNumber = new Input('phone_number');\n$phoneNumber-\u003egetValidatorChain()\n          -\u003eattach(\n          new VerifyPhoneNumber(\n              new Client(\n                  `\u003cTWILIO_ACCOUNT_SID\u003e`,\n                  `\u003cTWILIO_AUTH_TOKEN\u003e`,\n              )\n          )\n    );\n\n$inputFilter = new InputFilter();\n$inputFilter-\u003eadd($phoneNumber);\n\n$inputFilter-\u003esetData($_POST);\nif ($inputFilter-\u003eisValid()) {\n    echo \"The form is valid\\n\";\n} else {\n    echo \"The form is not valid\\n\";\n    foreach ($inputFilter-\u003egetInvalidInput() as $error) {\n        print_r($error-\u003egetMessages());\n    }\n}\n```\n\nIn both of the above examples, the `VerifyPhoneNumber` validator is initialised with a `Twilio\\Rest\\Client` object, which in turn is initialised with a Twilio Account SID and Auth Token.\nTo retrieve these, open [the Twilio Console][twilio-console-url] in your browser of choice, then copy the **Account SID** and **Auth Token** from the **Account Info**, as you can see in the screenshot below.\n\n![The Account Info panel of the Twilio Console, showing a user's Account SID, Auth Token, and phone number, where the Account SID and phone number have been partially or completely redacted.](./docs/images/twilio-console-account-info-panel.png)\n\n\u003e [!CAUTION]\n\u003e Use a package such as [PHP Dotenv][phpdotenv-url] to keep credentials, such as the Twilio Account SID and Auth Token out of code, and avoid them accidentally being tracked by Git (or your version control tool of choice), or your deployment tool's secrets manager is strongly encouraged.\n\n#### Supply query parameters\n\nThe previous example didn't supply any query parameters.\nThey allow you to retrieve additional data during the lookup, such as [when the SIM was last swapped](https://www.twilio.com/en-us/blog/how-to-detect-sim-swap-with-php-before-sending-sms-otp), and whether call forwarding is enabled.\n\n\u003e [!CAUTION]\n\u003e Be aware that some query parameters, such as in `Field`s require data packages which will incur charges on your account. Please double-check your code before running it so that you don't accidentally incur excessive unintended Lookup charges.\n\n##### Filter and validate query parameters\n\nTo ensure that the query parameters provided to the validator are valid, you can use the `QueryParametersInputFilter`.\nThis is a custom [laminas-inputfilter](https://docs.laminas.dev/laminas-inputfilter/intro/) class which ensures that the query parameter data provided is valid, and doesn't contain malicious information or values unsupported by the Lookup API.\n\n```php\nuse Laminas\\InputFilter\\InputFilter;\nuse Laminas\\InputFilter\\Input;\nuse Laminas\\Validator;\nuse Settermjd\\InputFilter\\QueryParametersInputFilter;\nuse Settermjd\\Validator\\VerifyPhoneNumber;\nuse Twilio\\Rest\\Client;\n\n$inputFilter = new QueryParametersInputFilter();\n$inputFilter-\u003esetData($suppliedQueryParameters);\nif ($inputFilter-\u003eisValid()) {\n    $validator = new VerifyPhoneNumber(\n        twilioClient: new Client(\n            `\u003cYOUR_TWILIO_ACCOUNT_SID\u003e`,\n            `\u003cYOUR_TWILIO_AUTH_TOKEN\u003e`,\n        ),\n        queryParameters: $inputFilter-\u003egetValues(),\n    );\n\n    if ($validator-\u003eisValid($email)) {\n        // The phone number is valid, so do what you want knowing that.\n    } else {\n        // The phone number is not valid, so show the reasons why.\n        foreach ($validator-\u003egetMessages() as $messageId =\u003e $message) {\n            printf(\"Validation failure '%s': %s\\n\", $messageId, $message);\n        }\n    }\n}\n```\n\nIn the example above, the query parameters (`$suppliedQueryParameters`) are checked with `QueryParametersInputFilter`.\nIf the data is valid, then the `VerifyPhoneNumber` validator is instantiated and used; that code is exactly the same as in the first example above.\n\n#### Add Caching Support\n\nThe validator is [PSR-16][psr16-url]-compliant.\nSo, if you want to further enhance performance, when initialising a `VerifyPhoneNumber` object, provide an object that implements [CacheInterface][cacheinterface-url] as the third argument; the example below uses [laminas-cache][laminascache-psr16-url].\n\n```php\nuse Laminas\\Cache\\Psr\\SimpleCache\\SimpleCacheDecorator;\nuse Laminas\\Cache\\Service\\StorageAdapterFactoryInterface;\nuse Psr\\Container\\ContainerInterface;\nuse Settermjd\\Validator\\VerifyPhoneNumber;\nuse Twilio\\Rest\\Client;\n\n/** @var ContainerInterface $container */\n$container = null; // can be any configured PSR-11 container\n\n$storageFactory = $container-\u003eget(StorageAdapterFactoryInterface::class);\n$storage = $storageFactory-\u003ecreate('apc');\n\n$validator = new VerifyPhoneNumber(\n    twilioClient: new Client(`\u003cYOUR_TWILIO_ACCOUNT_SID\u003e`, `\u003cYOUR_TWILIO_AUTH_TOKEN\u003e`), \n    cache: new SimpleCacheDecorator($storage),\n);\n```\n\nIf you're not sure which PSR-16 implementation to use, [check out the full list of providers on Packagist][simplecache-implementation-url].\n\n## Contributing\n\nIf you want to contribute to the project, whether you have found issues with it or just want to improve it, here's how:\n\n- [Issues][github-issues-url]: ask questions and submit your feature requests, bug reports, etc\n- [Pull requests][github-pr-url]: send your improvements\n\n## Did You Find the Project Useful?\n\nIf the project was useful, and you want to say thank you and/or support its active development, here's how:\n\n- Add a GitHub Star to the project\n- Write an interesting article about the project wherever you blog\n\n[cacheinterface-url]: https://www.php-fig.org/psr/psr-16/#21-cacheinterface\n[composer-url]: https://getcomposer.org\n[git-url]: https://git-scm.com/downloads\n[twilio-console-url]: https://console.twilio.com/\n[twilio-referral-url]: http://www.twilio.com/referral/QlBtVJ\n[github-issues-url]: https://github.com/settermjd/laminas-phone-number-validator/issues\n[github-pr-url]: https://github.com/settermjd/laminas-phone-number-validator/pulls\n[laminascache-psr16-url]: https://docs.laminas.dev/laminas-cache/v4/psr16/\n[laminas-inputfilter-url]: https://docs.laminas.dev/laminas-inputfilter/\n[phpdotenv-url]: https://github.com/vlucas/phpdotenv\n[psr16-url]: https://www.php-fig.org/psr/psr-16/\n[simplecache-implementation-url]: https://packagist.org/providers/psr/simple-cache-implementation\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsettermjd%2Flaminas-phone-number-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsettermjd%2Flaminas-phone-number-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsettermjd%2Flaminas-phone-number-validator/lists"}