@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -defined( 'ABSPATH' ) or die(); |
|
| 2 | +defined('ABSPATH') or die(); |
|
| 3 | 3 | /* |
| 4 | 4 | Plugin Name: WFV - Form Validation |
| 5 | 5 | Plugin URI: https://macder.github.io/wfv/ |
@@ -11,12 +11,12 @@ discard block |
||
| 11 | 11 | License URI: https://github.com/macder/wp-form-validation/blob/master/LICENSE |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | -define( 'WFV_VALIDATE_VERSION', '0.11.0' ); |
|
| 15 | -define( 'WFV_VALIDATE__MINIMUM_WP_VERSION', '3.7' ); |
|
| 16 | -define( 'WFV_VALIDATE__PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
|
| 17 | -define( 'WFV_VALIDATE__ACTION_POST', 'validate_form' ); |
|
| 14 | +define('WFV_VALIDATE_VERSION', '0.11.0'); |
|
| 15 | +define('WFV_VALIDATE__MINIMUM_WP_VERSION', '3.7'); |
|
| 16 | +define('WFV_VALIDATE__PLUGIN_DIR', plugin_dir_path(__FILE__)); |
|
| 17 | +define('WFV_VALIDATE__ACTION_POST', 'validate_form'); |
|
| 18 | 18 | |
| 19 | -require_once WFV_VALIDATE__PLUGIN_DIR . '/vendor/autoload.php'; |
|
| 19 | +require_once WFV_VALIDATE__PLUGIN_DIR.'/vendor/autoload.php'; |
|
| 20 | 20 | |
| 21 | 21 | use WFV\Agent\InspectionAgent; |
| 22 | 22 | use WFV\Artisan\Director; |
@@ -31,19 +31,19 @@ discard block |
||
| 31 | 31 | * @param array $form Form arguments |
| 32 | 32 | * @param bool $trim Trim whitespace from beginning and end of string |
| 33 | 33 | */ |
| 34 | -function wfv_create( $action, array &$form, $trim = true ) { |
|
| 35 | - $inspect = new InspectionAgent( $action ); |
|
| 36 | - $input = ( true === $inspect->safe_submit() ) ? $_POST : array(); |
|
| 34 | +function wfv_create($action, array &$form, $trim = true) { |
|
| 35 | + $inspect = new InspectionAgent($action); |
|
| 36 | + $input = (true === $inspect->safe_submit()) ? $_POST : array(); |
|
| 37 | 37 | |
| 38 | - $builder = new FormArtisan( $form ); |
|
| 39 | - $form = ( new Director( $action ) ) |
|
| 40 | - ->with( 'input', [ $input, $trim ] ) |
|
| 41 | - ->with( 'rules' ) |
|
| 42 | - ->with( 'errors' ) |
|
| 43 | - ->with( 'validator' ) |
|
| 44 | - ->compose( $builder ); |
|
| 38 | + $builder = new FormArtisan($form); |
|
| 39 | + $form = (new Director($action)) |
|
| 40 | + ->with('input', [$input, $trim]) |
|
| 41 | + ->with('rules') |
|
| 42 | + ->with('errors') |
|
| 43 | + ->with('validator') |
|
| 44 | + ->compose($builder); |
|
| 45 | 45 | |
| 46 | - if( $input ) { |
|
| 46 | + if ($input) { |
|
| 47 | 47 | $form->validate(); |
| 48 | 48 | } |
| 49 | 49 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Collection; |
| 3 | -defined( 'ABSPATH' ) or die(); |
|
| 3 | +defined('ABSPATH') or die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Abstraction\Collectable; |
| 6 | 6 | |
@@ -18,8 +18,8 @@ discard block |
||
| 18 | 18 | * |
| 19 | 19 | * @param array $rules |
| 20 | 20 | */ |
| 21 | - public function __construct( array $rules ) { |
|
| 22 | - $this->data = $this->parse_rules( $rules ); |
|
| 21 | + public function __construct(array $rules) { |
|
| 22 | + $this->data = $this->parse_rules($rules); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | |
@@ -42,10 +42,10 @@ discard block |
||
| 42 | 42 | * @param string $rule |
| 43 | 43 | * @return bool |
| 44 | 44 | */ |
| 45 | - public function is_custom( $rule ) { |
|
| 45 | + public function is_custom($rule) { |
|
| 46 | 46 | // WIP - needs adjusting to new rule string format |
| 47 | - if ( false === is_array( $rule ) ){ |
|
| 48 | - return ( false !== strpos( $rule, 'custom:' ) ) ? true : false; |
|
| 47 | + if (false === is_array($rule)) { |
|
| 48 | + return (false !== strpos($rule, 'custom:')) ? true : false; |
|
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | |
@@ -58,8 +58,8 @@ discard block |
||
| 58 | 58 | * @param string $rule |
| 59 | 59 | * @return bool |
| 60 | 60 | */ |
| 61 | - protected function extract_name( $rule ) { |
|
| 62 | - return strstr( $rule, ':', true ); |
|
| 61 | + protected function extract_name($rule) { |
|
| 62 | + return strstr($rule, ':', true); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -71,8 +71,8 @@ discard block |
||
| 71 | 71 | * @param string $rule |
| 72 | 72 | * @return bool |
| 73 | 73 | */ |
| 74 | - protected function extract_params( $rule ) { |
|
| 75 | - return ltrim( strstr($rule, ':'), ':'); |
|
| 74 | + protected function extract_params($rule) { |
|
| 75 | + return ltrim(strstr($rule, ':'), ':'); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
@@ -84,8 +84,8 @@ discard block |
||
| 84 | 84 | * @param string $rule |
| 85 | 85 | * @return bool |
| 86 | 86 | */ |
| 87 | - protected function has_parameters( $rule ) { |
|
| 88 | - return strpos( $rule, ':' ); |
|
| 87 | + protected function has_parameters($rule) { |
|
| 88 | + return strpos($rule, ':'); |
|
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | /** |
@@ -98,19 +98,19 @@ discard block |
||
| 98 | 98 | * @param array $rules |
| 99 | 99 | * @return array |
| 100 | 100 | */ |
| 101 | - protected function parse_rules( array $rules ) { |
|
| 101 | + protected function parse_rules(array $rules) { |
|
| 102 | 102 | // WIP - works, but confusing - simplify or breakdown into small methods |
| 103 | - $this->split_rules( $rules ); |
|
| 104 | - foreach( $rules as $field => $ruleset ) { |
|
| 105 | - $parsed[ $field ] = array_map( function( $rule ) { |
|
| 106 | - if ( $this->has_parameters( $rule ) ) { |
|
| 103 | + $this->split_rules($rules); |
|
| 104 | + foreach ($rules as $field => $ruleset) { |
|
| 105 | + $parsed[$field] = array_map(function($rule) { |
|
| 106 | + if ($this->has_parameters($rule)) { |
|
| 107 | 107 | return array( |
| 108 | - 'rule' => $this->extract_name( $rule ), |
|
| 109 | - 'params' => explode( ',', $this->extract_params( $rule ) ) |
|
| 108 | + 'rule' => $this->extract_name($rule), |
|
| 109 | + 'params' => explode(',', $this->extract_params($rule)) |
|
| 110 | 110 | ); |
| 111 | 111 | } |
| 112 | 112 | return $rule; |
| 113 | - }, $ruleset ); |
|
| 113 | + }, $ruleset); |
|
| 114 | 114 | } |
| 115 | 115 | return $parsed; |
| 116 | 116 | } |
@@ -124,10 +124,10 @@ discard block |
||
| 124 | 124 | * @param array $rules |
| 125 | 125 | * @return array |
| 126 | 126 | */ |
| 127 | - protected function split_rules( array &$rules ) { |
|
| 127 | + protected function split_rules(array &$rules) { |
|
| 128 | 128 | // perhaps the $rules array structure should be validated here?... |
| 129 | - $rules = array_map( function( $item ) { |
|
| 130 | - return explode( '|', $item ); |
|
| 131 | - }, $rules ); |
|
| 129 | + $rules = array_map(function($item) { |
|
| 130 | + return explode('|', $item); |
|
| 131 | + }, $rules); |
|
| 132 | 132 | } |
| 133 | 133 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Collection; |
| 3 | -defined( 'ABSPATH' ) or die(); |
|
| 3 | +defined('ABSPATH') or die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Abstraction\Collectable; |
| 6 | 6 | |
@@ -20,9 +20,9 @@ discard block |
||
| 20 | 20 | * @param array $data |
| 21 | 21 | * @param bool $trim |
| 22 | 22 | */ |
| 23 | - function __construct( array $data = array(), $trim ) { |
|
| 24 | - $data = $this->transform_array_leafs( $data, 'stripslashes' ); |
|
| 25 | - $this->data = ( $trim ) ? $this->transform_array_leafs( $data, 'trim' ) : $data; |
|
| 23 | + function __construct(array $data = array(), $trim) { |
|
| 24 | + $data = $this->transform_array_leafs($data, 'stripslashes'); |
|
| 25 | + $this->data = ($trim) ? $this->transform_array_leafs($data, 'trim') : $data; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
@@ -33,8 +33,8 @@ discard block |
||
| 33 | 33 | * @param bool $tokens With or without token and action attributes. |
| 34 | 34 | * @return array |
| 35 | 35 | */ |
| 36 | - public function get_array( $tokens = true ) { |
|
| 37 | - return ( $tokens ) ? $this->data : $this->neat_array(); |
|
| 36 | + public function get_array($tokens = true) { |
|
| 37 | + return ($tokens) ? $this->data : $this->neat_array(); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
@@ -47,8 +47,8 @@ discard block |
||
| 47 | 47 | */ |
| 48 | 48 | protected function neat_array() { |
| 49 | 49 | $input = $this->data; |
| 50 | - unset( $input[ $input['action'] .'_token'] ); |
|
| 51 | - unset( $input['action'] ); |
|
| 50 | + unset($input[$input['action'].'_token']); |
|
| 51 | + unset($input['action']); |
|
| 52 | 52 | return $input; |
| 53 | 53 | } |
| 54 | 54 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Collection; |
| 3 | -defined( 'ABSPATH' ) or die(); |
|
| 3 | +defined('ABSPATH') or die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Abstraction\Collectable; |
| 6 | 6 | |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | * |
| 27 | 27 | * @param array $labels Human friendly labels for the fields |
| 28 | 28 | */ |
| 29 | - function __construct( array $labels = [] ) { |
|
| 29 | + function __construct(array $labels = []) { |
|
| 30 | 30 | $this->labels = $labels; |
| 31 | 31 | } |
| 32 | 32 | |
@@ -38,9 +38,9 @@ discard block |
||
| 38 | 38 | * @param string $field |
| 39 | 39 | * @return string|null |
| 40 | 40 | */ |
| 41 | - public function first( $field ) { |
|
| 42 | - return ( $this->data[ $field ] ) |
|
| 43 | - ? array_values( $this->data[ $field ] )[0] |
|
| 41 | + public function first($field) { |
|
| 42 | + return ($this->data[$field]) |
|
| 43 | + ? array_values($this->data[$field])[0] |
|
| 44 | 44 | : null; |
| 45 | 45 | } |
| 46 | 46 | |
@@ -52,10 +52,10 @@ discard block |
||
| 52 | 52 | * |
| 53 | 53 | * @param array $errors |
| 54 | 54 | */ |
| 55 | - public function set_errors( array $errors = [] ) { |
|
| 56 | - $this->data = ( $this->is_populated() ) |
|
| 55 | + public function set_errors(array $errors = []) { |
|
| 56 | + $this->data = ($this->is_populated()) |
|
| 57 | 57 | ? $this->data |
| 58 | - : $this->with_labels( $errors ); |
|
| 58 | + : $this->with_labels($errors); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
@@ -68,9 +68,9 @@ discard block |
||
| 68 | 68 | * @param string $field |
| 69 | 69 | * @return string |
| 70 | 70 | */ |
| 71 | - protected function label( $field ) { |
|
| 72 | - return ( isset( $this->labels[ $field ] ) ) |
|
| 73 | - ? $this->labels[ $field ] |
|
| 71 | + protected function label($field) { |
|
| 72 | + return (isset($this->labels[$field])) |
|
| 73 | + ? $this->labels[$field] |
|
| 74 | 74 | : $field; |
| 75 | 75 | } |
| 76 | 76 | |
@@ -83,10 +83,10 @@ discard block |
||
| 83 | 83 | * @param array $errors |
| 84 | 84 | * @return array |
| 85 | 85 | */ |
| 86 | - protected function with_labels( $errors ) { |
|
| 87 | - foreach( $errors as $field => $messages ) { |
|
| 88 | - $label = $this->label( $field ); |
|
| 89 | - $labeled[ $field ] = str_replace( '{label}', $label, $errors[ $field ] ); |
|
| 86 | + protected function with_labels($errors) { |
|
| 87 | + foreach ($errors as $field => $messages) { |
|
| 88 | + $label = $this->label($field); |
|
| 89 | + $labeled[$field] = str_replace('{label}', $label, $errors[$field]); |
|
| 90 | 90 | } |
| 91 | 91 | return $labeled; |
| 92 | 92 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) or die(); |
|
| 3 | +defined('ABSPATH') or die(); |
|
| 4 | 4 | |
| 5 | 5 | use \Respect\Validation\Validator as RespectValidator; |
| 6 | 6 | use \Respect\Validation\Exceptions\NestedValidationException; |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | * @param bool (optional) $optional |
| 68 | 68 | * @param string (optional) $message |
| 69 | 69 | */ |
| 70 | - function __construct( RespectValidator $validator, $field, $optional = false, $message = false ) { |
|
| 70 | + function __construct(RespectValidator $validator, $field, $optional = false, $message = false) { |
|
| 71 | 71 | // WIP - simplify - parameter overload... |
| 72 | 72 | |
| 73 | 73 | $this->optional = $optional; |
@@ -76,10 +76,10 @@ discard block |
||
| 76 | 76 | |
| 77 | 77 | $args = func_get_args(); |
| 78 | 78 | |
| 79 | - $this->params = ( isset( $args[4] ) ) ? $args[4] : null; |
|
| 79 | + $this->params = (isset($args[4])) ? $args[4] : null; |
|
| 80 | 80 | |
| 81 | - if( $message ) { |
|
| 82 | - $this->set_message( $message ); |
|
| 81 | + if ($message) { |
|
| 82 | + $this->set_message($message); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | $this->set_policy(); |
@@ -126,8 +126,8 @@ discard block |
||
| 126 | 126 | * @param string|array $input |
| 127 | 127 | * @return bool |
| 128 | 128 | */ |
| 129 | - public function validate( $value ) { |
|
| 130 | - $is_valid = $this->validator->validate( $value ); |
|
| 129 | + public function validate($value) { |
|
| 130 | + $is_valid = $this->validator->validate($value); |
|
| 131 | 131 | return $is_valid; |
| 132 | 132 | } |
| 133 | 133 | |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | * |
| 139 | 139 | * @param |
| 140 | 140 | */ |
| 141 | - protected function set_message( $message ) { |
|
| 141 | + protected function set_message($message) { |
|
| 142 | 142 | $this->template['message'] = $message; |
| 143 | 143 | } |
| 144 | 144 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) or die(); |
|
| 3 | +defined('ABSPATH') or die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) or die(); |
|
| 3 | +defined('ABSPATH') or die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -32,8 +32,8 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | protected function set_policy() { |
| 34 | 34 | $v = $this->validator; |
| 35 | - $v = ( $this->optional ) |
|
| 36 | - ? $v->optional( $v->create()->boolVal() ) |
|
| 35 | + $v = ($this->optional) |
|
| 36 | + ? $v->optional($v->create()->boolVal()) |
|
| 37 | 37 | : $v->boolVal(); |
| 38 | 38 | } |
| 39 | 39 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) or die(); |
|
| 3 | +defined('ABSPATH') or die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -32,8 +32,8 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | protected function set_policy() { |
| 34 | 34 | $v = $this->validator; |
| 35 | - $v = ( $this->optional ) |
|
| 36 | - ? $v->optional( $v->create()->alpha('-_') ) |
|
| 35 | + $v = ($this->optional) |
|
| 36 | + ? $v->optional($v->create()->alpha('-_')) |
|
| 37 | 37 | : $v->alpha('-_'); |
| 38 | 38 | } |
| 39 | 39 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) or die(); |
|
| 3 | +defined('ABSPATH') or die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -32,8 +32,8 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | protected function set_policy() { |
| 34 | 34 | $v = $this->validator; |
| 35 | - $v = ( $this->optional ) |
|
| 36 | - ? $v->optional( $v->create()->callback( $this->params[0] ) ) |
|
| 37 | - : $v->callback( $this->params[0] ); |
|
| 35 | + $v = ($this->optional) |
|
| 36 | + ? $v->optional($v->create()->callback($this->params[0])) |
|
| 37 | + : $v->callback($this->params[0]); |
|
| 38 | 38 | } |
| 39 | 39 | } |