0 follower

Final Class Yiisoft\Validator\Rule\Image\ImageHandler

InheritanceYiisoft\Validator\Rule\Image\ImageHandler
ImplementsYiisoft\Validator\RuleHandlerInterface

Validates that a value is an image and optionally checks its dimensions.

See also Yiisoft\Validator\Rule\Image\Image.

Method Details

Hide inherited methods

__construct() public method

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();
}

            
validate() public method

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;
}