lucasvdh/laravel-iban

A Laravel plugin for IBAN validation

dev-master 2017-11-21 09:40 UTC

This package is auto-updated.

Last update: 2024-11-07 00:38:06 UTC


README

IBAN validation made easy.

license license license php-v

Getting started

  1. Include the package in your application
  2. Register the service provider
  3. Example usage

Include the package in your application

$ composer require lucasvdh/laravel-iban:5.*

Or add a requirement to your project's composer.json

"require": {
	"lucasvdh/laravel-iban": "5.*"
},

TODO: register package at the packagist

Register the service

Add the following to your config/app.php

<?php

return [

    ...

    'providers' => [
        ... 
        
        IBAN\ServiceProvider::class,
    ],
    
    ...
    
    // Optionally you can register the Facade alias
    'aliases' => [
        ...

        'IBAN' => IBAN\Facades\IBAN::class,
    ],
    
];

Example usages

IBAN validation can be used in the following ways:

  1. Via the facade
  2. Via dependency injection
  3. Via validation rules

Via the facade

function validateIBAN($iban) 
{
    echo $iban;
    
    if (\IBAN::validate($iban)) {
        echo 'is a valid IBAN';
    } else {
        echo 'is NOT valid IBAN';
    }
    
    echo "\r\n";
}

validateIBAN('NL39 RABO 0300 0652 64');
validateIBAN('123456789');

This results in

NL39 RABO 0300 0652 64 is a valid IBAN
123456789 is NOT a valid IBAN

Via dependency injection

<?php namespace App\Http\Controllers;

use IBAN\Services\IBANService;

class IBANController extends Controller
{
    
    public function validate(IBANService $IBANService, $iban)
    {
        if ($IBANService->validate($iban)) {
            return response()->json([
                'message' => $iban . 'is a valid IBAN'
            ]);
        } else {
            return response()->json([
                'message' => $iban . 'is NOT a valid IBAN'
            ]);
        }
    }
}

Via validation rules

$validator = Validator::make(
    ['required_iban_field' => 'required|iban'],
    ['optional_iban_field' => 'sometimes|iban'],
);