Oihana PHP

StartsWithRule extends Rule

Rule: Ensures that a given value starts with a specified prefix.

This rule validates that the input value begins with a given string prefix. It supports flexible comparison: the rule passes if

  • the value exactly matches the prefix,
  • the prefix is empty,
  • or the value starts with the prefix.

Example:

$rule = new StartsWithRule( 'abc'] );
$rule->check('abcdef'); // true
$rule->check('xyz');    // false
Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Constants

NAME  : string = 'startsWith'
The rule name.
PREFIX  : string = 'prefix'
The "prefix" parameter key.

Properties

$fillableParams  : array<string|int, mixed>|array<string|int, string>
$message  : string
The error message used when validation fails.

Methods

__construct()  : mixed
Creates a new StartsWithRule instance.
check()  : bool
Checks if the provided value meets the required conditions based on the prefix parameter.
prefix()  : static
Sets the prefix expression to evaluate.

Constants

PREFIX

The "prefix" parameter key.

public string PREFIX = 'prefix'

Properties

$fillableParams

protected array<string|int, mixed>|array<string|int, string> $fillableParams = [self::PREFIX]

The list of required parameters.

$message

The error message used when validation fails.

protected string $message = "The :attribute don't start with :prefix"

Methods

__construct()

Creates a new StartsWithRule instance.

public __construct([string|null $prefix = null ]) : mixed
Parameters
$prefix : string|null = null

The prefix expression to evaluate.

check()

Checks if the provided value meets the required conditions based on the prefix parameter.

public check(mixed $value) : bool
Parameters
$value : mixed

The value to be checked.

Tags
throws
ParameterException

If the required parameter is missing.

Return values
bool —

Returns true if the value equals the prefix, the prefix is empty, or the value starts with the prefix; otherwise, false.

prefix()

Sets the prefix expression to evaluate.

public prefix(string|null $value) : static
Parameters
$value : string|null

The prefix string to use for validation.

Return values
static —

Returns the current instance for method chaining.

On this page

Search results