diff vendor/symfony/validator/Constraint.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
line wrap: on
line diff
--- a/vendor/symfony/validator/Constraint.php	Thu Feb 28 13:11:55 2019 +0000
+++ b/vendor/symfony/validator/Constraint.php	Thu May 09 15:34:47 2019 +0100
@@ -105,6 +105,7 @@
      */
     public function __construct($options = null)
     {
+        $defaultOption = $this->getDefaultOption();
         $invalidOptions = [];
         $missingOptions = array_flip((array) $this->getRequiredOptions());
         $knownOptions = get_object_vars($this);
@@ -112,8 +113,12 @@
         // The "groups" option is added to the object lazily
         $knownOptions['groups'] = true;
 
-        if (\is_array($options) && \count($options) >= 1 && isset($options['value']) && !property_exists($this, 'value')) {
-            $options[$this->getDefaultOption()] = $options['value'];
+        if (\is_array($options) && isset($options['value']) && !property_exists($this, 'value')) {
+            if (null === $defaultOption) {
+                throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', \get_class($this)));
+            }
+
+            $options[$defaultOption] = $options['value'];
             unset($options['value']);
         }
 
@@ -122,7 +127,7 @@
         }
         if ($options && \is_array($options) && \is_string(key($options))) {
             foreach ($options as $option => $value) {
-                if (array_key_exists($option, $knownOptions)) {
+                if (\array_key_exists($option, $knownOptions)) {
                     $this->$option = $value;
                     unset($missingOptions[$option]);
                 } else {
@@ -130,26 +135,24 @@
                 }
             }
         } elseif (null !== $options && !(\is_array($options) && 0 === \count($options))) {
-            $option = $this->getDefaultOption();
-
-            if (null === $option) {
-                throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint %s', \get_class($this)));
+            if (null === $defaultOption) {
+                throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', \get_class($this)));
             }
 
-            if (array_key_exists($option, $knownOptions)) {
-                $this->$option = $options;
-                unset($missingOptions[$option]);
+            if (\array_key_exists($defaultOption, $knownOptions)) {
+                $this->$defaultOption = $options;
+                unset($missingOptions[$defaultOption]);
             } else {
-                $invalidOptions[] = $option;
+                $invalidOptions[] = $defaultOption;
             }
         }
 
         if (\count($invalidOptions) > 0) {
-            throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint %s', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions);
+            throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint "%s".', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions);
         }
 
         if (\count($missingOptions) > 0) {
-            throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint %s', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions));
+            throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint "%s".', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions));
         }
     }
 
@@ -173,7 +176,7 @@
             return;
         }
 
-        throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]);
+        throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, \get_class($this)), [$option]);
     }
 
     /**
@@ -199,7 +202,7 @@
             return $this->groups;
         }
 
-        throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]);
+        throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, \get_class($this)), [$option]);
     }
 
     /**
@@ -229,12 +232,13 @@
      *
      * Override this method to define a default option.
      *
-     * @return string
+     * @return string|null
      *
      * @see __construct()
      */
     public function getDefaultOption()
     {
+        return null;
     }
 
     /**
@@ -256,7 +260,7 @@
      *
      * By default, this is the fully qualified name of the constraint class
      * suffixed with "Validator". You can override this method to change that
-     * behaviour.
+     * behavior.
      *
      * @return string
      */