Pvalidate: Start and End with [Validation Rule]

Here is how we can use the StartWith and EndWith rules to validate a string.

Import required classes-

use Webhkp\Pvalidate\Rules\EndWith;
use Webhkp\Pvalidate\Validator;
use Webhkp\Pvalidate\Rules\StartWith;
use Webhkp\Pvalidate\ValidationBuilder;

Object Validation

Define a class (MyClass) with properties that require validation:

class MyClass {
    public function __construct(
    ) {

    }

    #[StartWith('my')]
    public string $firstString = 'my first string';    
    
    #[StartWith('my')]
    public string $secondString = 'My second string';    
    
    #[StartWith('MY', true)]
    public string $thirdString = 'My third string';    
    
    #[EndWith('string')]
    public string $fourthString = 'my fourth string';    
    
    #[EndWith('STRING')]
    public string $fifthString = 'My fifth StrinG';    
    
    #[EndWith('string', true)]
    public string $sixthString = 'My sixth String';    

    #[StartWith('MY')]
    #[EndWith('string')]
    public string $seventhString = 'My seventh String';    

    #[StartWith('MY', true)]
    #[EndWith('STRING', true)]
    public string $eighthString = 'My eighth String';    
}

An instance of MyClass is created, and the Validator::validate method is used to validate the object:

$myObj = new MyClass();

// Validate the object
$validationResponse = Validator::validate($myObj);

var_dump($validationResponse->isValid());
var_dump($validationResponse->getErrors());

Direct Usage of Validation Rules

The script demonstrates how to use the StartWith validation rule directly on a string:

$startValidationResponse = (new StartWith("sone"))->safeParse('some string here');

var_dump($startValidationResponse->isValid());
var_dump($startValidationResponse->getErrors());

Similarly, it shows how to use the EndWith validation rule:

$rule = new EndWith("test");
$endWithValidation = $rule->safeParse('some string here');

var_dump($endWithValidation->isValid());
var_dump($endWithValidation->getErrors());

StartWith and EndWith Rules Builder

The script demonstrates the use of ValidationBuilder to chain multiple validation rules:

$validation = ValidationBuilder::startWith("some")->endWith('string')->safeParse('some sample string');

var_dump($validation->isValid());
var_dump($validation->getErrors());
// var_dump($validation->getMessages());

Full Example Code

Here is the full example code-

<?php

require_once "vendor/autoload.php";

use Webhkp\Pvalidate\Rules\EndWith;
use Webhkp\Pvalidate\Validator;
use Webhkp\Pvalidate\Rules\StartWith;
use Webhkp\Pvalidate\ValidationBuilder;

class MyClass {
    public function __construct(
    ) {

    }

    #[StartWith('my')]
    public string $firstString = 'my first string';    
    
    #[StartWith('my')]
    public string $secondString = 'My second string';    
    
    #[StartWith('MY', true)]
    public string $thirdString = 'My third string';    
    
    #[EndWith('string')]
    public string $fourthString = 'my fourth string';    
    
    #[EndWith('STRING')]
    public string $fifthString = 'My fifth StrinG';    
    
    #[EndWith('string', true)]
    public string $sixthString = 'My sixth String';    

    #[StartWith('MY')]
    #[EndWith('string')]
    public string $seventhString = 'My seventh String';    

    #[StartWith('MY', true)]
    #[EndWith('STRING', true)]
    public string $eighthString = 'My eighth String';    
}


// Demo

// Create object
$myObj = new MyClass();

// Validate the object
$validationResponse = Validator::validate($myObj);

var_dump($validationResponse->isValid());
var_dump($validationResponse->getErrors());

// Use Start validation rule directly
$startValidationResponse = (new StartWith("sone"))->safeParse('some string here');

var_dump($startValidationResponse->isValid());
var_dump($startValidationResponse->getErrors());

// Check end with as rule
$rule = new EndWith("test");
$endWithValidation = $rule->safeParse('some string here');

var_dump($endWithValidation->isValid());
var_dump($endWithValidation->getErrors());

// Check with validation builder
$validation = ValidationBuilder::startWith("some")->endWith('string')->safeParse('some sample string');

var_dump($validation->isValid());
var_dump($validation->getErrors());
// var_dump($validation->getMessages());

Output:

bool(false)
array(3) {
  ["secondString"]=>
  array(3) {
    ["value"]=>
    string(16) "My second string"
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["startWith"]=>
      string(43) "(My second string) does not start with (my)"
    }
  }
  ["fifthString"]=>
  array(3) {
    ["value"]=>
    string(15) "My fifth StrinG"
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["endWith"]=>
      string(44) "(My fifth StrinG) does not end with (STRING)"
    }
  }
  ["seventhString"]=>
  array(3) {
    ["value"]=>
    string(17) "My seventh String"
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["endWith"]=>
      string(46) "(My seventh String) does not end with (string)"
    }
  }
}

bool(false)
array(1) {
  ["startWith"]=>
  string(45) "(some string here) does not start with (sone)"
}

bool(false)
array(1) {
  ["endWith"]=>
  string(43) "(some string here) does not end with (test)"
}

bool(true)
array(0) {
}

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.