Skip to content

Instantly share code, notes, and snippets.

@up1
Last active October 29, 2015 11:51
Show Gist options
  • Save up1/fccd26ab0ad5aa3e88f0 to your computer and use it in GitHub Desktop.
Save up1/fccd26ab0ad5aa3e88f0 to your computer and use it in GitHub Desktop.
Demo of Compose method
class ImageValidator{
public function validate($value, Constraint $constraint){
if (null === $value || '' === $value) {
return;
}
if (null === $constraint->minWidth && null === $constraint->maxWidth) {
return;
}
$size = @getimagesize($value);
if (empty($size) || ($size[0] === 0) || ($size[1] === 0)) {
$this->context->addViolation($constraint->sizeNotDetectedMessage);
return;
}
$width = $size[0];
$height = $size[1];
if ($constraint->minWidth) {
if (!ctype_digit((string) $constraint->minWidth)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width', $constraint->minWidth));
}
if ($width < $constraint->minWidth) {
$this->context->addViolation($constraint->minWidthMessage, array(
'{{ width }}' => $width,
'{{ min_width }}' => $constraint->minWidth
));
return;
}
}
if ($constraint->maxWidth) {
if (!ctype_digit((string) $constraint->maxWidth)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width', $constraint->maxWidth));
}
if ($width > $constraint->maxWidth) {
$this->context->addViolation($constraint->maxWidthMessage, array(
'{{ width }}' => $width,
'{{ max_width }}' => $constraint->maxWidth
));
return;
}
}
}
}
class ImageValidator{
public function validate($value, Constraint $constraint){
if ( ! $this->isInputTypeValid($value)) {
return;
}
if ( ! $this->isImage($constraint)) {
return;
}
$this->validateImageSize($value, $constraint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment