Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit d751cbe

Browse files
committed
feat(docs): documented Timezone rule
1 parent d339881 commit d751cbe

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

docs/03-rules.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- [Basic Rules](#basic-rules)
44
- [Comparison Rules](#comparison-rules)
5+
- [Date Rules](#date-rules)
56
- [Choice Rules](#choice-rules)
67
- [Other Rules](#other-rules)
78

@@ -18,6 +19,10 @@
1819
- [LessThanOrEqual](03x-rules-less-than-or-equal.md)
1920
- [Range](03x-rules-range.md)
2021

22+
## Date Rules
23+
24+
- [Timezone](03x-rules-timezone.md)
25+
2126
## Choice Rules
2227

2328
- [Choice](03x-rules-choice.md)

docs/03x-rules-timezone.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Timezone
2+
3+
Validates that a value is 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 timezones
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

Comments
 (0)