Skip to content

Commit 23d7552

Browse files
authored
1.6.2 (#14)
* v1.6.2 [improvement] XML reader refactoring. Implement validations: Choice, Expression * v1.6.2 [statics] Fix psalm, phpstan, cs * v1.6.2 [fix] minor changes * v1.6.2 [improvements] XSD: readable timezones configuration
1 parent 8dac2ee commit 23d7552

File tree

4 files changed

+77
-28
lines changed

4 files changed

+77
-28
lines changed

example/example.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@
8989
</property>
9090
<property name="timezone" type="string">
9191
<validation>
92-
<time_zone intl_compatible="true" country_code="BY" zone="4096"/>
92+
<time_zone intl_compatible="true" country_code="BY" >
93+
<zone value="PER_COUNTRY"/>
94+
</time_zone>
9395
</validation>
9496
</property>
9597
<property name="card_scheme" type="string">

src/Preparation/Processor/Property/Assert/TimeZoneStrategy.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,45 @@ protected function generateArguments(array $config): array
2323

2424
$parent['countryCode'] = $config['country_code'] ?? null;
2525
$parent['intlCompatible'] = $this->stringToBool($config['intl_compatible'] ?? 'false');
26-
$parent['zone'] = (int) ($config['zone'] ?? \DateTimeZone::ALL);
26+
27+
$this->applyTz($parent, $config);
2728

2829
return array_filter($parent);
2930
}
3031

32+
/**
33+
* @param array<string, mixed> $args
34+
* @param array<string, mixed> $config
35+
*
36+
* @return void
37+
*/
38+
protected function applyTz(array &$args, array $config): void
39+
{
40+
$zone = 0;
41+
if (empty($config['zone'])) {
42+
$args['zone'] = \DateTimeZone::ALL;
43+
44+
return;
45+
}
46+
47+
$zones = $config['zone'];
48+
foreach ($zones as $z) {
49+
$zone |= $this->getTzValue($z['value']);
50+
}
51+
52+
$args['zone'] = $zone;
53+
}
54+
55+
protected function getTzValue(string $zoneName): int
56+
{
57+
$constName = sprintf('%s::%s', \DateTimeZone::class, $zoneName);
58+
if (!\defined($constName)) {
59+
throw new \InvalidArgumentException(sprintf('Time zone `%s` is not defined in class %s', $zoneName, \DateTimeZone::class));
60+
}
61+
62+
return \constant($constName);
63+
}
64+
3165
protected function getValidatorProperty(): string
3266
{
3367
return 'time_zone';

src/Resource/schema/dto-1.6.xsd

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -135,31 +135,10 @@
135135
</xs:complexType>
136136
<xs:complexType name="constraint_time_zone">
137137
<xs:complexContent>
138-
<xs:extension base="mic:constraint_abstract" xmlns:mic="micro:dto-1.6">
139-
<xs:attribute name="intl_compatible" type="xs:boolean" default="false" />
140-
<!-- The value of this option must be a valid ISO 3166-1 alpha-2 country code (e.g. CN for China) -->
141-
<xs:attribute name="country_code" type="xs:string" default="" />
142-
<xs:attribute name="zone" default="2047">
143-
<xs:simpleType>
144-
<xs:restriction base="xs:integer">
145-
<!-- \DateTimeZone::{ name } -->
146-
<xs:enumeration value="1" /> <!-- AFRICA -->
147-
<xs:enumeration value="2" /> <!-- AMERICA -->
148-
<xs:enumeration value="4" /> <!-- ANTARCTICA -->
149-
<xs:enumeration value="8" /> <!-- ARCTIC -->
150-
<xs:enumeration value="16" /> <!-- ASIA -->
151-
<xs:enumeration value="32" /> <!-- ATLANTIC -->
152-
<xs:enumeration value="64" /> <!-- AUSTRALIA -->
153-
<xs:enumeration value="128" /> <!-- EUROPE -->
154-
<xs:enumeration value="256" /> <!-- INDIAN -->
155-
<xs:enumeration value="512" /> <!-- PACIFIC -->
156-
<xs:enumeration value="1024" /> <!-- UTC -->
157-
<xs:enumeration value="2047" /> <!-- ALL -->
158-
<xs:enumeration value="4095" /> <!-- ALL_WITH_BC -->
159-
<xs:enumeration value="4096" /> <!-- PER_COUNTRY -->
160-
</xs:restriction>
161-
</xs:simpleType>
162-
</xs:attribute>
138+
<xs:extension base="mic:constraint_abstract_timezone" xmlns:mic="micro:dto-1.6">
139+
<xs:choice>
140+
<xs:element name="zone" type="mic:time_zone" maxOccurs="unbounded" minOccurs="0" />
141+
</xs:choice>
163142
</xs:extension>
164143
</xs:complexContent>
165144
</xs:complexType>
@@ -363,4 +342,36 @@
363342
<xs:attribute name="name" use="required"/>
364343
<xs:attribute name="value" use="required"/>
365344
</xs:complexType>
345+
<xs:complexType name="constraint_abstract_timezone">
346+
<xs:complexContent>
347+
<xs:extension base="mic:constraint_abstract" xmlns:mic="micro:dto-1.6">
348+
<xs:attribute name="intl_compatible" type="xs:boolean" default="false" />
349+
<!-- The value of this option must be a valid ISO 3166-1 alpha-2 country code (e.g. CN for China) -->
350+
<xs:attribute name="country_code" type="xs:string" default="" />
351+
</xs:extension>
352+
</xs:complexContent>
353+
</xs:complexType>
354+
<xs:complexType name="time_zone">
355+
<xs:attribute name="value" use="required">
356+
<xs:simpleType>
357+
<xs:restriction base="xs:string">
358+
<!-- \DateTimeZone::{ name } -->
359+
<xs:enumeration value="AFRICA" />
360+
<xs:enumeration value="AMERICA" />
361+
<xs:enumeration value="ANTARCTICA" />
362+
<xs:enumeration value="ARCTIC" />
363+
<xs:enumeration value="ASIA" />
364+
<xs:enumeration value="ATLANTIC" />
365+
<xs:enumeration value="AUSTRALIA" />
366+
<xs:enumeration value="EUROPE" />
367+
<xs:enumeration value="INDIAN" />
368+
<xs:enumeration value="PACIFIC" />
369+
<xs:enumeration value="UTC" />
370+
<xs:enumeration value="ALL" />
371+
<xs:enumeration value="ALL_WITH_BC" />
372+
<xs:enumeration value="PER_COUNTRY" />
373+
</xs:restriction>
374+
</xs:simpleType>
375+
</xs:attribute>
376+
</xs:complexType>
366377
</xs:schema>

tests/Unit/example.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@
9494
</property>
9595
<property name="timezone" type="string">
9696
<validation>
97-
<time_zone intl_compatible="true" country_code="BY" zone="4096"/>
97+
<time_zone intl_compatible="true" country_code="BY">
98+
<zone value="PER_COUNTRY"/>
99+
</time_zone>
98100
</validation>
99101
</property>
100102
<property name="card_scheme" type="string">

0 commit comments

Comments
 (0)