PHP code example of okaybueno / validation

1. Go to this page and download the library: Download okaybueno/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/ */

    

okaybueno / validation example snippets





namespace MyApp\Validators\Users;

interface UserValidatorInterface
{
    const EXISTS_BY_ID = 'existsById';
    const EXISTS_BY_EMAIL = 'existsByEmail';
    
    public function existsById();
    public function existsByEmail();
}





namespace MyApp\Validators\Users\src;

use OkayBueno\Validation\src\LaravelValidator;

class UserValidator extends LaravelValidator implements UserValidatorInterface
{   

    public function existsById()
    {
        return [
            'id'    => '




namespace MyApp\Services\Frontend\Users\src;

use MyApp\Validators\Users\UsersValidatorInterface;

class UsersService implements UsersServicesInterface 
{

    protected $usersValidator;

    public function __construct(
        UsersValidatorInterface $usersValidatorInterface
    )
    {
        $this->usersValidator = $usersValidatorInterface;
    }
   
    
    public function findUserById( $userId )
    {
        $data = [
            'id' => $userId
        ]
        
        if ( $this->usersValidator->with( $data )->passes( UsersValidatorInterface::EXISTS_BY_ID ) )
        {
            // It passes the validation, so do whatever.. fetch user in $user and return it (for example).
            
            .
            .
            
            return $user;
        }
        
        // if we are at this point then the validation failed and this will return an array with the errors.
        return $this->usersValidator->errors();
    }
    
}