0 follower

Final Class Yiisoft\Validator\Rule\Image\Image

InheritanceYiisoft\Validator\Rule\Image\Image
ImplementsYiisoft\Validator\DumpedRuleInterface, Yiisoft\Validator\SkipOnEmptyInterface, Yiisoft\Validator\SkipOnErrorInterface, Yiisoft\Validator\WhenInterface
Uses TraitsYiisoft\Validator\Rule\Trait\SkipOnEmptyTrait, Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait, Yiisoft\Validator\Rule\Trait\WhenTrait

Defines validation options to check that a value is an image and optionally check its dimensions.

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

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\Rule\Image\Image
getAspectRatio() Yiisoft\Validator\Rule\Image\Image
getHandler() Yiisoft\Validator\Rule\Image\Image
getHeight() Yiisoft\Validator\Rule\Image\Image
getInvalidAspectRatioMessage() Yiisoft\Validator\Rule\Image\Image
getMaxHeight() Yiisoft\Validator\Rule\Image\Image
getMaxWidth() Yiisoft\Validator\Rule\Image\Image
getMinHeight() Yiisoft\Validator\Rule\Image\Image
getMinWidth() Yiisoft\Validator\Rule\Image\Image
getName() Yiisoft\Validator\Rule\Image\Image
getNotExactHeightMessage() Yiisoft\Validator\Rule\Image\Image
getNotExactWidthMessage() Yiisoft\Validator\Rule\Image\Image
getNotImageMessage() Yiisoft\Validator\Rule\Image\Image
getOptions() Yiisoft\Validator\Rule\Image\Image
getSkipOnEmpty() A getter for $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
getTooLargeHeightMessage() Yiisoft\Validator\Rule\Image\Image
getTooLargeWidthMessage() Yiisoft\Validator\Rule\Image\Image
getTooSmallHeightMessage() Yiisoft\Validator\Rule\Image\Image
getTooSmallWidthMessage() Yiisoft\Validator\Rule\Image\Image
getWhen() A getter for $when property. Yiisoft\Validator\Rule\Trait\WhenTrait
getWidth() Yiisoft\Validator\Rule\Image\Image
shouldSkipOnError() A getter for $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
skipOnEmpty() An immutable setter to change $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
skipOnError() An immutable setter to change $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
when() An immutable setter to change $when property. Yiisoft\Validator\Rule\Trait\WhenTrait

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( integer|null $width null, integer|null $height null, integer|null $minWidth null, integer|null $minHeight null, integer|null $maxWidth null, integer|null $maxHeight null, Yiisoft\Validator\Rule\Image\ImageAspectRatio|null $aspectRatio null, string $notImageMessage '{Property} must be an image.', string $notExactWidthMessage 'The width of {property} must be exactly {exactly, number} {exactly, plural, one{pixel} other{pixels}}.', string $notExactHeightMessage 'The height of {property} must be exactly {exactly, number} {exactly, plural, one{pixel} other{pixels}}.', string $tooSmallWidthMessage 'The width of {property} cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.', string $tooSmallHeightMessage 'The height of {property} cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.', string $tooLargeWidthMessage 'The width of {property} cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.', string $tooLargeHeightMessage 'The height of {property} cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.', string $invalidAspectRatioMessage 'The aspect ratio of {property} must be {aspectRatioWidth, number}:{aspectRatioHeight, number} with margin {aspectRatioMargin, number}%.', boolean|callable|null $skipOnEmpty null, boolean $skipOnError false, Closure|null $when null )
$width integer|null

Expected exact width of validated image file.

$height integer|null

Expected exact height of validated image file.

$minWidth integer|null

Expected minimum width of validated image file.

$minHeight integer|null

Expected minimum height of validated image file.

$maxWidth integer|null

Expected maximum width of validated image file.

$maxHeight integer|null

Expected maximum height of validated image file.

$aspectRatio Yiisoft\Validator\Rule\Image\ImageAspectRatio|null

Expected aspect ratio of validated image file.

$notImageMessage string

A message used when the validated value is not valid image file.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
$notExactWidthMessage string

A message used when the width of validated image file doesn't exactly equal to {@see $width}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {exactly}: expected exact width of validated image file.
$notExactHeightMessage string

A message used when the height of validated image file doesn't exactly equal to {@see $height}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {exactly}: expected exact height of validated image file.
$tooSmallWidthMessage string

A message used when the width of validated image file is less than {@see $minWidth}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {limit}: expected minimum width of validated image file.
$tooSmallHeightMessage string

A message used when the height of validated image file is less than {@see $minHeight}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {limit}: expected minimum height of validated image file.
$tooLargeWidthMessage string

A message used when the width of validated image file is more than {@see $maxWidth}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {limit}: expected maximum width of validated image file.
$tooLargeHeightMessage string

A message used when the height of validated image file is more than {@see $maxHeight}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {limit}: expected maximum height of validated image file.
$invalidAspectRatioMessage string

A message used when aspect ratio of validated image file is different than {@see \Yiisoft\Validator\Rule\Image\ImageAspectRatio::$width}:{@see \Yiisoft\Validator\Rule\Image\ImageAspectRatio::$height} with correction based on {@see \Yiisoft\Validator\Rule\Image\ImageAspectRatio::$margin}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {aspectRatioWidth}: expected width part for aspect ratio. For example, for 4:3 aspect ratio, it will be 4.
  • {aspectRatioHeight}: expected height part for aspect ratio. For example, for 4:3 aspect ratio, it will be 3.
  • {aspectRatioMargin}: expected margin for aspect ratio in percents.
$skipOnEmpty boolean|callable|null

Whether to skip this rule if the value validated is empty. See {@see \Yiisoft\Validator\SkipOnEmptyInterface}.

$skipOnError boolean

Whether to skip this rule if any of the previous rules gave an error. See {@see \Yiisoft\Validator\SkipOnErrorInterface}.

$when Closure|null

A callable to define a condition for applying the rule. See {@see \Yiisoft\Validator\WhenInterface}.

                public function __construct(
    private ?int $width = null,
    private ?int $height = null,
    private ?int $minWidth = null,
    private ?int $minHeight = null,
    private ?int $maxWidth = null,
    private ?int $maxHeight = null,
    private ?ImageAspectRatio $aspectRatio = null,
    private string $notImageMessage = '{Property} must be an image.',
    private string $notExactWidthMessage = 'The width of {property} must be exactly {exactly, number} {exactly, plural, one{pixel} other{pixels}}.',
    private string $notExactHeightMessage = 'The height of {property} must be exactly {exactly, number} {exactly, plural, one{pixel} other{pixels}}.',
    private string $tooSmallWidthMessage = 'The width of {property} cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.',
    private string $tooSmallHeightMessage = 'The height of {property} cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.',
    private string $tooLargeWidthMessage = 'The width of {property} cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.',
    private string $tooLargeHeightMessage = 'The height of {property} cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.',
    private string $invalidAspectRatioMessage = 'The aspect ratio of {property} must be {aspectRatioWidth, number}:{aspectRatioHeight, number} with margin {aspectRatioMargin, number}%.',
    bool|callable|null $skipOnEmpty = null,
    private bool $skipOnError = false,
    private ?Closure $when = null,
) {
    if ($this->width !== null && ($this->minWidth !== null || $this->maxWidth !== null)) {
        throw new InvalidArgumentException('Exact width and min / max width can\'t be specified together.');
    }
    if ($this->height !== null && ($this->minHeight !== null || $this->maxHeight !== null)) {
        throw new InvalidArgumentException('Exact width and min / max height can\'t be specified together.');
    }
    $this->skipOnEmpty = $skipOnEmpty;
}

            
getAspectRatio() public method

public Yiisoft\Validator\Rule\Image\ImageAspectRatio|null getAspectRatio ( )

                public function getAspectRatio(): ?ImageAspectRatio
{
    return $this->aspectRatio;
}

            
getHandler() public method

public string getHandler ( )

                public function getHandler(): string
{
    return ImageHandler::class;
}

            
getHeight() public method

public integer|null getHeight ( )

                public function getHeight(): ?int
{
    return $this->height;
}

            
getInvalidAspectRatioMessage() public method

public string getInvalidAspectRatioMessage ( )

                public function getInvalidAspectRatioMessage(): string
{
    return $this->invalidAspectRatioMessage;
}

            
getMaxHeight() public method

public integer|null getMaxHeight ( )

                public function getMaxHeight(): ?int
{
    return $this->maxHeight;
}

            
getMaxWidth() public method

public integer|null getMaxWidth ( )

                public function getMaxWidth(): ?int
{
    return $this->maxWidth;
}

            
getMinHeight() public method

public integer|null getMinHeight ( )

                public function getMinHeight(): ?int
{
    return $this->minHeight;
}

            
getMinWidth() public method

public integer|null getMinWidth ( )

                public function getMinWidth(): ?int
{
    return $this->minWidth;
}

            
getName() public method

public string getName ( )

                public function getName(): string
{
    return 'image';
}

            
getNotExactHeightMessage() public method

public string getNotExactHeightMessage ( )

                public function getNotExactHeightMessage(): string
{
    return $this->notExactHeightMessage;
}

            
getNotExactWidthMessage() public method

public string getNotExactWidthMessage ( )

                public function getNotExactWidthMessage(): string
{
    return $this->notExactWidthMessage;
}

            
getNotImageMessage() public method

public string getNotImageMessage ( )

                public function getNotImageMessage(): string
{
    return $this->notImageMessage;
}

            
getOptions() public method

