samsonasik/naming

Naming Filter and Validator for people name

3.0.1 2023-01-15 22:06 UTC

This package is auto-updated.

Last update: 2024-10-08 17:06:56 UTC


README

Latest Version ci build Code Coverage PHPStan Downloads

Naming is a library that has filter and validator for people name with multibyte string check support, extends the Laminas filter and validator, while it can be used as standalone.

Installation

composer require samsonasik/naming

Filter Flow

  • Strip Tags
  • String Trim
  • String To Upper first letter in each word with set lower case after that
  • Replace double space to single space
  • String To Upper after ' and - character if any

Examples:

Validation checks

  • Allowed characters: letters, hyphens, apostrophe, spaces, full stops.
  • Not allowed:
    • include number
    • special characters
    • single . character
    • single - character
    • single ' character
    • consecutive . characters
    • consecutive - characters
    • consecutive ' characters
    • full stops not in the last of each word

Usage with laminas-form instance:

use Naming\Filter;
use Naming\Validator;
use Laminas\Form\Element\Text;
use Laminas\Form\Form;
use Laminas\InputFilter\InputFilterProviderInterface;

class ContactForm extends Form implements InputFilterProviderInterface
{
    public function init()
    {
        $this->add([
            'type' => Text::class,
            'name' => 'fullname',
            'options' => [
                'label' => 'Full name',
            ],
        ]);
    }

    public function getInputFilterSpecification()
    {
        return [
            [
                'name'     => 'fullname',
                'required' => true,
                'filters' => [
                    [
                        'name' => Filter\Naming::class
                    ],
                ],
                'validators' => [
                    [
                        'name' => Validator\Naming::class,
                    ],
                ],
            ],
        ];
    }
}

Using standalone:

use Naming\Filter;
use Naming\Validator;

include 'vendor/autoload.php';

// ... VALID
$filtered = (new Filter\Naming())->filter('Abdul malik ikhsan');
$validator = new Validator\Naming();

echo $filtered; // Abdul Malik Ikhsan
var_dump($validator->isValid($filtered)); // true

// ... INVALID
$filtered = (new Filter\Naming())->filter('Abdul....');
$validator = new Validator\Naming();

echo $filtered; // Abdul....
var_dump($validator->isValid($filtered)); // false
var_dump(\current($validator->getMessages())); /* "Consecutive "."s are not allowed" */

Contributing

Contributions are very welcome. Please read CONTRIBUTING.md