yivoff/spanish-nif

Tool to verify and generate Spanish NIFs / Herramienta para verificar y generar NIFs de España

Installs: 90

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

v1.0.0 2022-01-31 16:38 UTC

This package is auto-updated.

Last update: 2024-10-29 06:13:07 UTC


README

PHP Version Require Latest Stable Version Total Downloads Latest Unstable Version License Tests codecov

Simple library to check that fiscal identifiers are valid according to Spanish legislation, and to generate valid fiscal identifiers.

Installation

composer require yivoff/nif-check

Usage

$checker = new \Yivoff\NifCheck\NifChecker();

// returns true if valid
$checker->verify($anyNif);

NIF Generator

An utility is provided to generate valid NIFs, which could be useful for testing purposes or similar scenarios.

$generator = new \Yivoff\NifCheck\Generator\RandomNif();

// Generates a valid DNI
$validDni = $generator->generateDni();
 
// Generates a valid NIE
$validNie = $generator->generateNie();
 
// Generates a valid CIF
$validCif = $generator->generateCif();
 
// Generates a valid NIF (randomly DNI, NIF, or CIF)
$validNif = $generator->generate();

Symfony Validator integration

The package provides a Symfony Validator attribute, which can simply be used like this:

use Yivoff\NifCheck\Validator\ValidNif;

class User
{
    #[ValidNif]
    public string $nif;
}

fakerphp/faker Integration

The package also includes a Faker Provider.

$faker = Faker\Factory::create();
$faker->addProvider(new \Yivoff\NifCheck\FakerProvider\NifProvider($faker, new \Yivoff\NifCheck\Generator\RandomNif()));

// now you can fake NIFs

$faker->spanishDni();
$faker->spanishNie();
$faker->spanishCif();
$faker->spanishNif();

See:

  1. https://es.wikipedia.org/wiki/N%C3%BAmero_de_identificaci%C3%B3n_fiscal
  2. https://es.wikipedia.org/wiki/C%C3%B3digo_de_identificaci%C3%B3n_fiscal