What is Metadata?
The metadata in libphonenumber-for-php contains all the rules, patterns, and information needed to:- Parse phone numbers from different countries
- Validate phone numbers according to regional rules
- Format phone numbers in various styles
- Determine phone number types (mobile, fixed-line, etc.)
When to Regenerate Metadata
You typically do not need to regenerate metadata yourself. The library maintainers update it regularly. You might need to regenerate metadata if:- You’re contributing to the library itself
- You need to test unreleased metadata changes from Google
- You’re creating a fork with custom modifications
- You want to use a specific version of Google’s libphonenumber data
The repository always includes up-to-date metadata. Unless you’re developing the library itself, you can skip the metadata generation process entirely.
Metadata Version
The current metadata version is tracked in theMETADATA-VERSION.php file:
METADATA-VERSION.php
- A specific tag (e.g.,
v9.0.25) - A commit hash
- A branch name
Build Process
The build process clones Google’s libphonenumber repository at the specified version and generates PHP classes from the XML metadata files.Running the Build
To regenerate metadata, use the Composer build command:- Generates metadata from Google’s libphonenumber
- Applies code style fixes
- Runs the test suite
- Executes static analysis
Metadata Only
If you only want to regenerate metadata without running tests:Build Requirements
To run the build process, you need:- PHP 8.1 or higher
- Composer with dev dependencies installed
- Git (to clone the libphonenumber repository)
- ext-dom PHP extension
Installing Dev Dependencies
Generated Files
The metadata generation process creates PHP classes in thesrc/data/ directory:
PhoneMetadata class with region-specific rules and patterns.
Metadata Structure
Metadata Loading
The library usesMultiFileMetadataSourceImpl to load metadata on-demand:
Customizing Metadata Version
To build against a different version of Google’s libphonenumber:- Edit
METADATA-VERSION.php:
- Run the build:
Always test thoroughly after changing the metadata version. New versions may include breaking changes or behavioral differences.
Troubleshooting
Build Fails to Clone Repository
Ensure Git is installed and you have network access:Out of Memory Errors
Increase PHP memory limit:Permission Errors
Ensure write permissions for thesrc/data/ directory:
Contributing Metadata Changes
If you discover incorrect metadata:- Do not modify the generated PHP files directly
- Report the issue to Google’s libphonenumber project
- Once fixed upstream, it will be included in the next release
Related Topics
- Performance - Understanding metadata loading and caching
- Lite Version - Using the lightweight version with reduced metadata