Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Import of Validate_IR to SVN

git-svn-id: http://svn.php.net/repository/pear/packages/Validate/trunk@308054 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
commit 187cb122b78eddf05ea485822d35fa9a22401be7 1 parent 3140dec
@CloCkWeRX CloCkWeRX authored
View
127 Validate/IR.php
@@ -0,0 +1,127 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * Specific validation methods for data used in the Iran.
+ *
+ * PHP version 5
+ *
+ * This source file is subject to the New BSD license,
+ * That is bundled with this package in the file LICENSE,
+ * and is available through the world-wide-web at http://www.opensource.org/licenses/bsd-license.php
+ * If you did not receive a copy of the new BSDlicense and are unable to obtain it through the world-wide-web,
+ * please send a note to pajoye@php.net so we can mail you a copy immediately.
+ *
+ * @category Validate
+ * @package Validate_IR
+ * @author Arash Hemmat <arash.hemmat@gmail.com>
+ * @copyright 2011 Arash Hemmat
+ * @license http://www.opensource.org/licenses/bsd-license.php new BSD
+ * @link http://pear.php.net/package/Validate_IR
+ */
+class Validate_IR
+{
+ /**
+ * Check for Persian/Farsi characters and number an zero width non-joiner space.
+ * Also accpets latin numbers preventing potential problem until PHP becomes fully unicode compatible.
+ *
+ * @param string $check The value to check.
+ * @return boolean
+ * @access public
+ */
+ function alphaNumeric($check) {
+ $pattern = '/[^\x{0600}-\x{06FF}\x{FB50}-\x{FDFD}\x{FE70}-\x{FEFF}\x{0750}-\x{077F}0-9\s\x{200C}]+/u';
+ return !preg_match($pattern, $check);
+ }
+
+ /**
+ * Checks for Persian/Farsi digits only and won't accept Arabic and Latin digits.
+ *
+ * @param string $check The value to check.
+ * @return boolean
+ * @access public
+ */
+ function numeric($check) {
+ $pattern = '/[^\x{06F0}-\x{06F9}\x]+/u';
+ return !preg_match($pattern, $check);
+ }
+
+ /**
+ * Validation of Iran credit card numbers.
+ *
+ * @param string $check The value to check.
+ * @return boolean
+ * @access public
+ */
+ function creditCard($check) {
+ $pattern = '/[0-9]{4}-?[0-9]{4}-?[0-9]{4}-?[0-9]{4}$/';
+ return preg_match($pattern, $check);
+ }
+
+ /**
+ * Checks phone numbers for Iran
+ *
+ * @param string $check The value to check.
+ * @return boolean
+ * @access public
+ */
+ function phoneNumber($check) {
+ $pattern = '/^[- .\(\)]?((98)|(\+98)|(0098)|0){1}[- .\(\)]{0,3}[1-9]{1}[0-9]{1,}[- .\(\)]*[0-9]{3,8}[- .\(\)]?$/';
+ return preg_match($pattern, $check);
+ }
+
+ /**
+ * Checks mobile numbers for Iran
+ *
+ * @param string $check The value to check.
+ * @return boolean
+ * @access public 0 9 124061351
+ */
+ function mobileNumber($check) {
+ $pattern = '/^[- .\(\)]?((98)|(\+98)|(0098)|0){1}[- .\(\)]{0,3}((91)|(93)){1}[0-9]{8}$/';
+ return preg_match($pattern, $check);
+ }
+
+ /**
+ * Checks postal codes for Iran
+ *
+ * @param string $check The value to check.
+ * @return boolean
+ * @access public
+ */
+ function postalCode($check) {
+ $pattern = '/^\d{10}$/';
+ return preg_match($pattern, $check);
+ }
+
+ /**
+ * Checks social security numbers for Iran called "kode melli"
+ *
+ * @param string $check The value to check.
+ * @return boolean
+ * @access public
+ */
+ function ssn($check) {
+ $pattern = '/^\d{10}$/';
+ if (!preg_match($pattern, $check)) {
+ return false;
+ }
+ $sum = 0;
+ $equivalent = 0;
+ for ($i = 0; $i < 9; $i++) {
+ $sum += $check{$i} * (10 - $i);
+ if ($check{1} == $check{$i}) {
+ $equivalent++;
+ }
+ }
+ if ($equivalent == 9) {
+ return false;
+ }
+ $remaining = $sum % 11;
+ if ($remaining <= 1) {
+ return (bool)($remaining == $check{9});
+ } else {
+ return (bool)((11 - $remaining) == $check{9});
+ }
+ }
+}
View
56 package_IR.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<package packagerversion="1.7.1" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
+ <name>Validate_IR</name>
+ <channel>pear.php.net</channel>
+ <summary>Data validation class for Iran.</summary>
+ <description>Package containes locale validation for IR such as:
+ * Phone Number
+ * Postal Code
+ * SSN
+</description>
+ <lead>
+ <name>Arash Hemmat</name>
+ <user>hemmat</user>
+ <email>arash.hemmat@gmail.com</email>
+ <active>yes</active>
+ </lead>
+ <date>2011-02-06</date>
+ <time>04:30:00</time>
+ <version>
+ <release>0.1.0</release>
+ <api>0.1.0</api>
+ </version>
+ <stability>
+ <release>alpha</release>
+ <api>alpha</api>
+ </stability>
+ <license uri="http://www.opensource.org/licenses/bsd-license.php">BSD License</license>
+ <notes>Initial release</notes>
+ <contents>
+ <dir name="/">
+ <file name="Validate/IR.php" role="php" />
+ <file name="tests/validate_IR_creditCard.phpt" role="test" />
+ <file name="tests/validate_IR_alphaNumeric.phpt" role="test" />
+ <file name="tests/validate_IR_postal_code.phpt" role="test" />
+ <file name="tests/validate_IR_mobileNumber.phpt" role="test" />
+ <file name="tests/validate_IR_numeric.phpt" role="test" />
+ <file name="tests/validate_IR_phoneNumber.phpt" role="test" />
+ <file name="tests/validate_IR_postalCode.phpt" role="test" />
+ <file name="tests/validate_IR_ssn.phpt" role="test" />
+ <file name="data/AU_postcodes.txt" role="data" />
+ <file name="LICENSE" role="doc" />
+ </dir>
+ </contents>
+ <dependencies>
+ <required>
+ <php>
+ <min>5.0.0</min>
+ <max>6.0.0</max>
+ </php>
+ <pearinstaller>
+ <min>1.9.0</min>
+ </pearinstaller>
+ </required>
+ </dependencies>
+ <phprelease />
+</package>
View
44 tests/validate_IR_alphaNumeric.phpt
@@ -0,0 +1,44 @@
+--TEST--
+alphaNumeric.phpt: Unit tests for
+--FILE--
+<?php
+// Validate test script
+$noYes = array('NO', 'YES');
+require 'Validate/IR.php';
+
+echo "Test alphaNumeric\n";
+
+$strings = array(
+ 'آزمایش ۱۲۳۴۵۶۷۸۹۰', //OK
+ 'آزمایش 1234567890', //OK
+ 'هِمّت بُلَند دار کِه مَردانِ روزگار اَز همّتِ بُلَند به جایی رسیده‌اَند', //OK
+ '', //OK
+
+ 'teststring', //NOK
+ 'test1234567890', //NOK
+ 'test آزمایش', //NOK
+ );
+
+list($version) = explode(".", phpversion(), 2);
+foreach ($strings as $string) {
+ echo "{$string}: ";
+ if ((int)$version > 4) {
+ try {
+ echo $noYes[Validate_IR::alphaNumeric($string)]."\n";
+ } catch (Exception $e) {
+ echo $e->getMessage()."\n";
+ }
+ } else {
+ echo $noYes[Validate_IR::alphaNumeric($string)]."\n";
+ }
+}
+?>
+--EXPECT--
+Test alphaNumeric
+آزمایش ۱۲۳۴۵۶۷۸۹۰: YES
+آزمایش 1234567890: YES
+هِمّت بُلَند دار کِه مَردانِ روزگار اَز همّتِ بُلَند به جایی رسیده‌اَند: YES
+﷼: YES
+teststring: NO
+test1234567890: NO
+test آزمایش: NO
View
40 tests/validate_IR_creditCard.phpt
@@ -0,0 +1,40 @@
+--TEST--
+creditCard.phpt: Unit tests for
+--FILE--
+<?php
+// Validate test script
+$noYes = array('NO', 'YES');
+require 'Validate/IR.php';
+
+echo "Test creditCard\n";
+
+$strings = array(
+ '1111222233334444', //OK
+ '1111-2222-3333-4444', //OK
+
+ 'teststring', //NOK
+ '1111', //NOK
+ '111-122-223-333-444-4', //NOK
+ );
+
+list($version) = explode(".", phpversion(), 2);
+foreach ($strings as $string) {
+ echo "{$string}: ";
+ if ((int)$version > 4) {
+ try {
+ echo $noYes[Validate_IR::creditCard($string)]."\n";
+ } catch (Exception $e) {
+ echo $e->getMessage()."\n";
+ }
+ } else {
+ echo $noYes[Validate_IR::creditCard($string)]."\n";
+ }
+}
+?>
+--EXPECT--
+Test creditCard
+1111222233334444: YES
+1111-2222-3333-4444: YES
+teststring: NO
+1111: NO
+111-122-223-333-444-4: NO
View
56 tests/validate_IR_mobileNumber.phpt
@@ -0,0 +1,56 @@
+--TEST--
+mobileNumber.phpt: Unit tests for
+--FILE--
+<?php
+// Validate test script
+$noYes = array('NO', 'YES');
+require 'Validate/IR.php';
+
+echo "Test mobileNumber\n";
+
+$strings = array(
+ '989123334444', //OK
+ '00989353334444', //OK
+ '+989363334444', //OK
+ '+98 9373334444', //OK
+ '(+98) 9383334444', //OK
+ '+98-9323334444', //OK
+
+ 'teststring', //NOK
+ '999123334444', //NOK
+ '+999353334444', //NOK
+ '00999363334444', //NOK
+ '000989373334444', //NOK
+ '+00989383334444', //NOK
+ '+0989323334444', //NOK
+ );
+
+list($version) = explode(".", phpversion(), 2);
+foreach ($strings as $string) {
+ echo "{$string}: ";
+ if ((int)$version > 4) {
+ try {
+ echo $noYes[Validate_IR::mobileNumber($string)]."\n";
+ } catch (Exception $e) {
+ echo $e->getMessage()."\n";
+ }
+ } else {
+ echo $noYes[Validate_IR::mobileNumber($string)]."\n";
+ }
+}
+?>
+--EXPECT--
+Test mobileNumber
+989123334444: YES
+00989353334444: YES
++989363334444: YES
++98 9373334444: YES
+(+98) 9383334444: YES
++98-9323334444: YES
+teststring: NO
+999123334444: NO
++999353334444: NO
+00999363334444: NO
+000989373334444: NO
++00989383334444: NO
++0989323334444: NO
View
38 tests/validate_IR_numeric.phpt
@@ -0,0 +1,38 @@
+--TEST--
+numeric.phpt: Unit tests for
+--FILE--
+<?php
+// Validate test script
+$noYes = array('NO', 'YES');
+require 'Validate/IR.php';
+
+echo "Test numeric\n";
+
+$strings = array(
+ '۱۲۳۴۵۶۷۸۹۰', //OK
+
+ 'teststring', //NOK
+ '1234567890', //NOK
+ '١٢٣٤٥٦٧٨٩٠', //NOK
+ );
+
+list($version) = explode(".", phpversion(), 2);
+foreach ($strings as $string) {
+ echo "{$string}: ";
+ if ((int)$version > 4) {
+ try {
+ echo $noYes[Validate_IR::numeric($string)]."\n";
+ } catch (Exception $e) {
+ echo $e->getMessage()."\n";
+ }
+ } else {
+ echo $noYes[Validate_IR::numeric($string)]."\n";
+ }
+}
+?>
+--EXPECT--
+Test numeric
+۱۲۳۴۵۶۷۸۹۰: YES
+teststring: NO
+1234567890: NO
+١٢٣٤٥٦٧٨٩٠: NO
View
76 tests/validate_IR_phoneNumber.phpt
@@ -0,0 +1,76 @@
+--TEST--
+mobileNumber.phpt: Unit tests for
+--FILE--
+<?php
+// Validate test script
+$noYes = array('NO', 'YES');
+require 'Validate/IR.php';
+
+echo "Test phoneNumber\n";
+
+$strings = array(
+ '982133334444', //OK
+ '00982133334444', //OK
+ '+982133334444', //OK
+ '+98 2133334444', //OK
+ '+98 21 33334444', //OK
+ '+98-21-33334444', //OK
+ '(+98) (21) (33334444)', //OK
+ '02133334444', //OK
+ '021 33334444', //OK
+ '021 33334444', //OK
+ '(021) (118)', //OK
+ '0411 3334444', //OK
+ '(0411) (3334444)', //OK
+
+ 'teststring', //NOK
+ '992133334444', //NOK
+ '+992133334444', //NOK
+ '00992133334444', //NOK
+ '000982133334444', //NOK
+ '+00982133334444', //NOK
+ '+0982133334444', //NOK
+ '+98/21/33334444', //NOK
+ '+02133334444', //NOK
+ '021 22' //NOK
+ );
+
+list($version) = explode(".", phpversion(), 2);
+foreach ($strings as $string) {
+ echo "{$string}: ";
+ if ((int)$version > 4) {
+ try {
+ echo $noYes[Validate_IR::phoneNumber($string)]."\n";
+ } catch (Exception $e) {
+ echo $e->getMessage()."\n";
+ }
+ } else {
+ echo $noYes[Validate_IR::phoneNumber($string)]."\n";
+ }
+}
+?>
+--EXPECT--
+Test phoneNumber
+982133334444: YES
+00982133334444: YES
++982133334444: YES
++98 2133334444: YES
++98 21 33334444: YES
++98-21-33334444: YES
+(+98) (21) (33334444): YES
+02133334444: YES
+021 33334444: YES
+021 33334444: YES
+(021) (118): YES
+0411 3334444: YES
+(0411) (3334444): YES
+teststring: NO
+992133334444: NO
++992133334444: NO
+00992133334444: NO
+000982133334444: NO
++00982133334444: NO
++0982133334444: NO
++98/21/33334444: NO
++02133334444: NO
+021 22: NO
View
36 tests/validate_IR_postalCode.phpt
@@ -0,0 +1,36 @@
+--TEST--
+postalCode.phpt: Unit tests for
+--FILE--
+<?php
+// Validate test script
+$noYes = array('NO', 'YES');
+require 'Validate/IR.php';
+
+echo "Test postalCode\n";
+
+$strings = array(
+ '1234567890', //OK
+
+ 'teststring', //NOK
+ '123456789' //NOK
+ );
+
+list($version) = explode(".", phpversion(), 2);
+foreach ($strings as $string) {
+ echo "{$string}: ";
+ if ((int)$version > 4) {
+ try {
+ echo $noYes[Validate_IR::postalCode($string)]."\n";
+ } catch (Exception $e) {
+ echo $e->getMessage()."\n";
+ }
+ } else {
+ echo $noYes[Validate_IR::postalCode($string)]."\n";
+ }
+}
+?>
+--EXPECT--
+Test postalCode
+1234567890: YES
+teststring: NO
+123456789: NO
View
46 tests/validate_IR_ssn.phpt
@@ -0,0 +1,46 @@
+--TEST--
+ssn.phpt: Unit tests for
+--FILE--
+<?php
+// Validate test script
+$noYes = array('NO', 'YES');
+require 'Validate/IR.php';
+
+echo "Test ssn\n";
+
+$strings = array(
+ '9876543210', //OK
+ '1234567891', //OK
+ '0324354657', //OK
+
+ 'teststring', //NOK
+ '1234567890', //NOK
+ '3333333333', //NOK
+ '0324354654', //NOK
+ '12345' //NOK
+ );
+
+list($version) = explode(".", phpversion(), 2);
+foreach ($strings as $string) {
+ echo "{$string}: ";
+ if ((int)$version > 4) {
+ try {
+ echo $noYes[Validate_IR::ssn($string)]."\n";
+ } catch (Exception $e) {
+ echo $e->getMessage()."\n";
+ }
+ } else {
+ echo $noYes[Validate_IR::ssn($string)]."\n";
+ }
+}
+?>
+--EXPECT--
+Test ssn
+9876543210: YES
+1234567891: YES
+0324354657: YES
+teststring: NO
+1234567890: NO
+3333333333: NO
+0324354654: NO
+12345: NO
Please sign in to comment.
Something went wrong with that request. Please try again.