ApPHP DataValidator Advanced - Example of Code

Show Plain Text »
  1. <?php
  2.  
  3. /**
  4.  * User friendly errors while validation
  5.  */
  6. class ValidatorError
  7. {
  8.     /**
  9.      * which error
  10.      * @var
  11.      */
  12.     private $errType;
  13.     /**
  14.      * current error data
  15.      * @var array
  16.      */
  17.     private $data;
  18.  
  19.     /**
  20.      * validator type  error occurred to
  21.      * @var
  22.      */
  23.     private $validatorType;
  24.  
  25.     /**
  26.      * error types
  27.      * @var array
  28.      */
  29.     static $errorTypeArray = array();
  30.  
  31.     public function __construct($errType, ValidatorAnyType $validatorType, array $additionalData = array()){
  32.         $this->errType = $errType;
  33.         $this->data = $additionalData;
  34.         $this->validatorType = $validatorType;
  35.     }
  36.  
  37.     /**
  38.      * Form standard errors in a user friendly view
  39.      * @return string
  40.      */
  41.     public function ToString(){
  42.         $this->_ProcessFieldName();
  43.         return ValidatorLocalization::GetField($this->errType, $this->data);
  44.     }
  45.  
  46.     /**
  47.      * getter for error type
  48.      * @return mixed
  49.      */
  50.     public function GetErrType(){
  51.         return $this->errType;
  52.     }
  53.  
  54.     /**
  55.      * getter for current error data
  56.      * @return array
  57.      */
  58.     public function GetErrData(){
  59.         return $this->data;
  60.     }
  61.  
  62.     /**
  63.      * getter for name of field current error belongs to
  64.      */
  65.     public function GetFieldName(){
  66.         return $this->validatorType->GetName();
  67.     }
  68.  
  69.     /**
  70.      * if field name is set get it either from user-friendly name or from field name
  71.      */
  72.     private function _ProcessFieldName(){
  73.         if(!isset($this->data['fieldName'])){
  74.             $ufName = $this->validatorType->GetUserFriendlyName() ;
  75.             $this->data['fieldName'] = !empty($ufName) ? $ufName : $this->validatorType->GetName();
  76.         }
  77.     }
  78. }
  79.    
  80. ?>