0 follower

Final Class Yiisoft\Validator\Rule\Count

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

Defines validation options to check that the value contains certain number of items.

Can be applied to arrays or classes implementing Countable interface.

See also Yiisoft\Validator\Rule\CountHandler.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\Rule\Count
getExactly() A getter for $exactly property. Yiisoft\Validator\Rule\Trait\CountableLimitTrait
getGreaterThanMaxMessage() A getter for $greaterThanMaxMessage property. Yiisoft\Validator\Rule\Trait\CountableLimitTrait
getHandler() Yiisoft\Validator\Rule\Count
getIncorrectInputMessage() Get error message used when the value is neither an array nor an object implementing Countable interface. Yiisoft\Validator\Rule\Count
getLessThanMinMessage() A getter for $lessThanMinMessage property. Yiisoft\Validator\Rule\Trait\CountableLimitTrait
getMax() A getter for $max property. Yiisoft\Validator\Rule\Trait\CountableLimitTrait
getMin() A getter for $min property. Yiisoft\Validator\Rule\Trait\CountableLimitTrait
getName() Yiisoft\Validator\Rule\Count
getNotExactlyMessage() A getter for $notExactlyMessage property. Yiisoft\Validator\Rule\Trait\CountableLimitTrait
getOptions() Yiisoft\Validator\Rule\Count
getSkipOnEmpty() A getter for $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
getWhen() A getter for $when property. Yiisoft\Validator\Rule\Trait\WhenTrait
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 __construct( integer|null $exactly null, integer|null $min null, integer|null $max null, string $incorrectInputMessage '{Property} must be an array or implement \\Countable interface. ' '{type} given.', string $lessThanMinMessage '{Property} must contain at least {min, number} {min, plural, one{item} ' 'other{items}}.', string $greaterThanMaxMessage '{Property} must contain at most {max, number} {max, plural, one{item} ' 'other{items}}.', string $notExactlyMessage '{Property} must contain exactly {exactly, number} {exactly, plural, one{item} ' 'other{items}}.', boolean|callable|null $skipOnEmpty null, boolean $skipOnError false, Closure|null $when null ): mixed
$exactly integer|null

Exact number of items. null means no strict comparison. Mutually exclusive with $min and $max.

$min integer|null

Minimum number of items. null means no minimum number limit. Can't be combined with $exactly. See $lessThanMinMessage for the customized message for a value with too few items.

$max integer|null

Maximum number of items. null means no maximum number limit. Can't be combined with $exactly. See $greaterThanMaxMessage for the customized message for a value with too many items.

$incorrectInputMessage string

Error message used when the value is neither an array nor an object implementing Countable interface.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {type}: the type of the value being validated.
$lessThanMinMessage string

Error message used when the number of items is smaller than $min.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {min}: minimum number of items required.
  • {number}: actual number of items.
$greaterThanMaxMessage string

Error message used when the number of items is greater than $max.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {max}: maximum number of items required.
  • {number}: actual number of items.
$notExactlyMessage string

Error message used when the number of items does not equal $exactly.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {exactly}: exact number of items required.
  • {number}: actual number of items.
$skipOnEmpty boolean|callable|null

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

$skipOnError boolean

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

$when Closure|null

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

                public function __construct(
    ?int $exactly = null,
    ?int $min = null,
    ?int $max = null,
    private string $incorrectInputMessage = '{Property} must be an array or implement \Countable interface. '
    . '{type} given.',
    string $lessThanMinMessage = '{Property} must contain at least {min, number} {min, plural, one{item} '
    . 'other{items}}.',
    string $greaterThanMaxMessage = '{Property} must contain at most {max, number} {max, plural, one{item} '
    . 'other{items}}.',
    string $notExactlyMessage = '{Property} must contain exactly {exactly, number} {exactly, plural, one{item} '
    . 'other{items}}.',
    bool|callable|null $skipOnEmpty = null,
    private bool $skipOnError = false,
    private ?Closure $when = null,
) {
    $this->initCountableLimitProperties(
        $min,
        $max,
        $exactly,
        $lessThanMinMessage,
        $greaterThanMaxMessage,
        $notExactlyMessage,
    );
    $this->skipOnEmpty = $skipOnEmpty;
}

            
getExactly() public method

Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getExactly()

A getter for $exactly property.

public getExactly( ): integer|null
return integer|null

A number representing "exactly" value. null means no strict comparison so lower / upper limits / both must be set.

                public function getExactly(): ?int
{
    return $this->exactly;
}

            
getGreaterThanMaxMessage() public method

Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getGreaterThanMaxMessage()

A getter for $greaterThanMaxMessage property.

public getGreaterThanMaxMessage( ): string
return string

Validation error message.

                public function getGreaterThanMaxMessage(): string
{
    return $this->greaterThanMaxMessage;
}

            
getHandler() public method

public getHandler( ): string

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

            
getIncorrectInputMessage() public method

Get error message used when the value is neither an array nor an object implementing Countable interface.

public getIncorrectInputMessage( ): string
return string

Error message.

                public function getIncorrectInputMessage(): string
{
    return $this->incorrectInputMessage;
}

            
getLessThanMinMessage() public method

Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getLessThanMinMessage()

A getter for $lessThanMinMessage property.

public getLessThanMinMessage( ): string
return string

Validation error message.

                public function getLessThanMinMessage(): string
{
    return $this->lessThanMinMessage;
}

            
getMax() public method

Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getMax()

A getter for $max property.

public getMax( ): integer|null
return integer|null

A number representing maximum boundary. null means no upper bound.

                public function getMax(): ?int
{
    return $this->max;
}

            
getMin() public method

Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getMin()

A getter for $min property.

public getMin( ): integer|null
return integer|null

A number representing minimum boundary. null means no lower bound.

                public function getMin(): ?int
{
    return $this->min;
}

            
getName() public method

public getName( ): string

                public function getName(): string
{
    return self::class;
}

            
getNotExactlyMessage() public method

Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getNotExactlyMessage()

A getter for $notExactlyMessage property.

public getNotExactlyMessage( ): string
return string

Validation error message.

                public function getNotExactlyMessage(): string
{
    return $this->notExactlyMessage;
}

            
getOptions() public method

public getOptions( ): array

                public function getOptions(): array
{
    return array_merge($this->getLimitOptions(), [
        'incorrectInputMessage' => [
            'template' => $this->getIncorrectInputMessage(),
            'parameters' => [],
        ],
        'skipOnEmpty' => $this->getSkipOnEmptyOption(),
        'skipOnError' => $this->skipOnError,
    ]);
}

            
getSkipOnEmpty() public method

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

A getter for $skipOnEmpty property.

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

A current raw (non-normalized) value.

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

            
getWhen() public method

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

A getter for $when property.

public getWhen( ): Closure|null
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;
}

            
shouldSkipOnError() public method

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

A getter for $skipOnError property.

public shouldSkipOnError( ): boolean
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 skipOnEmpty( boolean|callable|null $value ): $this
$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 skipOnError( boolean $value ): $this
$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 when( Closure|null $value ): $this
$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;
}