ulabox/nif-validator

Yet another(?) PHP implementation of the spanish NIF validator

v1.3.0 2023-11-14 14:53 UTC

This package is not auto-updated.

Last update: 2024-02-21 14:00:39 UTC


README

Build Status

A modern PHP 7.0+ Spanish NIF (Número de Indentifación Fiscal) validator.

Why another NIF validator?

Other NIF validators we saw either had really obscure code or just implemented a validator for the DNI/NIE, not the CIF.

Installation

Using Composer:

composer require ulabox/nif-validator

Usage

<?php

use NifValidator\NifValidator;

//CIF
assert(NifValidator::isValid('B65410011'));
//DNI
assert(NifValidator::isValid('93471790C'));
//NIE
assert(NifValidator::isValid('X5102754C'));

Starting from version 1.1.x you can also separetely validate personal and entity nifs

<?php

use NifValidator\NifValidator;

//CIF
assert(NifValidator::isValidEntity('B65410011'));
//DNI
assert(NifValidator::isValidPersonal('93471790C'));
//NIE
assert(NifValidator::isValidPersonal('X5102754C'));

Starting from version 1.2.x you can also separetely validate individual nif types

<?php

use NifValidator\NifValidator;

//CIF
assert(NifValidator::isValidCif('B65410011'));
//DNI
assert(NifValidator::isValidDni('93471790C'));
//NIE
assert(NifValidator::isValidNie('X5102754C'));


This validator does not strip or uppercase any character, it's your responsibility to previously filter the input.