Final Class Yiisoft\Validator\Rule\Image\ImageHandler
| Inheritance | Yiisoft\Validator\Rule\Image\ImageHandler |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerInterface |
Validates that a value is an image and optionally checks its dimensions.
See also Yiisoft\Validator\Rule\Image\Image.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Validator\Rule\Image\ImageHandler | |
| validate() | Yiisoft\Validator\Rule\Image\ImageHandler |
Method Details
| public mixed __construct ( Yiisoft\Validator\Rule\Image\ImageInfoProviderInterface|null $imageInfoProvider = null ) | ||
| $imageInfoProvider | Yiisoft\Validator\Rule\Image\ImageInfoProviderInterface|null | |
public function __construct(
?ImageInfoProviderInterface $imageInfoProvider = null,
) {
$this->imageInfoProvider = $imageInfoProvider ?? new NativeImageInfoProvider();
}
| public Yiisoft\Validator\Result validate ( mixed $value, Yiisoft\Validator\RuleInterface $rule, Yiisoft\Validator\ValidationContext $context ) | ||
| $value | mixed | |
| $rule | Yiisoft\Validator\RuleInterface | |
| $context | Yiisoft\Validator\ValidationContext | |
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
if (!$rule instanceof Image) {
throw new UnexpectedRuleException(Image::class, $rule);
}
$result = new Result();
$imageFilePath = $this->getImageFilePath($value);
if (empty($imageFilePath)) {
$result->addError($rule->getNotImageMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
]);
return $result;
}
if (!$this->shouldValidateDimensions($rule)) {
return $result;
}
$info = $this->imageInfoProvider->get($imageFilePath);
if (empty($info)) {
$result->addError($rule->getNotImageMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
]);
return $result;
}
$width = $info->getWidth();
$height = $info->getHeight();
if ($rule->getWidth() !== null && $width !== $rule->getWidth()) {
$result->addError($rule->getNotExactWidthMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'exactly' => $rule->getWidth(),
]);
}
if ($rule->getHeight() !== null && $height !== $rule->getHeight()) {
$result->addError($rule->getNotExactHeightMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'exactly' => $rule->getHeight(),
]);
}
if ($rule->getMinWidth() !== null && $width < $rule->getMinWidth()) {
$result->addError($rule->getTooSmallWidthMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'limit' => $rule->getMinWidth(),
]);
}
if ($rule->getMinHeight() !== null && $height < $rule->getMinHeight()) {
$result->addError($rule->getTooSmallHeightMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'limit' => $rule->getMinHeight(),
]);
}
if ($rule->getMaxWidth() !== null && $width > $rule->getMaxWidth()) {
$result->addError($rule->getTooLargeWidthMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'limit' => $rule->getMaxWidth(),
]);
}
if ($rule->getMaxHeight() !== null && $height > $rule->getMaxHeight()) {
$result->addError($rule->getTooLargeHeightMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'limit' => $rule->getMaxHeight(),
]);
}
$this->validateAspectRatio($width, $height, $rule, $context, $result);
return $result;
}
Signup or Login in order to comment.