Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Added PhoneNumber for a cast model and validation rule, digging deeper #54292

Closed

Conversation

michaelnabil230
Copy link
Contributor

This pull request introduces the PhoneNumber class to handle phone number operations in a standardized and extensible manner. Additionally, the PR includes enhancements to integrate the PhoneNumber class within Laravel models through casting and validation rules. I think the community needs this in framework like email.

Key Features

Provides a clean API for handling phone number formatting, validation, and type checking.

Supports multiple formats: International, National, E164, and RFC3966.

Integrates with the libphonenumber library for accurate phone number parsing and formatting.

Model Casting:

Introduced a custom cast to seamlessly handle PhoneNumber objects in Laravel models.

protected $casts = [
    'phone' => AsRawPhoneNumber::class,
    'phone' => AsE164PhoneNumber::class,
];

Validation Rule:

Added a custom validation rule ValidPhoneNumber to ensure phone numbers conform to the expected format and are valid for the given region.

$request->validate([
    'phone' => ['required', new Rule::phone()->country('US')],
]);

Digging Deeper PhoneNumber:

Added a custom class for Digging Deeper to ensure phone numbers conform to the expected format and are valid for the given region.

Illuminate\Support\PhoneNumber::of('+201200954866')->isValid();

Enhancements:

Unit Tests: Comprehensive test coverage for the PhoneNumber class, model casting, and validation rule.

Error Handling: Improved exception handling for invalid numbers and unsupported operations.

Notes:

This implementation leverages the libphonenumber library for core phone number operations, ensuring reliability and accuracy.

The validation rule supports regional validation, making it adaptable to different use cases.

Let me know if further refinements or additional features are needed!

@michaelnabil230 michaelnabil230 changed the title [11.x] Added Phone for a cast model and validation rule, digging deeper [11.x] Added PhoneNumber for a cast model and validation rule, digging deeper Jan 21, 2025
@michaelnabil230
Copy link
Contributor Author

TODO:

I encountered an issue with the test test_can_format_number. When run on PHP 8.2 and PHP 8.4, it fails, but it passes on PHP 8.3. Can anyone help identify the reason for this behavior?

@taylorotwell
Copy link
Member

I really feel like this should be a package. 🙂

@michaelnabil230 michaelnabil230 deleted the phone-cast-and-validation branch January 21, 2025 20:35
@michaelnabil230
Copy link
Contributor Author

Hello @taylorotwell,

Thank you for your feedback! 😊 While I understand the suggestion to make this a standalone package, integrating PhoneNumber directly into the framework adds significant value, similar to email validation.

Phone numbers are a core part of many applications, and having built-in support for formatting, validation, and type-checking ensures developers can use this functionality without additional dependencies. It simplifies the developer experience and aligns with Laravel's philosophy of providing tools for common application needs out of the box.

Feel free to let me know if you have further thoughts! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants