Skip to content

Commit

Permalink
1.6.2 (#14)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Asisyas committed Feb 17, 2023
1 parent 8dac2ee commit 23d7552
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 28 deletions.
4 changes: 3 additions & 1 deletion example/example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@
</property>
<property name="timezone" type="string">
<validation>
<time_zone intl_compatible="true" country_code="BY" zone="4096"/>
<time_zone intl_compatible="true" country_code="BY" >
<zone value="PER_COUNTRY"/>
</time_zone>
</validation>
</property>
<property name="card_scheme" type="string">
Expand Down
36 changes: 35 additions & 1 deletion src/Preparation/Processor/Property/Assert/TimeZoneStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,45 @@ protected function generateArguments(array $config): array

$parent['countryCode'] = $config['country_code'] ?? null;
$parent['intlCompatible'] = $this->stringToBool($config['intl_compatible'] ?? 'false');
$parent['zone'] = (int) ($config['zone'] ?? \DateTimeZone::ALL);

$this->applyTz($parent, $config);

return array_filter($parent);
}

/**
* @param array<string, mixed> $args
* @param array<string, mixed> $config
*
* @return void
*/
protected function applyTz(array &$args, array $config): void
{
$zone = 0;
if (empty($config['zone'])) {
$args['zone'] = \DateTimeZone::ALL;

return;
}

$zones = $config['zone'];
foreach ($zones as $z) {
$zone |= $this->getTzValue($z['value']);
}

$args['zone'] = $zone;
}

protected function getTzValue(string $zoneName): int
{
$constName = sprintf('%s::%s', \DateTimeZone::class, $zoneName);
if (!\defined($constName)) {
throw new \InvalidArgumentException(sprintf('Time zone `%s` is not defined in class %s', $zoneName, \DateTimeZone::class));
}

return \constant($constName);
}

protected function getValidatorProperty(): string
{
return 'time_zone';
Expand Down
61 changes: 36 additions & 25 deletions src/Resource/schema/dto-1.6.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -135,31 +135,10 @@
</xs:complexType>
<xs:complexType name="constraint_time_zone">
<xs:complexContent>
<xs:extension base="mic:constraint_abstract" xmlns:mic="micro:dto-1.6">
<xs:attribute name="intl_compatible" type="xs:boolean" default="false" />
<!-- The value of this option must be a valid ISO 3166-1 alpha-2 country code (e.g. CN for China) -->
<xs:attribute name="country_code" type="xs:string" default="" />
<xs:attribute name="zone" default="2047">
<xs:simpleType>
<xs:restriction base="xs:integer">
<!-- \DateTimeZone::{ name } -->
<xs:enumeration value="1" /> <!-- AFRICA -->
<xs:enumeration value="2" /> <!-- AMERICA -->
<xs:enumeration value="4" /> <!-- ANTARCTICA -->
<xs:enumeration value="8" /> <!-- ARCTIC -->
<xs:enumeration value="16" /> <!-- ASIA -->
<xs:enumeration value="32" /> <!-- ATLANTIC -->
<xs:enumeration value="64" /> <!-- AUSTRALIA -->
<xs:enumeration value="128" /> <!-- EUROPE -->
<xs:enumeration value="256" /> <!-- INDIAN -->
<xs:enumeration value="512" /> <!-- PACIFIC -->
<xs:enumeration value="1024" /> <!-- UTC -->
<xs:enumeration value="2047" /> <!-- ALL -->
<xs:enumeration value="4095" /> <!-- ALL_WITH_BC -->
<xs:enumeration value="4096" /> <!-- PER_COUNTRY -->
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:extension base="mic:constraint_abstract_timezone" xmlns:mic="micro:dto-1.6">
<xs:choice>
<xs:element name="zone" type="mic:time_zone" maxOccurs="unbounded" minOccurs="0" />
</xs:choice>
</xs:extension>
</xs:complexContent>
</xs:complexType>
Expand Down Expand Up @@ -363,4 +342,36 @@
<xs:attribute name="name" use="required"/>
<xs:attribute name="value" use="required"/>
</xs:complexType>
<xs:complexType name="constraint_abstract_timezone">
<xs:complexContent>
<xs:extension base="mic:constraint_abstract" xmlns:mic="micro:dto-1.6">
<xs:attribute name="intl_compatible" type="xs:boolean" default="false" />
<!-- The value of this option must be a valid ISO 3166-1 alpha-2 country code (e.g. CN for China) -->
<xs:attribute name="country_code" type="xs:string" default="" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="time_zone">
<xs:attribute name="value" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<!-- \DateTimeZone::{ name } -->
<xs:enumeration value="AFRICA" />
<xs:enumeration value="AMERICA" />
<xs:enumeration value="ANTARCTICA" />
<xs:enumeration value="ARCTIC" />
<xs:enumeration value="ASIA" />
<xs:enumeration value="ATLANTIC" />
<xs:enumeration value="AUSTRALIA" />
<xs:enumeration value="EUROPE" />
<xs:enumeration value="INDIAN" />
<xs:enumeration value="PACIFIC" />
<xs:enumeration value="UTC" />
<xs:enumeration value="ALL" />
<xs:enumeration value="ALL_WITH_BC" />
<xs:enumeration value="PER_COUNTRY" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:schema>
4 changes: 3 additions & 1 deletion tests/Unit/example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@
</property>
<property name="timezone" type="string">
<validation>
<time_zone intl_compatible="true" country_code="BY" zone="4096"/>
<time_zone intl_compatible="true" country_code="BY">
<zone value="PER_COUNTRY"/>
</time_zone>
</validation>
</property>
<property name="card_scheme" type="string">
Expand Down

0 comments on commit 23d7552

Please sign in to comment.