/**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if ($value instanceof FSiFile) {
         $tmpFile = sys_get_temp_dir() . '/' . uniqid();
         file_put_contents($tmpFile, $value->getContent());
         try {
             $this->symfonyValidator->validate($tmpFile, $constraint);
         } catch (\Exception $e) {
             unlink($tmpFile);
             throw $e;
         }
         unlink($tmpFile);
         return;
     }
     $this->symfonyValidator->validate($value, $constraint);
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (!$value instanceof AttachmentInterface) {
         return;
     }
     if (!$constraint instanceof FileConstraint) {
         throw new UnexpectedTypeException($constraint, FileConstraint::class);
     }
     parent::validate($value->getFile()->getTmpPathname(), $constraint);
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if ($value instanceof AbstractProductMedia) {
         $value = $value->getFile();
     }
     if (null === $value || '' === $value) {
         return;
     }
     parent::validate($value, $constraint);
     $this->validateAllowedExtension($value, $constraint);
 }
Esempio n. 4
0
 /**
  * {@inheritDoc}
  */
 public function validate($value, Constraint $constraint)
 {
     $violations = count($this->context->getViolations());
     parent::validate($value, $constraint);
     $failed = count($this->context->getViolations()) !== $violations;
     if ($failed || null === $value || '' === $value) {
         return;
     }
     if (null === $constraint->minWidth && null === $constraint->maxWidth && null === $constraint->minHeight && null === $constraint->maxHeight) {
         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;
         }
     }
     if ($constraint->minHeight) {
         if (!ctype_digit((string) $constraint->minHeight)) {
             throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight));
         }
         if ($height < $constraint->minHeight) {
             $this->context->addViolation($constraint->minHeightMessage, array('{{ height }}' => $height, '{{ min_height }}' => $constraint->minHeight));
             return;
         }
     }
     if ($constraint->maxHeight) {
         if (!ctype_digit((string) $constraint->maxHeight)) {
             throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight));
         }
         if ($height > $constraint->maxHeight) {
             $this->context->addViolation($constraint->maxHeightMessage, array('{{ height }}' => $height, '{{ max_height }}' => $constraint->maxHeight));
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (!$constraint instanceof HaphpyMedia) {
         throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\HaphpyMedia');
     }
     $violations = count($this->context->getViolations());
     parent::validate($value, $constraint);
     $failed = count($this->context->getViolations()) !== $violations;
     if ($failed || null === $value || '' === $value) {
         return;
     }
     if (null === $constraint->minWidth && null === $constraint->maxWidth && null === $constraint->minHeight && null === $constraint->maxHeight && null === $constraint->minRatio && null === $constraint->maxRatio && $constraint->allowSquare && $constraint->allowLandscape && $constraint->allowPortrait) {
         return;
     }
     if (strpos($value->getMimeType(), 'image') !== false) {
         $this->validateImage($value, $constraint);
     }
 }
 /**
  * Validate that the upload is a CSV (or plain text) file, and that the columns match those expected
  *
  * @param mixed $value
  * @param Validator\Constraint $constraint
  * @throws \InvalidArgumentException
  */
 public function validate($value, Validator\Constraint $constraint)
 {
     if (!$constraint instanceof Csv) {
         throw new \InvalidArgumentException('Constraint must be an instance of Csv');
     }
     if (null === $value || '' === $value) {
         return;
     }
     if (!$value instanceof CSVFile) {
         throw new \LogicException('Value must be an instance of CSVFile');
     }
     parent::validate($value, $constraint);
     try {
         $value->getFirstLineAsColumns($this->_heading);
     } catch (InvalidFileException $e) {
         $this->context->addViolation($constraint->csvColumnError);
     } catch (\Exception $e) {
         $this->context->addViolation($constraint->uploadErrorMessage);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (!$constraint instanceof Image) {
         throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Image');
     }
     $violations = count($this->context->getViolations());
     parent::validate($value, $constraint);
     $failed = count($this->context->getViolations()) !== $violations;
     if ($failed || null === $value || '' === $value) {
         return;
     }
     if (null === $constraint->minWidth && null === $constraint->maxWidth && null === $constraint->minHeight && null === $constraint->maxHeight && null === $constraint->minRatio && null === $constraint->maxRatio && $constraint->allowSquare && $constraint->allowLandscape && $constraint->allowPortrait) {
         return;
     }
     $size = @getimagesize($value);
     if (empty($size) || $size[0] === 0 || $size[1] === 0) {
         $this->buildViolation($constraint->sizeNotDetectedMessage)->addViolation();
         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->buildViolation($constraint->minWidthMessage)->setParameter('{{ width }}', $width)->setParameter('{{ min_width }}', $constraint->minWidth)->addViolation();
             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->buildViolation($constraint->maxWidthMessage)->setParameter('{{ width }}', $width)->setParameter('{{ max_width }}', $constraint->maxWidth)->addViolation();
             return;
         }
     }
     if ($constraint->minHeight) {
         if (!ctype_digit((string) $constraint->minHeight)) {
             throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight));
         }
         if ($height < $constraint->minHeight) {
             $this->buildViolation($constraint->minHeightMessage)->setParameter('{{ height }}', $height)->setParameter('{{ min_height }}', $constraint->minHeight)->addViolation();
             return;
         }
     }
     if ($constraint->maxHeight) {
         if (!ctype_digit((string) $constraint->maxHeight)) {
             throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight));
         }
         if ($height > $constraint->maxHeight) {
             $this->buildViolation($constraint->maxHeightMessage)->setParameter('{{ height }}', $height)->setParameter('{{ max_height }}', $constraint->maxHeight)->addViolation();
         }
     }
     $ratio = round($width / $height, 2);
     if (null !== $constraint->minRatio) {
         if (!is_numeric((string) $constraint->minRatio)) {
             throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum ratio', $constraint->minRatio));
         }
         if ($ratio < $constraint->minRatio) {
             $this->buildViolation($constraint->minRatioMessage)->setParameter('{{ ratio }}', $ratio)->setParameter('{{ min_ratio }}', $constraint->minRatio)->addViolation();
         }
     }
     if (null !== $constraint->maxRatio) {
         if (!is_numeric((string) $constraint->maxRatio)) {
             throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum ratio', $constraint->maxRatio));
         }
         if ($ratio > $constraint->maxRatio) {
             $this->buildViolation($constraint->maxRatioMessage)->setParameter('{{ ratio }}', $ratio)->setParameter('{{ max_ratio }}', $constraint->maxRatio)->addViolation();
         }
     }
     if (!$constraint->allowSquare && $width == $height) {
         $this->buildViolation($constraint->allowSquareMessage)->setParameter('{{ width }}', $width)->setParameter('{{ height }}', $height)->addViolation();
     }
     if (!$constraint->allowLandscape && $width > $height) {
         $this->buildViolation($constraint->allowLandscapeMessage)->setParameter('{{ width }}', $width)->setParameter('{{ height }}', $height)->addViolation();
     }
     if (!$constraint->allowPortrait && $width < $height) {
         $this->buildViolation($constraint->allowPortraitMessage)->setParameter('{{ width }}', $width)->setParameter('{{ height }}', $height)->addViolation();
     }
 }
