PHP code example of neuron-php / validation

1. Go to this page and download the library: Download neuron-php/validation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

neuron-php / validation example snippets


use Neuron\Validation\IsEmail;

$Validator = new IsEmail();

if( $Validator->isValid( '[email protected]' ) )
{
	// accepted
}

use Neuron\Validation\Collection;
use Neuron\Validation\IsInteger;
use Neuron\Validation\IsPositive;

$Rules = new Collection();
$Rules->add( 'positive', new IsPositive() );
$Rules->add( 'integer', new IsInteger() );

if( !$Rules->isValid( -1 ) )
{
	$Failed = $Rules->getViolations(); // [ 'positive' ]
}

$Rules->get( 'positive' );
$Rules->remove( 'positive' );

use Neuron\Validation\IsEmail;
use Neuron\Validation\Policy;

class Registration
{
	use Policy;

	public function __construct()
	{
		$this->addRule( 'email', new IsEmail() );
	}

	public function emailIsValid( string $Email ): bool
	{
		return $this->isRuleValid( 'email', $Email );
	}
}

use Neuron\Validation\IsDate;
use Neuron\Validation\IsDateTime;
use Neuron\Validation\IsTime;

$Date = new IsDate();
$Date->setFormat( 'Y-m-d' );
$Date->isValid( '2015-01-01' );

$DateTime = new IsDateTime();
$DateTime->isValid( '2015-01-01 14:15:16' );

$Time = new IsTime();
$Time->isValid( '1:30:23 PM' );

use Neuron\Data\Objects\DateRange;
use Neuron\Validation\IsDateWithinRange;

$Validator = new IsDateWithinRange(
	new DateRange( '2024-01-01', '2024-12-31' )
);

$Validator->isValid( '2024-06-01' );

use Neuron\Data\Objects\DateRange;
use Neuron\Validation\IsDateRange;

$Validator = new IsDateRange();
$Validator->isValid( new DateRange( '2000-01-01', '2000-01-03' ) );

use Neuron\Data\Objects\NumericRange;
use Neuron\Validation\IsCurrency;
use Neuron\Validation\IsNumberWithinRange;
use Neuron\Validation\IsNumericRange;

( new IsCurrency() )->isValid( '1.01' );

$Within = new IsNumberWithinRange( new NumericRange( 1, 10 ) );
$Within->isValid( 10 );

$Range = new IsNumericRange();
$Range->isValid( new NumericRange( 1, 10 ) );

use Neuron\Validation\IsInSet;
use Neuron\Validation\IsPassword;
use Neuron\Validation\IsRegExPattern;
use Neuron\Validation\IsStringLength;

( new IsStringLength( 3, 10 ) )->isValid( 'test' );

( new IsInSet( [ 'admin', 'editor' ] ) )->isValid( 'admin' );
( new IsInSet( [ 1, 2, 3 ], false ) )->isValid( '2' );

( new IsRegExPattern( '/^SKU-\d+$/' ) )->isValid( 'SKU-100' );

$Password = new IsPassword(
	minLength: 12,
	maxLength: 64,
	

use Neuron\Validation\IsPhoneNumber;

$Us = new IsPhoneNumber();
$Us->isValid( '941-248-3500' );

$International = new IsPhoneNumber( IsPhoneNumber::INTERNATIONAL );
$International->isValid( '+49 89 636 48098' );

use Neuron\Validation\IsBase64;

$Validator = new IsBase64();
$Validator->isValid( 'SGVsbG8=' );

use Neuron\Validation\IsImage;

new IsImage();
new IsImage( [ 'image/png' ], 1024 * 1024 );
new IsImage( allowedMimeTypes: [ 'image/png' ], allowSvg: true );

use Neuron\Validation\IsBoolean;

( new IsBoolean() )->isValid( true );
( new IsBoolean( true ) )->isValid( 'yes' );

use Neuron\Validation\Base;

class IsEven extends Base
{
	protected function validate( mixed $Value ): bool
	{
		return is_int( $Value ) && $Value % 2 === 0;
	}
}
bash
composer