From 5453417e5b6442f7a339e6240565fd5fdb6c6497 Mon Sep 17 00:00:00 2001 From: Divine <48183131+divine@users.noreply.github.com> Date: Thu, 27 Jul 2023 01:51:21 +0300 Subject: [PATCH 1/5] fix: UniqueEntity::$service is considered final --- Validator/Constraints/Unique.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Validator/Constraints/Unique.php b/Validator/Constraints/Unique.php index 5b5b6a92..441d5eaa 100644 --- a/Validator/Constraints/Unique.php +++ b/Validator/Constraints/Unique.php @@ -16,6 +16,11 @@ #[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] class Unique extends UniqueEntity { - /** @var string */ - public $service = 'doctrine_odm.mongodb.unique'; + /** + * The validator must be defined as a service with this name. + */ + public function validatedBy(): string + { + return 'doctrine_odm.mongodb.unique'; + } } From c3ba15b79b0c3f4c0585217a67f3004f7b85a1bc Mon Sep 17 00:00:00 2001 From: Divine <48183131+divine@users.noreply.github.com> Date: Thu, 27 Jul 2023 18:55:08 +0300 Subject: [PATCH 2/5] fix: review --- Validator/Constraints/Unique.php | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/Validator/Constraints/Unique.php b/Validator/Constraints/Unique.php index 441d5eaa..ad6337f3 100644 --- a/Validator/Constraints/Unique.php +++ b/Validator/Constraints/Unique.php @@ -17,10 +17,34 @@ class Unique extends UniqueEntity { /** - * The validator must be defined as a service with this name. + * @param array|string $fields The combination of fields that must contain unique values or a set of options + * @param bool|array|string $ignoreNull The combination of fields that ignore null values */ - public function validatedBy(): string - { - return 'doctrine_odm.mongodb.unique'; + public function __construct( + $fields, + string $message = null, + string $service = null, + string $em = null, + string $entityClass = null, + string $repositoryMethod = null, + string $errorPath = null, + bool|string|array $ignoreNull = null, + array $groups = null, + $payload = null, + array $options = [] + ) { + parent::__construct( + $fields, + $message, + 'doctrine_odm.mongodb.unique', + $em, + $entityClass, + $repositoryMethod, + $errorPath, + $ignoreNull, + $groups, + $payload, + $options + ); } } From 4a8f1a48330ab9318b2dccfc62194229a095259c Mon Sep 17 00:00:00 2001 From: Divine <48183131+divine@users.noreply.github.com> Date: Fri, 28 Jul 2023 16:08:29 +0300 Subject: [PATCH 3/5] Update Validator/Constraints/Unique.php Co-authored-by: Fran Moreno --- Validator/Constraints/Unique.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Validator/Constraints/Unique.php b/Validator/Constraints/Unique.php index ad6337f3..6fffd7a0 100644 --- a/Validator/Constraints/Unique.php +++ b/Validator/Constraints/Unique.php @@ -23,7 +23,7 @@ class Unique extends UniqueEntity public function __construct( $fields, string $message = null, - string $service = null, + string $service = 'doctrine_odm.mongodb.unique', string $em = null, string $entityClass = null, string $repositoryMethod = null, @@ -36,7 +36,7 @@ public function __construct( parent::__construct( $fields, $message, - 'doctrine_odm.mongodb.unique', + $service, $em, $entityClass, $repositoryMethod, From bee93a3162f58eab763ba5bdbd9a00f3cebd9e68 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Sat, 29 Jul 2023 09:40:38 +0200 Subject: [PATCH 4/5] Add UniqueTest --- Tests/Validator/Constraints/UniqueTest.php | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Tests/Validator/Constraints/UniqueTest.php diff --git a/Tests/Validator/Constraints/UniqueTest.php b/Tests/Validator/Constraints/UniqueTest.php new file mode 100644 index 00000000..8931700c --- /dev/null +++ b/Tests/Validator/Constraints/UniqueTest.php @@ -0,0 +1,43 @@ += 80000) { + $loader = new AnnotationLoader(); + } else { + $loader = new AnnotationLoader(new AnnotationReader()); + } + + self::assertTrue($loader->loadClassMetadata($metadata)); + + [$constraint] = $metadata->getConstraints(); + assert($constraint instanceof Unique); + self::assertSame(['email'], $constraint->fields); + self::assertSame('doctrine_odm.mongodb.unique', $constraint->validatedBy()); + } +} + +/** @Unique(fields={"email"}) */ +#[Unique(['email'])] +class UniqueDocumentDummyOne +{ + private string $email; +} From 3320f0646504f2a07d615b07bfb127dc0e4efaf8 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Sun, 6 Aug 2023 16:35:01 +0200 Subject: [PATCH 5/5] Fix CS --- Validator/Constraints/Unique.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Validator/Constraints/Unique.php b/Validator/Constraints/Unique.php index 6fffd7a0..81fa0ad6 100644 --- a/Validator/Constraints/Unique.php +++ b/Validator/Constraints/Unique.php @@ -17,19 +17,20 @@ class Unique extends UniqueEntity { /** - * @param array|string $fields The combination of fields that must contain unique values or a set of options - * @param bool|array|string $ignoreNull The combination of fields that ignore null values + * @param string[]|string $fields The combination of fields that must contain unique values or a set of options + * @param bool|string[]|string $ignoreNull The combination of fields that ignore null values + * @param mixed $payload */ public function __construct( $fields, - string $message = null, + ?string $message = null, string $service = 'doctrine_odm.mongodb.unique', - string $em = null, - string $entityClass = null, - string $repositoryMethod = null, - string $errorPath = null, - bool|string|array $ignoreNull = null, - array $groups = null, + ?string $em = null, + ?string $entityClass = null, + ?string $repositoryMethod = null, + ?string $errorPath = null, + $ignoreNull = null, + ?array $groups = null, $payload = null, array $options = [] ) { @@ -44,7 +45,7 @@ public function __construct( $ignoreNull, $groups, $payload, - $options + $options, ); } }