twoh/validator

This project contains the basic Implementation of Validators in PHP DocBlocks.

1.0.7 2025-01-20 08:36 UTC

This package is auto-updated.

Last update: 2025-02-20 09:03:01 UTC


README

TWOH Logo

Build Status License

About Validator

This project contains the basic Implementation of Validators in PHP DocBlocks.

Requirements

  • PHP 8.3 or higher
  • Composer

Installation

composer req twoh/validator

Usage

Custom Validator Example

Here is an example of a custom validator. Always extend from the ValidatorAdapter class.

<?php

declare(strict_types=1);

namespace TWOH\Validator\Examples;

use TWOH\Validator\Adapters\ValidatorAdapter;

class UserValidator extends ValidatorAdapter
{
    /**
     * @param string $value
     * @return bool
     */
    protected function validateNotEmpty(
        string $value
    ): bool
    {
        return !empty($value);
    }

    /**
     * @param $value
     * @return bool
     */
    protected function validateIsMail($value): bool
    {
        return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
    }

    /**
     * @param string $value
     * @return bool
     */
    protected function validateStrengthPassword(
        string $value
    ): bool
    {
        $hasUppercase = preg_match('@[A-Z]@', $value);
        $hasLowercase = preg_match('@[a-z]@', $value);
        $hasNumber = preg_match('@[0-9]@', $value);
        $hasSpecialChar = preg_match('@[^\w]@', $value);

        return $hasUppercase && $hasLowercase && $hasNumber && $hasSpecialChar;
    }

    /**
     * @param string $value
     * @param array $params
     * @return bool
     */
    protected function validateLength(
        string $value,
        array $params
    ): bool
    {
        $length = strlen($value);
        return $length >= $params['min'] && $length <= $params['max'];
    }
}

Custom Validator Model

Here is an example of a model that uses the custom validators. It test the email and password. Therefore the functions NotEmpty, IsMail, StrengthPassword and Length are used and will be simliar as defined in UserValidator. The only two things that is different is the @ sign in front of the function name and the missing validate prefix.

<?php

declare(strict_types=1);

namespace TWOH\Validator\Examples\Models;

class User
{
    /**
     * @NotEmpty
     * @IsMail
     */
    public string $email;

    /**
     * @NotEmpty
     * @StrengthPassword
     * @Length(min=12, max=16)
     */
    public string $password;
}

Custom Validator Usage

// define model
$user = new User();

// setparams
$user->email = 'a.reichel91@outlook.com';
$user->password = 'HelloWorld???10';

// init validator
$validator = new UserValidator();
$errors = $validator->validate($user);

If the validation fails, the $errors array will contain the error messages.

Existing validators

  • MailValidator
    /**
    
  • @NotEmpty - check if value is not empty
  • @IsMail - check if mail is validated correctly */
    
    
  • PasswordValidator
    /**
    
  • @NotEmpty - check if value is not empty
  • @StrengthPassword - check if password matches given password strength
  • @Length - check if is long enough */
    
    

Running Tests

To verify that the tests are passing, run the following command:

vendor/bin/phpunit