Skip to content

Commit 1fe2f9c

Browse files
committed
Factorize code a bit
1 parent 90e684c commit 1fe2f9c

1 file changed

Lines changed: 16 additions & 36 deletions

File tree

src/ZipCodeValidator/Constraints/ZipCode.php

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,13 @@ public function __construct(
3333
)
3434
{
3535
if (is_string($options)) {
36-
$options = [
37-
'iso' => $options,
38-
];
36+
$options = ['iso' => $options];
3937
} elseif (null === $options) {
4038
$options = [];
4139
} elseif (!is_array($options)) {
4240
throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint "%s".', 'options', __CLASS__), ['options']);
4341
}
4442

45-
$availableOptions = ['iso', 'getter', 'strict', 'caseSensitiveCheck', 'message', 'groups', 'payload'];
46-
$invalidOptions = array_values(array_filter(array_keys($options), fn ($option) => !in_array($option, $availableOptions, true)));
47-
if ([] !== $invalidOptions) {
48-
throw new InvalidOptionsException(
49-
sprintf('The options "%s" do not exist in constraint "%s".', implode('", "', $invalidOptions), __CLASS__),
50-
$invalidOptions
51-
);
52-
}
53-
5443
$resolvedOptions = [
5544
'iso' => $iso,
5645
'getter' => $getter,
@@ -61,6 +50,14 @@ public function __construct(
6150
'payload' => $payload,
6251
];
6352

53+
$invalidOptions = array_values(array_filter(array_keys($options), fn ($option) => !in_array($option, array_keys($resolvedOptions), true)));
54+
if ([] !== $invalidOptions) {
55+
throw new InvalidOptionsException(
56+
sprintf('The options "%s" do not exist in constraint "%s".', implode('", "', $invalidOptions), __CLASS__),
57+
$invalidOptions
58+
);
59+
}
60+
6461
foreach ($resolvedOptions as $option => $resolvedValue) {
6562
if (null !== $resolvedValue || !array_key_exists($option, $options)) {
6663
continue;
@@ -69,37 +66,20 @@ public function __construct(
6966
$resolvedOptions[$option] = 'groups' === $option ? (array) $options[$option] : $options[$option];
7067
}
7168

72-
$iso = $resolvedOptions['iso'];
73-
$getter = $resolvedOptions['getter'];
74-
$strict = $resolvedOptions['strict'];
75-
$caseSensitiveCheck = $resolvedOptions['caseSensitiveCheck'];
76-
$message = $resolvedOptions['message'];
77-
7869
parent::__construct(null, $resolvedOptions['groups'], $resolvedOptions['payload']);
7970

80-
if (null !== $iso) {
81-
$this->iso = $iso;
82-
}
83-
84-
if (null !== $getter) {
85-
$this->getter = $getter;
86-
}
87-
88-
if (null !== $strict) {
89-
$this->strict = $strict;
90-
}
71+
unset($resolvedOptions['groups'], $resolvedOptions['payload']);
9172

92-
if (null !== $caseSensitiveCheck) {
93-
$this->caseSensitiveCheck = $caseSensitiveCheck;
94-
}
95-
96-
if (null !== $message) {
97-
$this->message = $message;
73+
foreach ($resolvedOptions as $option => $resolvedValue) {
74+
if (null === $resolvedValue) {
75+
continue;
76+
}
77+
78+
$this->{$option} = $resolvedValue;
9879
}
9980

10081
if (null === $this->iso && null === $this->getter) {
10182
throw new MissingOptionsException(sprintf('Either the option "iso" or "getter" must be given for constraint %s', __CLASS__), ['iso', 'getter']);
10283
}
10384
}
104-
10585
}

0 commit comments

Comments
 (0)