Login   Register  
PHP Classes
elePHPant
Icontem

File: class.cnp.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of the pop  >  CNP validator  >  class.cnp.php  >  Download  
File: class.cnp.php
Role: Class source
Content type: text/plain
Description: `CNP` class source code
Class: CNP validator
Validate and extract details from a Romanian CNP
Author: By
Last change: # validator recognizes the CNP which start with 1-9
# class returns data about residence and origin of owner
Date: 1 year ago
Size: 5,215 bytes
 

Contents

Class file image Download
<?php
/*
 * @author: Iosif Chiriluta
 * @description: This class checks if a CNP is valid or not. If the CNP is valid, this class pull out the information about the possessor of given CNP.
 * @release date: 10/13/2011
 * @last update: 08/27/2012
 * @version: 2.0
 */
class CNP {

    
/**
     * @access public 
     * @var integer
     */
    
public $error;    

    
/**
     * @access protected
     * @var string
     */
    
protected $cnp;

    
/**
     * @access protected
     * @var string[]
     */
    
protected $cities = array(
        
=> "Alba",
        
=> "Arad",
        
=> "Arges",
        
=> "Bacau",
        
=> "Bihor",
        
=> "Bistrita-Nasaud",
        
=> "Botosani",
        
=> "Brasov",
        
=> "Braila",
        
10 => "Buzau",
        
11 => "Caras-Severin",
        
12 => "Cluj",
        
13 => "Constanta",
        
14 => "Covasna",
        
15 => "Dambovita",
        
16 => "Dolj",
        
17 => "Galati",
        
18 => "Gorj",
        
19 => "Harghita",
        
20 => "Hunedoara",
        
21 => "Ialomita",
        
22 => "Iasi",
        
23 => "Ilfov",
        
24 => "Maramures",
        
25 => "Mehedinti",
        
26 => "Mures",
        
27 => "Neamt",
        
28 => "Olt",
        
29 => "Prahova",
        
30 => "Satu Mare",
        
31 => "Salaj",
        
32 => "Sibiu",
        
33 => "Suceava",
        
34 => "Teleorman",
        
35 => "Timis",
        
36 => "Tulcea",
        
37 => "Vaslui",
        
38 => "Valcea",
        
39 => "Vrancea",
        
41 => "Bucuresti/Sectorul 1",
        
42 => "Bucuresti/Sectorul 2",
        
43 => "Bucuresti/Sectorul 3",
        
44 => "Bucuresti/Sectorul 4",
        
45 => "Bucuresti/Sectorul 5",
        
46 => "Bucuresti/Sectorul 6",
        
51 => "Calarasi",
        
52 => "Giurgiu"
    
);

    
/**
     * @access protected
     * @var string[]
     */
    
protected $months = array(
        
=> "Ianuarie",
        
=> "Februarie",
        
=> "Martie",
        
=> "Aprilie",
        
=> "Mai",
        
=> "Iunie",
        
=> "Iulie",
        
=> "August",
        
=> "Septembrie",
        
10 => "Octombrie",
        
11 => "Noiembrie",
        
12 => "Decembrie"
    
);

    
/**
     * @access public
     * @param string $cnp The CNP that will be validated
     * @return void|boolean
     */
    
public function __construct($cnp) {
        
        
$this->cnp $cnp;

        if (
FALSE === $this->_validate())
            return 
FALSE;
                
        
$this->genre     = (int)substr($this->cnp01);
        
$this->month     = (int)substr($this->cnp32);
        
$this->day         = (int)substr($this->cnp52);
        
$this->city     = (int)substr($this->cnp72);
        
        if( (
$this->month || 12 $this->month) || ($this->day || 31 $this->day) || (!isset($this->cities[$this->city])) ) {
            
$this->error 3;
            return 
FALSE;
        }
    }
    
    
/**
     * @access public
     * @return string[] Returns all information extracted from CNP
     */  
    
public function fetchAllData() {
        return array(
            
"genre"     => $this->getGenre(),
            
"year"         => $this->getYear(),
            
"month"     => $this->getMonth(),
            
"day"         => $this->getDay(),
            
"city"         => $this->getCity(),
            
"resident"     => $this->getResidentInfo(),
            
"stranger"    => $this->getStrangerInfo()
        );
    }

    
/**
     * @access public
     * @return boolean Checks and returns if the owner of CNP lives in Romania
     */
    
public function getResidentInfo() {
        return 
in_array($this->genre, array(78)) ? TRUE FALSE;
    }
    
/**
     * @access public
     * @return boolean Checks and returns if the owner of CNP has Romanian orgins
     */
    
public function getStrangerInfo() {
        return 
in_array($this->genre, array(789)) ? TRUE FALSE;
    }

     
/**
      * @access public
      * @return string[] Returns an array composed of number and initials genre
      */
    
public function getGenre() {
        return array(
            
$this->genre,
            
in_array($this->genre, array(1357)) ? "m" : (
                
in_array($this->genre, array(2468)) ? "f" "n/a"
            
)
        );
    }

    
/**
     * @access public
     * @return string Returns the year extracted from CNP
     */
    
public function getYear() {
        return (
                
in_array($this->genre, array(12)) ? 19 : (
                    
in_array($this->genre, array(34)) ? 18 : (
                        
in_array($this->genre, array(56)) ? 20 NULL
                    

                )
            ) . 
            
substr($this->cnp12);
    }

    
/**
     * @access public
     * @return integer[] Returns an array composed of number and name of month extracted from CNP
     */ 
    
public function getMonth() {
        return array(
            
$this->month,
            
$this->months[$this->month]
        );
    }

    
/**
     * @access public
     * @return mixed[] Returns an array composed of month day and week name of day extracted from CNP
     */
    
public function getDay() {
        return array(
            
$this->day,
            
date("l"strtotime("{$this->getYear()}/{$this->month}/{$this->day}"))
        );
    } 

    
/**
     * @access public
     * @return mixed[] Returns an array composed of name of county and number extracted from CNP
     */
    
public function getCity() {
        return array (
            
$this->city,
            
$this->cities[$this->city]
        );
    }

    
/**
     * @access private
     * @return boolean Checks if the CNP is valid
     */
    
private function _validate() {
        
$key "279146358279";
        
        if(
13 !== strlen($this->cnp)) {
            
$this->error 1;
            return 
FALSE;
        }

        
$s 0;

        for(
$i 0$i <= 11$i++)
            
$s += $this->cnp[$i] * $key[$i];
        
        
$s %= 11;

        if((
10 === $s && "1" !== $this->cnp[12]) || (10 $s && $s != $this->cnp[12])) {
            
$this->error 2;
            return 
FALSE;
        }

        return 
TRUE;
    }
}
?>