Skip to content

Commit b329fa2

Browse files
authored
Add attribute support, fix uppercase issue with getter (#50)
1 parent c0944ec commit b329fa2

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

src/ZipCodeValidator/Constraints/ZipCode.php

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

33
namespace ZipCodeValidator\Constraints;
44

5+
use Attribute;
56
use Symfony\Component\Validator\Constraint;
67
use Symfony\Component\Validator\Exception\MissingOptionsException;
78

@@ -10,6 +11,7 @@
1011
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
1112
* @author Martin Schindler
1213
*/
14+
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD)]
1315
class ZipCode extends Constraint
1416
{
1517
public string $message = 'This value is not a valid ZIP code.';
@@ -18,15 +20,15 @@ class ZipCode extends Constraint
1820
public bool $strict = true;
1921
public bool $caseSensitiveCheck = true;
2022

21-
public function __construct(mixed $options = null)
23+
public function __construct(mixed $options = null, array $groups = null, mixed $payload = null)
2224
{
23-
if (null !== $options && !is_array($options)) {
25+
if (is_string($options)) {
2426
$options = array(
2527
'iso' => $options
2628
);
2729
}
2830

29-
parent::__construct($options);
31+
parent::__construct($options, $groups, $payload);
3032

3133
if (null === $this->iso && null === $this->getter) {
3234
throw new MissingOptionsException(sprintf('Either the option "iso" or "getter" must be given for constraint %s', __CLASS__), ['iso', 'getter']);

src/ZipCodeValidator/Constraints/ZipCodeValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function validate($value, Constraint $constraint): void
235235
throw new ConstraintDefinitionException(sprintf($message, $getter, get_class($object)));
236236
}
237237

238-
$iso = $object->$getter();
238+
$iso = strtoupper($object->$getter() ?? '');
239239
}
240240

241241
if(empty($iso)){

0 commit comments

Comments
 (0)