This documentation explains step by step how to use the Webhkp\Pvalidate
library to perform length validation on string properties in a PHP class. The provided code demonstrates how to create a class with length constraints on its properties and how to validate those constraints using the library.
Import the Necessary Classes
Ensure you have the Webhkp\Pvalidate
package installed via Composer. Then import/use the following-
use Webhkp\Pvalidate\Rules\Builder\LengthRuleBuilder;
use Webhkp\Pvalidate\Validator;
use Webhkp\Pvalidate\Rules\Length;
use Webhkp\Pvalidate\ValidationBuilder;
Define the Class with Length Constraints
Create a class (MyClass
) with properties that have length constraints. Use the #[Length]
attribute to specify the constraints.
By annotating properties with the #[Length]
attribute, you can specify minimum and/or maximum lengths:
class MyClass {
public function __construct() {}
#[Length(2, 5)]
public string $firstString = 'my first string';
#[Length(min: 20)]
public string $secondString = 'my second string';
#[Length(max: 10)]
public string $thirdString = 'my third string';
#[Length(min: 20, max: 50)]
public string $fourthString = 'my fourth string';
#[Length(min: 10, max: 20)]
public string $fifthString = 'my fifth string';
#[Length(min: 100, max: 20)]
public string $sixthString = 'my sixth string';
#[MinLength(10)]
public string $minLengthTestString = 'abc';
#[MaxLength(10)]
public string $maxLengthTestString = 'abc def ghi jkl';
}
Validate the Object
Create an instance of the class and validate it using the Validator::validate
method. This method checks the properties against the defined length constraints:
$myObj = new MyClass();
$validationResponse = Validator::validate($myObj);
var_dump($validationResponse->isValid());
var_dump($validationResponse->getErrors());
Use Length Rule Directly
You can also validate strings directly using the Length
class:
$lengthRuleValidation1 = (new Length(10, 20))->safeParse('some string here');
var_dump($lengthRuleValidation1->isValid());
var_dump($lengthRuleValidation1->getErrors());
$rule = new Length(10);
$lengthRuleValidation2 = $rule->safeParse('some string here');
var_dump($lengthRuleValidation2->isValid());
var_dump($lengthRuleValidation2->getErrors());
$rule = new Length(max: 10);
$lengthRuleValidation3 = $rule->safeParse('some string here');
var_dump($lengthRuleValidation3->isValid());
var_dump($lengthRuleValidation3->getErrors());
Use the MinLength rule directly-
$validationResult = (new MinLength(10))->safeParse('abc');
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
Use the MaxLength rule directly-
$validationResult = (new MaxLength(10))->safeParse('abc def ghi jkl mno');
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
Use Length Rule Builder
The LengthRuleBuilder
allows you to build length rules in a more flexible manner:
$lengthRule = (new LengthRuleBuilder())->setMinLength(5)->setMaxLength(15)->build();
$lengthValidationWithBuilder = $lengthRule->safeParse('my sample string here');
Use Validation Builder
The ValidationBuilder
provides a fluent interface for building validation rules:
$validation = ValidationBuilder::length(20, 25)->safeParse('some sample string');
var_dump($validation->isValid());
var_dump($validation->getErrors());
The ValidationBuilder
can use the minLength directly-
$validation = ValidationBuilder::minLength(20)->safeParse('some sample string');
var_dump($validation->isValid());
var_dump($validation->getErrors());
The ValidationBuilder
also can use the maxLength-
$validation = ValidationBuilder::maxLength(10)->safeParse('some sample string');
var_dump($validation->isValid());
var_dump($validation->getErrors());
The ValidationBuilder
can use the minLength and maxLength together-
$validation = ValidationBuilder::minLength(10)->maxLength(100)->safeParse('some sample string');
var_dump($validation->isValid());
var_dump($validation->getErrors());
Full Example Code
Here is the full example-
<?php
require_once "vendor/autoload.php";
use Webhkp\Pvalidate\Rules\Builder\LengthRuleBuilder;
use Webhkp\Pvalidate\Rules\MinLength;
use Webhkp\Pvalidate\Validator;
use Webhkp\Pvalidate\Rules\Length;
use Webhkp\Pvalidate\Rules\MaxLength;
use Webhkp\Pvalidate\ValidationBuilder;
// #[Attribute]
class MyClass {
public function __construct(
) {
}
#[Length(2, 5)]
public string $firstString = 'my first string';
#[Length(min: 20)]
public string $secondString = 'my second string';
#[Length(max: 10)]
public string $thirdString = 'my third string';
#[Length(min: 20, max: 50)]
public string $fourthString = 'my fourth string';
#[Length(min: 10, max: 20)]
public string $fifthString = 'my fifth string';
#[Length(min: 100, max: 20)]
public string $sixthString = 'my sixth string';
#[MinLength(10)]
public string $minLengthTestString = 'abc';
#[MaxLength(10)]
public string $maxLengthTestString = 'abc def ghi jkl';
}
// Demo
// Create object
$myObj = new MyClass();
// Validate the object
$validationResponse = Validator::validate($myObj);
var_dump($validationResponse->isValid());
var_dump($validationResponse->getErrors());
// Use length validation rule directly
$lengthRuleValidation1 = (new Length(10, 20))->safeParse('some string here');
var_dump($lengthRuleValidation1->isValid());
var_dump($lengthRuleValidation1->getErrors());
$rule = new Length(10);
$lengthRuleValidation2 = $rule->safeParse('some string here');
var_dump($lengthRuleValidation2->isValid());
var_dump($lengthRuleValidation2->getErrors());
$rule = new Length(max: 10);
$lengthRuleValidation3 = $rule->safeParse('some string here');
var_dump($lengthRuleValidation3->isValid());
var_dump($lengthRuleValidation3->getErrors());
// Use MinLength rule directly
$validationResult = (new MinLength(10))->safeParse('abc');
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
// Use MaxLength rule directly
$validationResult = (new MaxLength(10))->safeParse('abc def ghi jkl mno');
var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());
// Use length rule builder
$lengthRule = (new LengthRuleBuilder())->setMinLength(5)->setMaxLength(15)->build();
$lengthValidationWithBuilder = $lengthRule->safeParse('my sample string here');
// Use Validation builder
$validation = ValidationBuilder::length(20, 25)->safeParse('some sample string');
var_dump($validation->isValid());
var_dump($validation->getErrors());
// var_dump($validation->getMessages());
$validation = ValidationBuilder::minLength(20)->safeParse('some sample string');
var_dump($validation->isValid());
var_dump($validation->getErrors());
// var_dump($validation->getMessages());
$validation = ValidationBuilder::maxLength(10)->safeParse('some sample string');
var_dump($validation->isValid());
var_dump($validation->getErrors());
// var_dump($validation->getMessages());
Output:
bool(false)
array(7) {
["firstString"]=>
array(3) {
["value"]=>
string(15) "my first string"
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["maxLength"]=>
string(55) "firstString length should be smaller than or equal to 5"
}
}
["secondString"]=>
array(3) {
["value"]=>
string(16) "my second string"
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["minLength"]=>
string(56) "secondString length should be larger than or equal to 20"
}
}
["thirdString"]=>
array(3) {
["value"]=>
string(15) "my third string"
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["maxLength"]=>
string(56) "thirdString length should be smaller than or equal to 10"
}
}
["fourthString"]=>
array(3) {
["value"]=>
string(16) "my fourth string"
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["minLength"]=>
string(56) "fourthString length should be larger than or equal to 20"
}
}
["sixthString"]=>
array(3) {
["value"]=>
string(15) "my sixth string"
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["wrongParam"]=>
string(65) "max value (20) should be greater than or equal to min value (100)"
}
}
["minLengthTestString"]=>
array(3) {
["value"]=>
string(3) "abc"
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["minLength"]=>
string(63) "minLengthTestString length should be larger than or equal to 10"
}
}
["maxLengthTestString"]=>
array(3) {
["value"]=>
string(15) "abc def ghi jkl"
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["maxLength"]=>
string(64) "maxLengthTestString length should be smaller than or equal to 10"
}
}
}
bool(true)
array(0) {
}
bool(true)
array(0) {
}
bool(false)
array(1) {
["maxLength"]=>
string(45) " length should be smaller than or equal to 10"
}
bool(false)
array(1) {
["minLength"]=>
string(44) " length should be larger than or equal to 10"
}
bool(false)
array(1) {
["maxLength"]=>
string(45) " length should be smaller than or equal to 10"
}
bool(false)
array(1) {
["length"]=>
array(3) {
["value"]=>
string(18) "some sample string"
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["minLength"]=>
string(44) " length should be larger than or equal to 20"
}
}
}
bool(false)
array(1) {
["minLength"]=>
array(3) {
["value"]=>
string(18) "some sample string"
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["minLength"]=>
string(44) " length should be larger than or equal to 20"
}
}
}
bool(false)
array(1) {
["maxLength"]=>
array(3) {
["value"]=>
string(18) "some sample string"
["valid"]=>
bool(false)
["errors"]=>
array(1) {
["maxLength"]=>
string(45) " length should be smaller than or equal to 10"
}
}
}