robertogallea / laravel-codicefiscale

Codice fiscale validation for php/laravel
MIT License
42 stars 16 forks source link

Add new tryParse method to CodiceFiscale that doesn't throw an error #44

Closed JBou closed 2 years ago

JBou commented 2 years ago

Description

This PR adds a new method called tryParse that doesn't throw an exception, instead it returns a bool. It also fixes the samples in the README on How to use the Utility CodiceFiscale class to correspond to the actual behavior of the CodiceFiscale class. Fixes #43 Please check if the documentation is good like this or if I have to change something.

Motivation and context

It simplifies some cases, for example when using it in a blade view: Before:

@php
    try {
        $cf = new robertogallea\LaravelCodiceFiscale\CodiceFiscale();
        $result = $cf->parse($registration->tax_number);
    } catch (Exception $ex) {
        $result = false;
        $cf_error = $ex;
    }
@endphp
@if($result == true)
    <i class="fa fa-check" style="color:green" data-toggle="tooltip" data-placement="top" title="{{$cf->getCodiceFiscale()}}"></i>
@else
    <i class="fa fa-warning" style="color:red" data-toggle="tooltip" data-placement="top" title="{{$cf_error->getMessage()}}"></i>
@endif

After:

@php($cf = new robertogallea\LaravelCodiceFiscale\CodiceFiscale())
@if($cf->tryParse($registration->tax_number))
    <i class="fa fa-check" style="color:green" data-toggle="tooltip" data-placement="top" title="{{$cf->getCodiceFiscale()}}"></i>
@else
    <i class="fa fa-warning" style="color:red" data-toggle="tooltip" data-placement="top" title="{{$cf->getError()->getMessage()}}"></i>
@endif

How has this been tested?

I have added 2 tests to test the new method

Screenshots (if appropriate)

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

Checklist:

robertogallea commented 2 years ago

Thanks for this PR!