|
| 1 | +# Timezone |
| 2 | + |
| 3 | +Validates that a value is a valid timezone identifier. |
| 4 | + |
| 5 | +```php |
| 6 | +Timezone( |
| 7 | + string $timezoneGroup = \DateTimeZone::ALL, |
| 8 | + ?string $countryCode = null, |
| 9 | + string $message = 'The "{{ name }}" value is not a valid timezone, "{{ value }}" given.' |
| 10 | +); |
| 11 | +``` |
| 12 | + |
| 13 | +## Basic Usage |
| 14 | + |
| 15 | +```php |
| 16 | +// All timezone identifiers |
| 17 | +Validator::timezone()->validate('Europe/Lisbon'); // true |
| 18 | + |
| 19 | +// Restrict timezone identifiers to a specific geographical zone |
| 20 | +Validator::timezone(timezoneGroup: \DateTimeZone::EUROPE)->validate('Europe/Lisbon'); // true |
| 21 | +Validator::timezone(timezoneGroup: \DateTimeZone::AFRICA)->validate('Europe/Lisbon'); // false |
| 22 | +// Or multiple geographical zones |
| 23 | +Validator::timezone(timezoneGroup: \DateTimeZone::AFRICA | \DateTimeZone::EUROPE)->validate('Europe/Lisbon'); // true |
| 24 | + |
| 25 | +// Restrict timezone identifiers to a specific country |
| 26 | +Validator::timezone(timezoneGroup: \DateTimeZone::PER_COUNTRY, countryCode: 'pt')->validate('Europe/Lisbon'); // true |
| 27 | +Validator::timezone(timezoneGroup: \DateTimeZone::PER_COUNTRY, countryCode: 'en')->validate('Europe/Lisbon'); // false |
| 28 | +``` |
| 29 | + |
| 30 | +> **Note** |
| 31 | +> An `UnexpectedValueException` will be thrown when the `timezoneGroup` value is `\DateTimeZone::PER_COUNTRY` |
| 32 | +> and the `countryCode` value is `null` (not provided). |
| 33 | +
|
| 34 | +> **Note** |
| 35 | +> An `UnexpectedValueException` will be thrown when the `countryCode` value is not valid. |
| 36 | +> Only if the `timezoneGroup` value is `\DateTimeZone::PER_COUNTRY`, otherwise it is ignored. |
| 37 | +
|
| 38 | +## Options |
| 39 | + |
| 40 | +### `timezoneGroup` |
| 41 | + |
| 42 | +type: `int` default: `\DateTimeZone::ALL` |
| 43 | + |
| 44 | +Set this option to restrict timezone identifiers to a specific geographical zone. |
| 45 | + |
| 46 | +Available timezone groups: |
| 47 | + |
| 48 | +- `\DateTimeZone::AFRICA` |
| 49 | +- `\DateTimeZone::AMERICA` |
| 50 | +- `\DateTimeZone::ANTARCTICA` |
| 51 | +- `\DateTimeZone::ARCTIC` |
| 52 | +- `\DateTimeZone::ASIA` |
| 53 | +- `\DateTimeZone::ATLANTIC` |
| 54 | +- `\DateTimeZone::AUSTRALIA` |
| 55 | +- `\DateTimeZone::EUROPE` |
| 56 | +- `\DateTimeZone::INDIAN` |
| 57 | +- `\DateTimeZone::PACIFIC` |
| 58 | + |
| 59 | +In addition, there are special timezone groups: |
| 60 | + |
| 61 | +- `\DateTimeZone::ALL` all timezones; |
| 62 | +- `\DateTimeZone::ALL_WITH_BC` all timezones including deprecated timezones; |
| 63 | +- `\DateTimeZone::PER_COUNTRY` timezones per country (must be used together with the [`countryCode`](#countrycode) option); |
| 64 | +- `\DateTimeZone::UTC` UTC timezones. |
| 65 | + |
| 66 | +### `countryCode` |
| 67 | + |
| 68 | +type: `?string` default: `null` |
| 69 | + |
| 70 | +If the `timezoneGroup` option value is `\DateTimeZone::PER_COUNTRY`, |
| 71 | +this option is required to restrict valid timezone identifiers to the ones that belong to the given country. |
| 72 | + |
| 73 | +Must be a valid alpha-2 country code. |
| 74 | +Check the [official country codes](https://en.wikipedia.org/wiki/ISO_3166-1#Current_codes) list for more information. |
| 75 | + |
| 76 | +### `message` |
| 77 | + |
| 78 | +type `string` default: `The "{{ name }}" value is not a valid timezone, "{{ value }}" given.` |
| 79 | + |
| 80 | +Message that will be shown if the input value is not a valid timezone. |
| 81 | + |
| 82 | +The following parameters are available: |
| 83 | + |
| 84 | +| Parameter | Description | |
| 85 | +|---------------------|---------------------------| |
| 86 | +| `{{ value }}` | The current invalid value | |
| 87 | +| `{{ name }}` | Name of the invalid value | |
| 88 | +| `{{ countryCode }}` | Selected country code | |
0 commit comments