Esempio n. 8
0
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (!$constraint instanceof Image) {
         throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Image');
     }
     $violations = count($this->context->getViolations());
     parent::validate($value, $constraint);
     $failed = count($this->context->getViolations()) !== $violations;
     if ($failed || null === $value || '' === $value) {
         return;
     }
     if (null === $constraint->minWidth && null === $constraint->maxWidth && null === $constraint->minHeight && null === $constraint->maxHeight && null === $constraint->minRatio && null === $constraint->maxRatio && $constraint->allowSquare && $constraint->allowLandscape && $constraint->allowPortrait && !$constraint->detectCorrupted) {
         return;
     }
     $size = @getimagesize($value);
     if (empty($size) || $size[0] === 0 || $size[1] === 0) {
         $this->context->buildViolation($constraint->sizeNotDetectedMessage)->setCode(Image::SIZE_NOT_DETECTED_ERROR)->addViolation();
         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->buildViolation($constraint->minWidthMessage)->setParameter('{{ width }}', $width)->setParameter('{{ min_width }}', $constraint->minWidth)->setCode(Image::TOO_NARROW_ERROR)->addViolation();
             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->buildViolation($constraint->maxWidthMessage)->setParameter('{{ width }}', $width)->setParameter('{{ max_width }}', $constraint->maxWidth)->setCode(Image::TOO_WIDE_ERROR)->addViolation();
             return;
         }
     }
     if ($constraint->minHeight) {
         if (!ctype_digit((string) $constraint->minHeight)) {
             throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight));
         }
         if ($height < $constraint->minHeight) {
             $this->context->buildViolation($constraint->minHeightMessage)->setParameter('{{ height }}', $height)->setParameter('{{ min_height }}', $constraint->minHeight)->setCode(Image::TOO_LOW_ERROR)->addViolation();
             return;
         }
     }
     if ($constraint->maxHeight) {
         if (!ctype_digit((string) $constraint->maxHeight)) {
             throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight));
         }
         if ($height > $constraint->maxHeight) {
             $this->context->buildViolation($constraint->maxHeightMessage)->setParameter('{{ height }}', $height)->setParameter('{{ max_height }}', $constraint->maxHeight)->setCode(Image::TOO_HIGH_ERROR)->addViolation();
         }
     }
     $ratio = round($width / $height, 2);
     if (null !== $constraint->minRatio) {
         if (!is_numeric((string) $constraint->minRatio)) {
             throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum ratio', $constraint->minRatio));
         }
         if ($ratio < $constraint->minRatio) {
             $this->context->buildViolation($constraint->minRatioMessage)->setParameter('{{ ratio }}', $ratio)->setParameter('{{ min_ratio }}', $constraint->minRatio)->setCode(Image::RATIO_TOO_SMALL_ERROR)->addViolation();
         }
     }
     if (null !== $constraint->maxRatio) {
         if (!is_numeric((string) $constraint->maxRatio)) {
             throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum ratio', $constraint->maxRatio));
         }
         if ($ratio > $constraint->maxRatio) {
             $this->context->buildViolation($constraint->maxRatioMessage)->setParameter('{{ ratio }}', $ratio)->setParameter('{{ max_ratio }}', $constraint->maxRatio)->setCode(Image::RATIO_TOO_BIG_ERROR)->addViolation();
         }
     }
     if (!$constraint->allowSquare && $width == $height) {
         $this->context->buildViolation($constraint->allowSquareMessage)->setParameter('{{ width }}', $width)->setParameter('{{ height }}', $height)->setCode(Image::SQUARE_NOT_ALLOWED_ERROR)->addViolation();
     }
     if (!$constraint->allowLandscape && $width > $height) {
         $this->context->buildViolation($constraint->allowLandscapeMessage)->setParameter('{{ width }}', $width)->setParameter('{{ height }}', $height)->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR)->addViolation();
     }
     if (!$constraint->allowPortrait && $width < $height) {
         $this->context->buildViolation($constraint->allowPortraitMessage)->setParameter('{{ width }}', $width)->setParameter('{{ height }}', $height)->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)->addViolation();
     }
     if ($constraint->detectCorrupted) {
         if (!function_exists('imagecreatefromstring')) {
             throw new RuntimeException('Corrupted images detection requires installed and enabled GD extension');
         }
         $resource = @imagecreatefromstring(file_get_contents($value));
         if (false === $resource) {
             $this->context->buildViolation($constraint->corruptedMessage)->setCode(Image::CORRUPTED_IMAGE_ERROR)->addViolation();
             return;
         }
         imagedestroy($resource);
     }
 }
 function it_saves_file_and_calls_symfony_validator(FileValidator $fileValidator, File $constraint, FSiFile $file)
 {
     $file->getContent()->willReturn('file content');
     $fileValidator->validate(Argument::containingString('/tmp/'), $constraint)->shouldBeCalled();
     $this->validate($file, $constraint);
 }