Skip to content

Instantly share code, notes, and snippets.

@maarekj maarekj/App\Validator\SameAs.php Secret
Last active Jan 16, 2020

Embed
What would you like to do?
Example using of SameAs
<?php
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
/**
* Constraint for the Unique Entity validator.
*
* @Annotation
* @Target({"PROPERTY", "ANNOTATION"})
*/
class SameAs extends Constraint
{
/** @var string */
public $class;
/** @var string|null */
public $property;
/** @var array|null */
public $targetGroups;
/** {@inheritdoc} */
public function getDefaultOption()
{
return 'class';
}
/** {@inheritdoc} */
public function getRequiredOptions()
{
return ['class'];
}
}
<?php
namespace App\Validator;
use Illuminate\Support\Arr;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
use Symfony\Component\Validator\Mapping\PropertyMetadata;
final class SameAsValidator extends ConstraintValidator
{
/** {@inheritdoc} */
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof SameAs) {
throw new UnexpectedTypeException($constraint, SameAs::class);
}
$class = $constraint->class;
$property = null === $constraint->property ? $this->context->getPropertyName() : $constraint->property;
if (null === $property) {
throw new UnexpectedTypeException($property, 'string');
}
$targetGroups = (array) (null === $constraint->targetGroups ? $this->context->getGroup() : $constraint->targetGroups);
$classMetadata = $this->context->getValidator()->getMetadataFor($class);
if (!$classMetadata instanceof ClassMetadataInterface) {
throw new UnexpectedTypeException($classMetadata, ClassMetadataInterface::class);
}
$propertyMetadatas = $classMetadata->getPropertyMetadata($property);
$constraints = Arr::flatten(\array_map(function (PropertyMetadata $propertyMetadata) {
return $propertyMetadata->getConstraints();
}, $propertyMetadatas), 1);
$this->context->getValidator()->inContext($this->context)->validate($value, $constraints, $targetGroups);
}
}
<?php
use App\Validator\SameAs;
final class EditPost {
/** @var Post */
public $post;
/**
* @var string|null
* @SameAs(class=Post::class, property="slug")
*/
public $slug;
/**
* @var string|null
* @SameAs(class=Post::class, property="content")
*/
public $content;
/**
* @var Category|null
* @SameAs(class=Post::class, property="category")
*/
public $category;
public function __construct(Post $post) {
$this->post = $post;
$this->slug = $post->getSlug();
$this->content = $post->getContent();
$this->category = $post->getCategory();
}
}
@Pixelshaped

This comment has been minimized.

Copy link

Pixelshaped commented Dec 14, 2019

Hi!
Would you care to share the code for your SameAs validator? Seems like a neat way to handle DTOs!
Thanks!

@maarekj

This comment has been minimized.

Copy link
Owner Author

maarekj commented Jan 15, 2020

Hi!
Would you care to share the code for your SameAs validator? Seems like a neat way to handle DTOs!
Thanks!

I just added the SameAs.php and SameAsValidator.php in revision 3 of this gist.

@Pixelshaped

This comment has been minimized.

Copy link

Pixelshaped commented Jan 16, 2020

Well thank you my good sir!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.