-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Fixed group handling in composite constraints #11505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ | |
* | ||
* Constraint instances are immutable and serializable. | ||
* | ||
* @property array $groups The groups that the constraint belongs to | ||
* | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
* | ||
* @api | ||
|
@@ -48,11 +50,6 @@ abstract class Constraint | |
*/ | ||
const PROPERTY_CONSTRAINT = 'property'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $groups = array(self::DEFAULT_GROUP); | ||
|
||
/** | ||
* Initializes the constraint with options. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why this was changed. Makes it a little inconsistent between groups option and all the rest which are real properties. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because we need a way to determine whether the
So I went with this solution. |
||
* | ||
|
@@ -86,6 +83,10 @@ public function __construct($options = null) | |
{ | ||
$invalidOptions = array(); | ||
$missingOptions = array_flip((array) $this->getRequiredOptions()); | ||
$knownOptions = get_object_vars($this); | ||
|
||
// 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']; | ||
|
@@ -94,14 +95,14 @@ public function __construct($options = null) | |
|
||
if (is_array($options) && count($options) > 0 && is_string(key($options))) { | ||
foreach ($options as $option => $value) { | ||
if (property_exists($this, $option)) { | ||
if (array_key_exists($option, $knownOptions)) { | ||
$this->$option = $value; | ||
unset($missingOptions[$option]); | ||
} else { | ||
$invalidOptions[] = $option; | ||
} | ||
} | ||
} elseif (null !== $options && ! (is_array($options) && count($options) === 0)) { | ||
} elseif (null !== $options && !(is_array($options) && count($options) === 0)) { | ||
$option = $this->getDefaultOption(); | ||
|
||
if (null === $option) { | ||
|
@@ -110,7 +111,7 @@ public function __construct($options = null) | |
); | ||
} | ||
|
||
if (property_exists($this, $option)) { | ||
if (array_key_exists($option, $knownOptions)) { | ||
$this->$option = $options; | ||
unset($missingOptions[$option]); | ||
} else { | ||
|
@@ -131,15 +132,56 @@ public function __construct($options = null) | |
array_keys($missingOptions) | ||
); | ||
} | ||
|
||
$this->groups = (array) $this->groups; | ||
} | ||
|
||
/** | ||
* Unsupported operation. | ||
* Sets the value of a lazily initialized option. | ||
* | ||
* Corresponding properties are added to the object on first access. Hence | ||
* this method will be called at most once per constraint instance and | ||
* option name. | ||
* | ||
* @param string $option The option name | ||
* @param mixed $value The value to set | ||
* | ||
* @throws InvalidOptionsException If an invalid option name is given | ||
*/ | ||
public function __set($option, $value) | ||
{ | ||
if ('groups' === $option) { | ||
$this->groups = (array) $value; | ||
|
||
return; | ||
} | ||
|
||
throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, get_class($this)), array($option)); | ||
} | ||
|
||
/** | ||
* Returns the value of a lazily initialized option. | ||
* | ||
* Corresponding properties are added to the object on first access. Hence | ||
* this method will be called at most once per constraint instance and | ||
* option name. | ||
* | ||
* @param string $option The option name | ||
* | ||
* @return mixed The value of the option | ||
* | ||
* @throws InvalidOptionsException If an invalid option name is given | ||
* | ||
* @internal This method should not be used or overwritten in userland code. | ||
* | ||
* @since 2.6 | ||
*/ | ||
public function __get($option) | ||
{ | ||
if ('groups' === $option) { | ||
$this->groups = array(self::DEFAULT_GROUP); | ||
|
||
return $this->groups; | ||
} | ||
|
||
throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, get_class($this)), array($option)); | ||
} | ||
|
||
|
@@ -217,4 +259,23 @@ public function getTargets() | |
{ | ||
return self::PROPERTY_CONSTRAINT; | ||
} | ||
|
||
/** | ||
* Optimizes the serialized value to minimize storage space. | ||
* | ||
* @return array The properties to serialize | ||
* | ||
* @internal This method may be replaced by an implementation of | ||
* {@link \Serializable} in the future. Please don't use or | ||
* overwrite it. | ||
* | ||
* @since 2.6 | ||
*/ | ||
public function __sleep() | ||
{ | ||
// Initialize "groups" option if it is not set | ||
$this->__get('groups'); | ||
|
||
return array_keys(get_object_vars($this)); | ||
} | ||
} |
< 8000 div class="d-flex flex-items-center">