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

/**
 *
 *
 * @since 0.8.0
 */
class Input implements ValidationInterface {

  use AccessorTrait;
  use MutatorTrait;

  /**
   * __construct
   *
   * @since 0.8.0
   * @since 0.9.0 Sanitize $_POST moved
   *
   */
  function __construct( $action ) {
    if( $this->is_submit( $action ) ) {
      $this->set_input();
    }
  }

  /**
   *
   *
   * @since 0.9.0
   *
   * @param string $field
   * @param string|array (optional) $callback
   * @return string|null
   */
  public function render( $field, $callback = 'htmlspecialchars' ) {
    if( is_string( $field ) ) {
      return ( $this->has( $field ) ) ? $this->call_func( $callback, $this->$field ) : null;
    }
    return null;
  }

  /**
   * Transform a string or array leafs using a callback.
   * When $value is an array, only the leafs (no keys) are transformed.
   * Infinite array traversal.
   *
   * @since 0.9.0
   *
   * @param string|array $value
   * @param string $callback
   * @return string|array
   */
  public function transform( $input, $callback ) {
    if( is_array( $input ) ) {
      return $this->transform_array_leafs( $input, $callback );
    }
    return $this->call_func( $callback, $input );
  }

  /**
   * Trigger a callback function
   *
   * @since 0.9.0
   * @access private
   *
   * @param string|array $callback
   * @param string (optional) $input The input string
   * @return
   */
  private function call_func( $callback, $input = null ) {
    if( is_array( $callback ) ) {
      $method = $callback[0];
      $args = $callback[1];

      if ( $input ) {
        array_unshift( $args, $input );
      }
      return call_user_func_array( $method, $args );
    }
    return $callback( $input );
  }

  /**
   * Transform the $array leafs, traversing infinite dimensions
   *
   * @since 0.9.0
   * @access private
   *
   * @param array $array
   * @param string|array $callback
   * @return array
   */
  private function transform_array_leafs( $array, $callback ) {
    array_walk_recursive( $array, function( &$item, $key ) use( $callback ) {
      $item = $this->call_func( $callback, $item );
    } );
    return $array;
  }

  /**
   * Check if there was a $_POST for this action
   *
   * @since 0.7.2
   *
   * @param string $action The forms action value
   * @return bool
   */
  private function is_submit( $action ) {
    return ( $_POST && $_POST['action'] === $action ) ? true : false;
  }

  /**
   * Set the properties
   *
   * @since 0.9.0
   * @access private
   */
  private function set_input() {
    $input = $this->transform_array_leafs( $_POST, 'stripslashes' );
    $this->set( $input );
  }
}<commit_msg>Input - porting it back in for builder<commit_after><?php
namespace WFV\Component;
defined( 'ABSPATH' ) or die();

/**
 *
 *
 * @since 0.8.0
 */
class Input {

  /**
   * __construct
   *
   * @since 0.8.0
   * @since 0.9.0 Sanitize $_POST moved
   *
   */
  function __construct( $action ) {
    if( $this->is_submit( $action ) ) {
      $this->copy_input();
    }
  }

  /**
   *
   *
   * @since 0.9.0
   *
   * @param string $field
   * @param string|array (optional) $callback
   * @return string|null
   */
  public function render( $field, $callback = 'htmlspecialchars' ) {
    if( is_string( $field ) ) {
      return ( $this->has( $field ) ) ? $this->call_func( $callback, $this->$field ) : null;
    }
    return null;
  }

  /**
   * Transform a string or array leafs using a callback.
   * When $value is an array, only the leafs (no keys) are transformed.
   * Infinite array traversal.
   *
   * @since 0.9.0
   *
   * @param string|array $value
   * @param string $callback
   * @return string|array
   */
  public function transform( $input, $callback ) {
    if( is_array( $input ) ) {
      return $this->transform_array_leafs( $input, $callback );
    }
    return $this->call_func( $callback, $input );
  }

  /**
   * Trigger a callback function
   *
   * @since 0.9.0
   * @access private
   *
   * @param string|array $callback
   * @param string (optional) $input The input string
   * @return
   */
  private function call_func( $callback, $input = null ) {
    if( is_array( $callback ) ) {
      $method = $callback[0];
      $args = $callback[1];

      if ( $input ) {
        array_unshift( $args, $input );
      }
      return call_user_func_array( $method, $args );
    }
    return $callback( $input );
  }

  /**
   * Transform the $array leafs, traversing infinite dimensions
   *
   * @since 0.9.0
   * @access private
   *
   * @param array $array
   * @param string|array $callback
   * @return array
   */
  private function transform_array_leafs( $array, $callback ) {
    array_walk_recursive( $array, function( &$item, $key ) use( $callback ) {
      $item = $this->call_func( $callback, $item );
    } );
    return $array;
  }

  /**
   * Check if there was a $_POST for this action
   *
   * @since 0.7.2
   *
   * @param string $action The forms action value
   * @return bool
   */
  private function is_submit( $action ) {
    return ( $_POST && $_POST['action'] === $action ) ? true : false;
  }

  /**
   * Assign input properties
   *
   * @since 0.9.0
   * @access private
   */
  private function copy_input() {
    // WIP reduce responsibility
    $input = $this->transform_array_leafs( $_POST, 'stripslashes' );

    foreach( $input as $field => $value ) {
      $this->$field = $value;
    }
  }
}
{'fraction_non_alphanumeric': 0.15019255455712452, 'fraction_numerical': 0.01063634696497341, 'mean_word_length': 2.4497153700189753, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, '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': '4367396', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1860, 'n_words': 484}