- How do I use this package for Laravel form validation? For example, validating a country code input.
- Use Laravel’s `Rule::in()` with the enum class. For example, `CountryAlpha2::from($request->country)` will throw an exception if invalid, or integrate it into a Form Request with `Rule::in(CountryAlpha2::class)`. This replaces manual checks like `in_array()` and ensures compile-time safety.
- Will this package work with Laravel 10+ and PHP 8.1+? Are there any known compatibility issues?
- Yes, the package is fully compatible with Laravel 10+ and PHP 8.1+. It has no Laravel-specific dependencies and is stateless, so it integrates seamlessly. However, test thoroughly in your environment, especially if using older Laravel versions with PHP 8.1+ (e.g., Laravel 9 with PHP 8.1).
- Can I store enum values directly in a MySQL database? How should I handle database migrations?
- Store the enum’s `value` (e.g., `country_code` as `VARCHAR(2)`) in the database. Use Laravel’s `casts` property in Eloquent models to automatically convert between the database value and the enum. For example, `protected $casts = ['country_code' => CountryAlpha2::class];` handles the conversion transparently.
- How do I handle cases where a user inputs an invalid ISO code, like 'XX' for a country?
- The enums throw `ValueError` exceptions when invalid codes are passed (e.g., `CountryAlpha2::from('XX')`). Catch these exceptions in your validation logic or use Laravel’s `Rule::exists()` with a custom rule to handle edge cases gracefully. This ensures runtime safety without manual validation.
- Does this package support custom or internal standards (e.g., company-specific country codes)?
- No, this package only provides standardized enums (ISO, IANA, etc.). For custom codes, extend the existing enums or create your own enum class. Avoid conflicts by ensuring your custom codes don’t overlap with the package’s standardized values, which are strictly validated.
- How often are the enums updated, and how can I verify the latest data?
- The enums are updated daily via automated scripts that sync with upstream sources (ISO, IANA, etc.). Check the [GitHub Actions workflows](https://github.com/PrinsFrank/standards/actions) for recent updates. The package also includes a `CHANGELOG.md` and `UPGRADING.md` for breaking changes.
- Is there a performance impact if I use enums extensively, like iterating all countries in a loop?
- Enums are lightweight, but iterating all cases (e.g., `CountryAlpha2::cases()`) can consume memory for large collections. Benchmark in staging if performance is critical. For most use cases (e.g., validation, API responses), the impact is negligible due to lazy loading.
- How do I integrate this with Laravel’s API responses (e.g., JSON:API or REST)?
- Enums implement `JsonSerializable`, so they serialize to their `value` by default. For API responses, return the enum’s value directly (e.g., `return response()->json(['country' => $user->country_code->value])`). Use OpenAPI/Swagger schemas to document enum types (e.g., `type: string, enum: [NL, US, DE]`).
- What alternatives exist for ISO standards in Laravel, and why should I choose this package?
- Alternatives include `league/iso3166`, `symfony/i18n`, or manual arrays. This package stands out for its **PHP 8.1+ enums**, which provide **compile-time safety**, **strong typing**, and **daily updates** without external dependencies. It also integrates natively with Laravel’s validation and Eloquent, reducing boilerplate.
- How can I test my Laravel application to ensure the enums work as expected?
- Test enums in PHPUnit by asserting types (e.g., `assertInstanceOf(CountryAlpha2::class, $country)`) and validating edge cases (e.g., `CountryAlpha2::from('XX')` should throw `ValueError`). Mock enums in unit tests for isolated logic. Use Laravel’s `FreshDatabase` or `DatabaseTransactions` to test database interactions with casted enums.