<?php 

class Validation_app_model extends CI_Model {
	public $validation_array = array(
			'login'=> array('email'=>array('required'=>array('code'=>'ER02', 'message'=>'Email id is null or empty'),
											  'email'=>array('code'=>'ER03', 'message'=>'Invalid Email id')
										),
							'password'=>array('required'=>array('code'=>'ER04', 'message'=>'Password is null or empty'),
								        )
						),
			'check_email_availability'=> array('email'=>array('required'=>array('code'=>'ER02', 'message'=>'Email id is null or empty'),
											  'email'=>array('code'=>'ER03', 'message'=>'Invalid Email id')
										),
							'phone'=>array('required'=>array('code'=>'ER07', 'message'=>'Phone no is null or empty'),
										   'phone'=>array('code'=>'ER08', 'message'=>'Invalid Phone no')
						),
					),
			'registration'=> array('email'=>array('required'=>array('code'=>'ER02', 'message'=>'Email id is null or empty'),
											  'email'=>array('code'=>'ER03', 'message'=>'Invalid Email id')
										),
							'phone'=>array('required'=>array('code'=>'ER07', 'message'=>'Phone no is null or empty'),
										   'phone'=>array('code'=>'ER08', 'message'=>'Invalid Phone no'),
								        ),
							'password'=>array('required'=>array('code'=>'ER04', 'message'=>'Password is null or empty'),
								        )
						),
			'get_places_list'=>array('auth_token'=>array('required'=>array('code'=>'ER17', 'message'=>'User Id is null or empty'),
								        )
						),
			'place_details'=>array('auth_token'=>array('required'=>array('code'=>'ER17', 'message'=>'User Id is null or empty'),
								        )
						),
		);

	public function validation_check($method_name, $parms) {
 		$state = 0;
 		$rules = $this->validation_array[$method_name]; 		
 		$error_key = '';
 		foreach ($rules as $key => $value) {
 			foreach ($value as $keys => $values) {
 				switch ($keys) {
 						case 'required':
 							if(!isset($parms[$key]) || $parms[$key]=='' || $parms[$key]== null){
 								$state = 1;
 								$error_key = $values;
 							} 
 							break;
 						case 'email':
 							if (isset($parms[$key]) && !filter_var($parms[$key], FILTER_VALIDATE_EMAIL)) {
	  							$state = 1;
 								$error_key = $values; 
							} 
 							break;
 						case 'phone':
 							if(isset($parms[$key])){
 							$phone = preg_replace('/[^0-9]/', '', $parms[$key]);
	 							if (strlen($phone) !== 10) {
		  							$state = 1;
	 								$error_key = $values; 
								}
							} 
 							break;


 						
 						default:
 							# code...
 							break;
 					}
 					if($state==1){
 						break;
 					}
 			}
 			if($state==1){
 				break;
 			}
 		}
 		return array('state'=>$state,'response'=>$error_key);
 	} 	
}