<commit_before><?php
namespace WFV\Factory;
defined( 'ABSPATH' ) or die();

use \Valitron\Validator;

use WFV\Errors;
use WFV\Form;
use WFV\Input;
use WFV\Messages;
use WFV\Rules;

/**
 * Static methods for assembling instances
 *
 * @since 0.8.2
 */
class ValidationFactory {

  /**
   *
   *
   * @since 0.9.1
   * @access protected
   * @var
   */
  protected static $config;


  /**
   * Build an instance of WFV\Form as described by $form
   *
   * @since 0.8.2
   *
   * @param array $form
   */
  public static function create_form( &$form ) {
    self::$config = $form;

    $action = $form['action'];
    $errors = new Errors();
    $input = self::input();
    $messages = self::messages();
    $rules = self::rules();

    $form = new Form( $action, $input, $rules, $messages, $errors );
  }

  /**
   *
   *
   * @since 0.9.1
   *
   * @param
   */
  public static function create_validator( $form ) {
    return self::validator( $form->input->get_array() );
  }

  /**
   * Push rules onto an instance of Valitron
   *
   * @since 0.7.0
   *
   * @param
   * @param
   */
  public static function load_rules( $form, &$validator ) {

    // loop the field
    foreach( $form->rules as $field => $rules ) {

      // loop this field rules - a field can have many rules
      foreach( $rules as $rule ) {
        if( $form->rules->is_custom( $rule ) ) {
          self::add_rule( $rule, $validator );
        }

        // check if this field/rule has a custom error message
        if( $form->messages->exist( $field, $rule ) ) {
          $message = $form->messages->$field;
          $validator->rule( $rule, $field )->message( $message[ $rule ] );
        } else { // use defaults
          $validator->rule( $rule, $field );
        }
      }
    }
  }

  /**
   * Add custom rule to Valitron\Validator
   * Trigger callback function for this custom rule
   *
   * @since 0.7.1
   * @access private
   *
   * @param string $rule
   * @param object $valitron Instance of Valitron\Validator
   */
  private function add_rule( $rule, $validator ) {
    $validator::addRule( $rule, function($field, $value, array $params, array $fields ) use ( $rule ) {
      $rule = explode( ':', $rule );
      $callback = 'wfv__'. $rule[1];
      // TODO: throw exception if no callback, or warning?
      return ( function_exists( $callback ) ) ? $callback( $value ) : false;
    });
  }

  /**
   *
   *
   * @since 0.9.1
   *
   * @param
   */
  private function guard() {

  }

  /**
   *
   *
   * @since 0.9.1
   *
   * @param
   */
  private function input() {
    return new Input( self::$config['action'] );<commit_msg>ValidationFactory - new methods to create Guard instance<commit_after><?php
namespace WFV\Factory;
defined( 'ABSPATH' ) or die();

use \Valitron\Validator;

use WFV\Errors;
use WFV\Form;
use WFV\Guard;
use WFV\Input;
use WFV\Messages;
use WFV\Rules;

/**
 * Static methods for assembling instances
 *
 * @since 0.8.2
 */
class ValidationFactory {

  /**
   *
   *
   * @since 0.9.1
   * @access protected
   * @var
   */
  protected static $config;


  /**
   * Build an instance of WFV\Form as described by $form
   *
   * @since 0.8.2
   *
   * @param array $form
   */
  public static function create_form( &$form ) {
    self::$config = $form;

    $action = $form['action'];
    $errors = new Errors();
    $input = self::input();
    $messages = self::messages();
    $rules = self::rules();

    $form = new Form( $action, $input, $rules, $messages, $errors );
  }

  /**
   *
   *
   * @since 0.9.1
   *
   * @param
   */
  public static function create_guard( $input_action, $input_token ) {
    return self::guard( $input_action, $input_token );
  }

  /**
   *
   *
   * @since 0.9.1
   *
   * @param
   */
  public static function create_validator( $form ) {
    return self::validator( $form->input->get_array() );
  }

  /**
   * Push rules onto an instance of Valitron
   *
   * @since 0.7.0
   *
   * @param
   * @param
   */
  public static function load_rules( $form, &$validator ) {

    // loop the field
    foreach( $form->rules as $field => $rules ) {

      // loop this field rules - a field can have many rules
      foreach( $rules as $rule ) {
        if( $form->rules->is_custom( $rule ) ) {
          self::add_rule( $rule, $validator );
        }

        // check if this field/rule has a custom error message
        if( $form->messages->exist( $field, $rule ) ) {
          $message = $form->messages->$field;
          $validator->rule( $rule, $field )->message( $message[ $rule ] );
        } else { // use defaults
          $validator->rule( $rule, $field );
        }
      }
    }
  }

  /**
   * Add custom rule to Valitron\Validator
   * Trigger callback function for this custom rule
   *
   * @since 0.7.1
   * @access private
   *
   * @param string $rule
   * @param object $valitron Instance of Valitron\Validator
   */
  private function add_rule( $rule, $validator ) {
    $validator::addRule( $rule, function($field, $value, array $params, array $fields ) use ( $rule ) {
      $rule = explode( ':', $rule );
      $callback = 'wfv__'. $rule[1];
      // TODO: throw exception if no callback, or warning?
      return ( function_exists( $callback ) ) ? $callback( $value ) : false;
    });
  }

  /**
   *
   *
   * @since 0.9.1
   *
   * @param
   */
  private function guard( $input_action, $input_token ) {
    return new Guard( $input_action, $input_token );
  }

  /**
   *
   *
   * @since 0.9.1
   *
   * @param
   */
  private function input() {
    return new Input( self::$config['action'] );
{'fraction_non_alphanumeric': 0.15761161915008068, 'fraction_numerical': 0.009503317195624889, 'mean_word_length': 2.26772114821324, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '226714', 'n_tokens_mistral': 1932, 'n_tokens_neox': 1877, 'n_words': 498}