[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/ -> validation.php (source)

   1  <?php
   2  
   3  class GP_Validation_Rules {
   4      
   5      var $rules = array();
   6      var $errors = array();
   7      
   8      static $positive_suffices = array(
   9          'should_be', 'should', 'can', 'can_be',
  10      );
  11      static $negative_suffices = array(
  12          'should_not_be', 'should_not', 'cant', 'cant_be',
  13      );
  14      
  15  	function __construct( $field_names ) {
  16          $this->field_names = $field_names;
  17      }
  18      
  19  	function __call( $name, $args ) {
  20          foreach( array( 'positive', 'negative' ) as $kind ) {
  21              $suffices = "{$kind}_suffices";
  22              foreach( self::$$suffices as $suffix ) {
  23                  foreach( $this->field_names as $field_name ) {
  24                      if ( $name == "{$field_name}_{$suffix}" ) {
  25                          $this->rules[$field_name][] = array( 'field' => $field_name, 'rule' => $args[0], 'kind' => $kind, 'args' => array_slice( $args, 1 ) );
  26                          return true;
  27                      }
  28                  }
  29              }
  30          }
  31          trigger_error(sprintf('Call to undefined function: %s::%s().', get_class($this), $name), E_USER_ERROR);
  32      }
  33      
  34  	function run( $thing ) {
  35          $this->errors = array();
  36          $verdict = true;
  37          foreach( $this->field_names as $field_name ) {
  38              // do not try to validate missing fields
  39              if ( !gp_object_has_var( $thing, $field_name ) ) continue;
  40              $value = $thing->$field_name;
  41              $field_verdict = $this->run_on_single_field( $field_name, $value );
  42              $verdict = $verdict && $field_verdict;
  43          }
  44          return $verdict;
  45      }
  46      
  47  	function run_on_single_field( $field, $value ) {
  48          if ( !isset( $this->rules[$field] ) || !is_array( $this->rules[$field] ) ) {
  49              // no rules means always valid
  50              return true;
  51          }
  52          $verdict = true;
  53          foreach( $this->rules[$field] as $rule ) {
  54              $callback = GP_Validators::get( $rule['rule'] );
  55              if ( is_null( $callback ) ) {
  56                  trigger_error( __('Non-existent validator: ' . $rule['rule'] ) );
  57                  continue;
  58              }
  59              $args = $rule['args'];
  60              array_unshift( $args, $value );
  61              if ( 'positive' == $rule['kind'] ) {
  62                  if ( !call_user_func_array( $callback['positive'], $args ) ) {
  63                      $this->errors[] = $this->construct_error_message( $rule, $value );
  64                      $verdict = false;
  65                  }
  66              } else {
  67                  if ( is_null( $callback['negative'] ) ) {
  68                      if ( call_user_func_array( $callback['positive'], $args ) ) {
  69                          $this->errors[] = $this->construct_error_message( $rule, $value );
  70                          $verdict = false;
  71                      }
  72                  } else if ( !call_user_func_array( $callback['negative'], $args ) ) {
  73                      $this->errors[] = $this->construct_error_message( $rule, $value );
  74                      $verdict = false;
  75                  }
  76              }
  77          }
  78          return $verdict;
  79      }
  80      
  81  	function construct_error_message( $rule, $value ) {
  82          // TODO: better error messages, should include info from callback
  83          return sprintf( __('The field <strong>%s</strong> has invalid value!'), $rule['field'], $value );
  84      }
  85  }
  86  
  87  class GP_Validators {
  88      static $callbacks = array();
  89      
  90  	static function register( $key, $callback, $negative_callback = null ) {
  91          // TODO: add data for easier generation of error messages        
  92          self::$callbacks[$key] = array( 'positive' => $callback, 'negative' => $negative_callback );
  93      }
  94      
  95  	static function unregister( $key ) {
  96          unset( self::$callbacks[$key] );
  97      }
  98      
  99  	static function get( $key ) {
 100          return gp_array_get( self::$callbacks, $key, null );
 101      }
 102  }
 103  
 104  GP_Validators::register( 'empty', lambda( '$value', 'empty($value)' ) );
 105  GP_Validators::register( 'positive_int', lambda( '$value', '((int)$value > 0)' ) );
 106  GP_Validators::register( 'int', lambda( '$value', '(bool)preg_match("/^-?\d+$/", $value)' ) );
 107  GP_Validators::register( 'null', lambda( '$value', 'is_null($value)' ) );
 108  GP_Validators::register( 'between', lambda( '$value, $start, $end', '$value >= $start && $value <= $end' ) );
 109  GP_Validators::register( 'between_exclusive', lambda( '$value, $start, $end', '$value > $start && $value < $end' ) );
 110  


Generated: Thu May 24 03:59:35 2012 Hosted by follow the white rabbit.