Location: PHPKode > scripts > PESEL validator > pesel.php
<?php

/**
 * PESEL Validator (part of Lotos Framework)
 *
 * Copyright (c) 2005-2010 Artur Graniszewski (hide@address.com) 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * - Neither the name of the Lotos Framework nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category   Library
 * @package    Lotos
 * @subpackage Validators\Pl\Pesel
 * @copyright  Copyright (c) 2005-2011 Artur Graniszewski (hide@address.com)
 * @license    New BSD License
 * @version    $Id$
 */
class Pesel 
{
	/**
	 * String containing polish national identification number.
	 * 
	 * @var string
	 */
	protected $peselString;
	
	/**
	 * Day of birth.
	 * 
	 * @var int
	 */
	protected $dayOfBirth;
	
	/**
	 * Month of birth.
	 * 
	 * @var int
	 */
	protected $monthOfBirth;
	
	/**
	 * Year of birth.
	 * 
	 * @var int
	 */
	protected $yearOfBirth;
	
	/**
	 * Sex.
	 * 
	 * 0 = male, 1 = female
	 * 
	 * @var int
	 */
	protected $sex;
	
	/**
	 * Creates PESEL object and validates it's contents.
	 * 
	 * @param string $peselString String containing elevel digits of the PESEL number.
	 * @return Pesel
	 */
	public function __construct($peselString) {
		$this->peselString = $peselString;
		$this->validate();		
	}
	
	/**
	 * Validates PESEL number;
	 *
	 * @return void 
	 */
	protected function validate() {
		if(!preg_match('~^[\d]{11}$~', $this->peselString)) {
			throw new Exception('PESEL should contain 11 digits');
		}		
		$this->sex = (int)(substr($this->peselString, 9, 1) % 2 === 0);
		$this->dayOfBirth = (int)substr($this->peselString, 4, 2);
		$this->monthOfBirth = (int)substr($this->peselString, 2, 2);
		$century = 1900;
		if($this->monthOfBirth > 12) {
			switch($this->monthOfBirth[0]) {
				case 2:
				case 3:
					$century = 2000;
					$this->monthOfBirth -= 20;
					break;
				case 4:
				case 5:
					$century = 2100;
					$this->monthOfBirth -= 40;
					break;
				case 6:
				case 7:
					$century = 2200;
					$this->monthOfBirth -= 60;
					break;
				case 8:
				case 9:
					$century = 1800;
					$this->monthOfBirth -= 80;
					break;
				default:
					break;
			}
		}
		
		if($this->monthOfBirth == 0 || $this->monthOfBirth > 12) {
			throw new Exception('Invalid month of birth');
		}		

		$this->yearOfBirth = ((int)substr($this->peselString, 0, 2)) + $century;
		
		if(!checkdate($this->monthOfBirth, $this->dayOfBirth, $this->yearOfBirth)) {
			throw new Exception('Invalid date of birth');
		}
				
		$p = $this->peselString;
		$crc = 
			1 * $p[0] + 
			3 * $p[1] +
			7 * $p[2] +
			9 * $p[3] +
			1 * $p[4] +
			3 * $p[5] +
			7 * $p[6] +
			9 * $p[7] +
			1 * $p[8] +
			3 * $p[9];
		$crc = 10 - ((int) substr($crc, -1));			
		if($crc != $p[10]) {
			throw new Exception('Invalid PESEL checksum');
		}
	}
	
	/**
	 * Returns calculated year of birth.
	 * 
	 * @return int Day of birth. 
	 */	
	public function getYearOfBirth() {
		return $this->yearOfBirth;
	}
	
	/**
	 * Returns calculated month of birth.
	 * 
	 * @return int Month of birth. 
	 */	
	public function getMonthOfBirth() {
		return $this->monthOfBirth;
	}
	
	/**
	 * Returns calculated day of birth.
	 * 
	 * @return int Day of birth. 
	 */
	public function getDayOfBirth() {
		return $this->dayOfBirth;
	}
	
	/**
	 * Checks if given person is male.
	 * 
	 * @return bool True if male, false otherwise. 
	 */	
	public function isMale() {
		return $this->sex === 0;
	}
	
	/**
	 * Checks if given person is female.
	 * 
	 * @return bool True if female, false otherwise. 
	 */
	public function isFemale() {
		return $this->sex === 1;		
	}
	
	/**
	 * Returns PESEL number.
	 *
	 * @return string PESEL number. 
	 */
	public function getPesel() {
		return $this->peselString;
	}
}
Return current item: PESEL validator