Created
June 9, 2015 18:52
-
-
Save prionkor/62445b566cdf0eb65a51 to your computer and use it in GitHub Desktop.
Company Validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Numstock\Controllers; | |
use Numstock\Models\Company_Query; | |
class Company_Validator extends General_Validator{ | |
public function __construct(Company_Query $query){ | |
$this->query = $query; | |
} | |
public function validate($data){ | |
$errors = array(); | |
$validated_data = array(); | |
$is_alias = false; | |
if( isset($data['id']) && !empty($data['id']) ){ | |
$validated_data['id'] = (int) $data['id']; | |
} | |
if( (isset($data['alias']) && !empty($data['alias']) || ( isset($validated_data['id']) && $this->has_alias($validated_data['id'])))){ | |
$validated_data['alias'] = (int) $data['alias']; | |
$is_alias = true; | |
} | |
if( !$is_alias && ( !isset($data['name']) || empty($data['name']) )){ | |
$errors[] = 'Company name is required.'; | |
}else{ | |
$validated_data['name'] = sanitize_text_field($data['name']); | |
} | |
if( !isset($data['name_abbr']) || empty($data['name_abbr']) ){ | |
$errors[] = 'Short version of the company name is required.'; | |
}else{ | |
$validated_data['name_abbr'] = strtolower(sanitize_text_field($data['name_abbr'])); | |
} | |
if( !$is_alias && ( !isset($data['year_start_financial']) || empty($data['year_start_financial']) || !in_array(strtolower($data['year_start_financial']), $this->months)) ){ | |
$errors[] = 'Select the month when financial year starts for the company.'; | |
}else{ | |
$validated_data['year_start_financial'] = strtolower(sanitize_text_field($data['year_start_financial'])); | |
} | |
if( isset($data['year_start_fiscal'])){ | |
$validated_data['year_start_fiscal'] = strtolower(sanitize_text_field($data['year_start_fiscal'])); | |
} | |
$this->data = $validated_data; | |
$this->errors = $errors; | |
return $errors; | |
} | |
public function get_data(){ | |
return $this->data; | |
} | |
public function has_alias($company_id){ | |
$company = $this->query->get_company($company_id); | |
return ($company->alias) ? $company->alias : false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment