[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Translation Validation API
   4   *
   5   * @package GlotPress
   6   * @since 1.0.0
   7   */
   8  
   9  /**
  10   * Core class to handle validation of translations.
  11   *
  12   * Uses magic methods in the format of [field]_[rule].
  13   *
  14   * The below is a list of all magic methods called to ensure Scrutinizer recognizes them.
  15   * Note that once a method has been defined from one file it will not be redefine in subsequent file sections.
  16   *
  17   * From gp_includes/things/administrative-permissions.php:
  18   *
  19   *     @method bool user_id_should_not_be( string $name, array $args = null )
  20   *     @method bool action_should_not_be( string $name, array $args = null )
  21   *     @method bool object_type_should_be( string $name, array $args = null )
  22   *     @method bool object_id_should_be( string $name, array $args = null )
  23   *
  24   * From gp_includes/things/glossary-entry.php:
  25   *
  26   *     @method bool term_should_not_be( string $name, array $args = null )
  27   *     @method bool part_of_speech_should_not_be( string $name, array $args = null )
  28   *     @method bool glossary_id_should_be( string $name, array $args = null )
  29   *     @method bool last_edited_by_should_be( string $name, array $args = null )
  30   *
  31   * From gp_includes/things/original.php:
  32   *
  33   *     @method bool singular_should_not_be( string $name, array $args = null )
  34   *     @method bool status_should_not_be( string $name, array $args = null )
  35   *     @method bool project_id_should_be( string $name, array $args = null )
  36   *     @method bool priority_should_be( string $name, array $args = null )
  37   *
  38   * From gp_includes/things/translation.php:
  39   *
  40   *     @method bool translation_0_should_not_be( string $name, array $args = null )
  41   *     @method bool original_id_should_be( string $name, array $args = null )
  42   *     @method bool translation_set_id_should_be( string $name, array $args = null )
  43   *     @method bool user_id_should_be( string $name, array $args = null )
  44   *     @method bool user_id_last_modified_should_not_be( string $name, array $args = null )
  45   *
  46   * From gp_includes/things/glossary.php:
  47   *
  48   *     @method bool translation_set_id_should_not_be( string $name, array $args = null )
  49   *
  50   * From gp_includes/things/project.php:
  51   *
  52   *     @method bool name_should_not_be( string $name, array $args = null )
  53   *     @method bool slug_should_not_be( string $name, array $args = null )
  54   *
  55   * From gp_includes/things/translation-set.php:
  56   *
  57   *     @method bool locale_should_not_be( string $name, array $args = null )
  58   *     @method bool project_id_should_not_be( string $name, array $args = null )
  59   *
  60   * From gp_includes/things/validator-permission.php:
  61   *
  62   *     @method bool locale_slug_should_not_be( string $name, array $args = null )
  63   *     @method bool user_id_should_not_be( string $name, array $args = null )
  64   *     @method bool action_should_not_be( string $name, array $args = null )
  65   *     @method bool set_slug_should_not_be( string $name, array $args = null )
  66   */
  67  class GP_Validation_Rules {
  68  
  69      var $rules = array();
  70  
  71      public $errors = array();
  72      public $field_names;
  73  
  74      static $positive_suffices = array(
  75          'should_be', 'should', 'can', 'can_be',
  76      );
  77      static $negative_suffices = array(
  78          'should_not_be', 'should_not', 'cant', 'cant_be',
  79      );
  80  
  81  	public function __construct( $field_names ) {
  82          $this->field_names = $field_names;
  83      }
  84  
  85  	public function __call( $name, $args ) {
  86          foreach( array( 'positive', 'negative' ) as $kind ) {
  87              $suffices = "{$kind}_suffices";
  88              foreach( self::$$suffices as $suffix ) {
  89                  foreach( $this->field_names as $field_name ) {
  90                      if ( $name == "{$field_name}_{$suffix}" ) {
  91                          $this->rules[$field_name][] = array( 'field' => $field_name, 'rule' => $args[0], 'kind' => $kind, 'args' => array_slice( $args, 1 ) );
  92                          return true;
  93                      }
  94                  }
  95              }
  96          }
  97          trigger_error(sprintf('Call to undefined function: %s::%s().', get_class($this), $name), E_USER_ERROR);
  98      }
  99  
 100  	public function run( $thing ) {
 101          $this->errors = array();
 102          $verdict = true;
 103          foreach( $this->field_names as $field_name ) {
 104              // do not try to validate missing fields
 105              if ( !gp_object_has_var( $thing, $field_name ) ) continue;
 106              $value = $thing->$field_name;
 107              $field_verdict = $this->run_on_single_field( $field_name, $value );
 108              $verdict = $verdict && $field_verdict;
 109          }
 110          return $verdict;
 111      }
 112  
 113  	public function run_on_single_field( $field, $value ) {
 114          if ( !isset( $this->rules[$field] ) || !is_array( $this->rules[$field] ) ) {
 115              // no rules means always valid
 116              return true;
 117          }
 118          $verdict = true;
 119  
 120          foreach( $this->rules[$field] as $rule ) {
 121              $callback = GP_Validators::get( $rule['rule'] );
 122              if ( is_null( $callback ) ) {
 123                  trigger_error( sprintf( __( 'Non-existent validator: %s', 'glotpress' ), $rule['rule'] ) );
 124                  continue;
 125              }
 126              $args = $rule['args'];
 127              array_unshift( $args, $value );
 128              if ( 'positive' == $rule['kind'] ) {
 129                  if ( !call_user_func_array( $callback['positive'], $args ) ) {
 130                      $this->errors[] = $this->construct_error_message( $rule );
 131                      $verdict = false;
 132                  }
 133              } else {
 134                  if ( is_null( $callback['negative'] ) ) {
 135                      if ( call_user_func_array( $callback['positive'], $args ) ) {
 136                          $this->errors[] = $this->construct_error_message( $rule );
 137                          $verdict = false;
 138                      }
 139                  } else if ( !call_user_func_array( $callback['negative'], $args ) ) {
 140                      $this->errors[] = $this->construct_error_message( $rule );
 141                      $verdict = false;
 142                  }
 143              }
 144          }
 145          return $verdict;
 146      }
 147  
 148  	public function construct_error_message( $rule ) {
 149          $type_field = 'field';
 150          $name_field = $rule['field'];
 151          $name_rule  = str_replace( '_', ' ', $rule['rule'] );
 152  
 153          if ( 1 === preg_match( '/translation_[0-9]/', $name_field ) ) {
 154              $type_field = 'textarea';
 155              $name_field = 'Translation ' . ( intval( substr( $name_field, 12 ) ) + 1 );
 156          }
 157  
 158          if ( 'positive' == $rule['kind'] ) {
 159              /* translators: 1: type of a validation field, 2: name of a validation field, 3: validation rule */
 160              return sprintf( __( 'The %1$s %2$s is invalid and should be %3$s!', 'glotpress' ), $type_field, '<strong>' . $name_field . '</strong>', $name_rule );
 161          } else { //if ( 'negative' == $rule['kind'] )
 162              /* translators: 1: type of a validation field, 2: name of a validation field, 3: validation rule */
 163              return sprintf( __( 'The %1$s %2$s is invalid and should not be %3$s!', 'glotpress' ), $type_field, '<strong>' . $name_field . '</strong>', $name_rule );
 164          }
 165      }
 166  }
 167  
 168  class GP_Validators {
 169      static $callbacks = array();
 170  
 171  	static public function register( $key, $callback, $negative_callback = null ) {
 172          // TODO: add data for easier generation of error messages
 173          self::$callbacks[$key] = array( 'positive' => $callback, 'negative' => $negative_callback );
 174      }
 175  
 176  	static public function unregister( $key ) {
 177          unset( self::$callbacks[$key] );
 178      }
 179  
 180  	static public function get( $key ) {
 181          return gp_array_get( self::$callbacks, $key, null );
 182      }
 183  }
 184  
 185  GP_Validators::register( 'empty', 'gp_is_empty' );
 186  GP_Validators::register( 'empty_string', 'gp_is_empty_string' );
 187  GP_Validators::register( 'positive_int', 'gp_is_positive_int' );
 188  GP_Validators::register( 'int', 'gp_is_int' );
 189  GP_Validators::register( 'null', 'gp_is_null' );
 190  GP_Validators::register( 'between', 'gp_is_between' );
 191  GP_Validators::register( 'between_exclusive', 'gp_is_between_exclusive' );
 192  


Generated: Sat Aug 17 01:01:56 2019 Cross-referenced by PHPXref 0.7.1