PHP code example of cpana / cnp

1. Go to this page and download the library: Download cpana/cnp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cpana / cnp example snippets


use CPANA\CNP\CodNumericPersonal;
use CPANA\CNP\Exception\CNPExceptionInterface;

...
try {
    $cnpObj = new CodNumericPersonal($inputString);
} catch (CNPExceptionInterface $e) {
    // handle error
} 


use CPANA\CNP\CodNumericPersonal;
use CPANA\CNP\Exception\GenericInvalidCNPException;
use CPANA\CNP\Exception\InvalidCNPLengthCNPException;
use CPANA\CNP\Exception\NonNumericValueCNPException;
use CPANA\CNP\Exception\CNPExceptionInterface;

...
try {
    $cnpObj = new CodNumericPersonal($inputString);
} catch (InvalidCNPLengthCNPException $e) {
    // Ex: Display message to user
    
} catch (NonNumericValueCNPException $e) {
    // Ex: Display message to user
    
} catch (GenericInvalidCNPException $e) {
    $code = $e->getCode();
    // take decision based on error code
    switch ($code) {
        case GenericInvalidCNPException::EXCEPTION_COUNTY:
            // take some action like  highlight wrong digits for county JJ
            echo 'Error code:'.$e->getCode().' '.$e->getMessage().PHP_EOL;
            break;
        case GenericInvalidCNPException::EXCEPTION_GENDER:
            // take some specific action ..
            break;
        case GenericInvalidCNPException::EXCEPTION_YEAR:
            // take some specific action ..
            break;
        case GenericInvalidCNPException::EXCEPTION_MONTH:
            // take some specific action ..
        case GenericInvalidCNPException::EXCEPTION_DAY:
            // take some specific action ..
            break;
    }
} catch (CNPExceptionInterface $e) {
    // do something
} catch (\Exception $e) {
    // do something
}