-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] deprecate member metadata accessors #11703
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 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…property metadata accessors add changelog for deprecations fix test
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,10 @@ | |
use Symfony\Component\Form\Guess\Guess; | ||
use Symfony\Component\Form\Guess\TypeGuess; | ||
use Symfony\Component\Form\Guess\ValueGuess; | ||
use Symfony\Component\Validator\MetadataFactoryInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Mapping\ClassMetadataInterface; | ||
use Symfony\Component\Validator\Mapping\GenericMetadata; | ||
use Symfony\Component\Validator\MetadataFactoryInterface; | ||
|
||
class ValidatorTypeGuesser implements FormTypeGuesserInterface | ||
{ | ||
|
@@ -264,10 +266,14 @@ protected function guess($class, $property, \Closure $closure, $defaultValue = n | |
$guesses = array(); | ||
$classMetadata = $this->metadataFactory->getMetadataFor($class); | ||
|
||
if ($classMetadata->hasMemberMetadatas($property)) { | ||
$memberMetadatas = $classMetadata->getMemberMetadatas($property); | ||
if ($classMetadata instanceof ClassMetadataInterface && $classMetadata->hasPropertyMetadata($property)) { | ||
$memberMetadatas = $classMetadata->getPropertyMetadata($property); | ||
|
||
foreach ($memberMetadatas as $memberMetadata) { | ||
if (!$memberMetadata instanceof GenericMetadata) { | ||
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. in Symfony 2.4 and lower, |
||
continue; | ||
} | ||
|
||
$constraints = $memberMetadata->getConstraints(); | ||
|
||
foreach ($constraints as $constraint) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,7 +160,7 @@ public function accept(ValidationVisitorInterface $visitor, $value, $group, $pro | |
$pathPrefix = empty($propertyPath) ? '' : $propertyPath.'.'; | ||
|
||
foreach ($this->getConstrainedProperties() as $property) { | ||
foreach ($this->getMemberMetadatas($property) as $member) { | ||
foreach ($this->getPropertyMetadata($property) as $member) { | ||
$member->accept($visitor, $member->getPropertyValue($value), $group, $pathPrefix.$property, $propagatedGroup); | ||
} | ||
} | ||
|
@@ -268,7 +268,7 @@ public function addPropertyConstraint($property, Constraint $constraint) | |
if (!isset($this->properties[$property])) { | ||
$this->properties[$property] = new PropertyMetadata($this->getClassName(), $property); | ||
|
||
$this->addMemberMetadata($this->properties[$property]); | ||
$this->addPropertyMetadata($this->properties[$property]); | ||
} | ||
|
||
$constraint->addImplicitGroupName($this->getDefaultGroup()); | ||
|
@@ -294,7 +294,7 @@ public function addGetterConstraint($property, Constraint $constraint) | |
if (!isset($this->getters[$property])) { | ||
$this->getters[$property] = new GetterMetadata($this->getClassName(), $property); | ||
|
||
$this->addMemberMetadata($this->getters[$property]); | ||
$this->addPropertyMetadata($this->getters[$property]); | ||
} | ||
|
||
$constraint->addImplicitGroupName($this->getDefaultGroup()); | ||
|
@@ -316,16 +316,18 @@ public function mergeConstraints(ClassMetadata $source) | |
} | ||
|
||
foreach ($source->getConstrainedProperties() as $property) { | ||
foreach ($source->getMemberMetadatas($property) as $member) { | ||
foreach ($source->getPropertyMetadata($property) as $member) { | ||
$member = clone $member; | ||
|
67ED
||
foreach ($member->getConstraints() as $constraint) { | ||
$constraint->addImplicitGroupName($this->getDefaultGroup()); | ||
if ($member instanceof GenericMetadata) { | ||
foreach ($member->getConstraints() as $constraint) { | ||
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. @webmozart should 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. Yes I think that's a good change. @Tobion can you add this? 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. Yes. In the future we would also need to replace typehints to ClassMetadata with ClassMetadataInterface. At this moment, also 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. Done. |
||
$constraint->addImplicitGroupName($this->getDefaultGroup()); | ||
} | ||
} | ||
|
||
$this->addMemberMetadata($member); | ||
$this->addPropertyMetadata($member); | ||
|
||
if (!$member->isPrivate($this->name)) { | ||
if ($member instanceof MemberMetadata && !$member->isPrivate($this->name)) { | ||
$property = $member->getPropertyName(); | ||
|
||
if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) { | ||
|
@@ -342,12 +344,12 @@ public function mergeConstraints(ClassMetadata $source) | |
* Adds a member metadata. | ||
* | ||
* @param MemberMetadata $metadata | ||
* | ||
* @deprecated Deprecated since version 2.6, to be removed in 3.0. | ||
*/ | ||
protected function addMemberMetadata(MemberMetadata $metadata) | ||
{ | ||
$property = $metadata->getPropertyName(); | ||
|
||
$this->members[$property][] = $metadata; | ||
$this->addPropertyMetadata($metadata); | ||
} | ||
|
||
/** | ||
|
@@ -356,10 +358,12 @@ protected function addMemberMetadata(MemberMetadata $metadata) | |
* @param string $property The name of the property | ||
* | ||
* @return bool | ||
* | ||
* @deprecated Deprecated since version 2.6, to be removed in 3.0. Use {@link hasPropertyMetadata} instead. | ||
*/ | ||
public function hasMemberMetadatas($property) | ||
{ | ||
return array_key_exists($property, $this->members); | ||
return $this->hasPropertyMetadata($property); | ||
} | ||
|
||
/** | ||
|
@@ -368,14 +372,12 @@ public function hasMemberMetadatas($property) | |
* @param string $property The name of the property | ||
* | ||
* @return MemberMetadata[] An array of MemberMetadata | ||
* | ||
* @deprecated Deprecated since version 2.6, to be removed in 3.0. Use {@link getPropertyMetadata} instead. | ||
*/ | ||
public function getMemberMetadatas($property) | ||
{ | ||
if (!isset($this->members[$property])) { | ||
return array(); | ||
} | ||
|
||
return $this->members[$property]; | ||
return $this->getPropertyMetadata($property); | ||
} | ||
|
||
/** | ||
|
@@ -505,4 +507,16 @@ public function getCascadingStrategy() | |
{ | ||
return CascadingStrategy::NONE; | ||
} | ||
|
||
/** | ||
* Adds a property metadata. | ||
* | ||
* @param PropertyMetadataInterface $metadata | ||
*/ | ||
private function addPropertyMetadata(PropertyMetadataInterface $metadata) | ||
{ | ||
$property = $metadata->getPropertyName(); | ||
|
||
$this->members[$property][] = $metadata; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the metadata for a class should always be a ClassMetadataInterface so I don't think this check is necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's necessary because the interface only says it returns an MetadataInterface. Everything else is implementation detail we cannot trust.