public array getOptions ( )

                public function getOptions(): array
{
    return [
        'width' => $this->width,
        'height' => $this->height,
        'minWidth' => $this->minWidth,
        'minHeight' => $this->minHeight,
        'maxWidth' => $this->maxWidth,
        'maxHeight' => $this->maxHeight,
        'aspectRatioWidth' => $this->getAspectRatio()?->getWidth(),
        'aspectRatioHeight' => $this->getAspectRatio()?->getHeight(),
        'aspectRatioMargin' => $this->getAspectRatio()?->getMargin(),
        'notExactWidthMessage' => [
            'template' => $this->notExactWidthMessage,
            'parameters' => [
                'exactly' => $this->width,
            ],
        ],
        'notExactHeightMessage' => [
            'template' => $this->notExactHeightMessage,
            'parameters' => [
                'exactly' => $this->height,
            ],
        ],
        'tooSmallWidthMessage' => [
            'template' => $this->tooSmallWidthMessage,
            'parameters' => [
                'limit' => $this->minWidth,
            ],
        ],
        'tooSmallHeightMessage' => [
            'template' => $this->tooSmallHeightMessage,
            'parameters' => [
                'limit' => $this->minHeight,
            ],
        ],
        'tooLargeWidthMessage' => [
            'template' => $this->tooLargeWidthMessage,
            'parameters' => [
                'limit' => $this->maxWidth,
            ],
        ],
        'tooLargeHeightMessage' => [
            'template' => $this->tooLargeHeightMessage,
            'parameters' => [
                'limit' => $this->maxHeight,
            ],
        ],
        'notImageMessage' => [
            'template' => $this->notImageMessage,
            'parameters' => [],
        ],
        'invalidAspectRatioMessage' => [
            'template' => $this->invalidAspectRatioMessage,
            'parameters' => [
                'aspectRatioWidth' => $this->getAspectRatio()?->getWidth(),
                'aspectRatioHeight' => $this->getAspectRatio()?->getHeight(),
                'aspectRatioMargin' => $this->getAspectRatio()?->getMargin(),
            ],
        ],
        'skipOnEmpty' => $this->getSkipOnEmptyOption(),
        'skipOnError' => $this->skipOnError,
    ];
}

            
getSkipOnEmpty() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::getSkipOnEmpty()

A getter for $skipOnEmpty property.

public boolean|callable|null getSkipOnEmpty ( )
return boolean|callable|null

A current raw (non-normalized) value.

                public function getSkipOnEmpty(): bool|callable|null
{
    return $this->skipOnEmpty;
}

            
getTooLargeHeightMessage() public method

public string getTooLargeHeightMessage ( )

                public function getTooLargeHeightMessage(): string
{
    return $this->tooLargeHeightMessage;
}

            
getTooLargeWidthMessage() public method

public string getTooLargeWidthMessage ( )

                public function getTooLargeWidthMessage(): string
{
    return $this->tooLargeWidthMessage;
}

            
getTooSmallHeightMessage() public method

public string getTooSmallHeightMessage ( )

                public function getTooSmallHeightMessage(): string
{
    return $this->tooSmallHeightMessage;
}

            
getTooSmallWidthMessage() public method

public string getTooSmallWidthMessage ( )

                public function getTooSmallWidthMessage(): string
{
    return $this->tooSmallWidthMessage;
}

            
getWhen() public method

Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::getWhen()

A getter for $when property.

public Closure|null getWhen ( )
return Closure|null

Current value:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.

                public function getWhen(): ?Closure
{
    return $this->when;
}

            
getWidth() public method

public integer|null getWidth ( )

                public function getWidth(): ?int
{
    return $this->width;
}

            
shouldSkipOnError() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::shouldSkipOnError()

A getter for $skipOnError property.

public boolean shouldSkipOnError ( )
return boolean

Current value. true means to skip the current rule when the previous one errored and false - do not skip.

                public function shouldSkipOnError(): bool
{
    return $this->skipOnError;
}

            
skipOnEmpty() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::skipOnEmpty()

An immutable setter to change $skipOnEmpty property.

public $this skipOnEmpty ( boolean|callable|null $value )
$value boolean|callable|null

A new value.

return $this

The new instance with a changed value.

                public function skipOnEmpty(bool|callable|null $value): static
{
    $new = clone $this;
    $new->skipOnEmpty = $value;
    return $new;
}

            
skipOnError() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::skipOnError()

An immutable setter to change $skipOnError property.

public $this skipOnError ( boolean $value )
$value boolean

A new value. true means to skip the current rule when the previous one errored and false - do not skip.

return $this

The new instance with a changed value.

                public function skipOnError(bool $value): static
{
    $new = clone $this;
    $new->skipOnError = $value;
    return $new;
}

            
when() public method

Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::when()

An immutable setter to change $when property.

public $this when ( Closure|null $value )
$value Closure|null

A new value:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.
return $this

The new instance with a changed value.

                public function when(?Closure $value): static
{
    $new = clone $this;
    $new->when = $value;
    return $new;
}