@@ -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\FormComposite; |
| 22 | 22 | use WFV\Agent\InspectionAgent; |
@@ -33,21 +33,21 @@ discard block |
||
| 33 | 33 | * @param array $form Form arguments |
| 34 | 34 | * @param bool $trim Trim whitespace from beginning and end of string |
| 35 | 35 | */ |
| 36 | -function wfv_create( $action, array &$form, $trim = true ) { |
|
| 37 | - $inspect = new InspectionAgent( $action ); |
|
| 38 | - $input = ( true === $inspect->safe_submit() ) ? $_POST : array(); |
|
| 36 | +function wfv_create($action, array &$form, $trim = true) { |
|
| 37 | + $inspect = new InspectionAgent($action); |
|
| 38 | + $input = (true === $inspect->safe_submit()) ? $_POST : array(); |
|
| 39 | 39 | |
| 40 | - $builder = new FormArtisan( $form ); |
|
| 41 | - $form = ( new Director( $action ) ) |
|
| 42 | - ->with( 'input', [ $input, $trim ] ) |
|
| 43 | - ->with( 'rules' ) |
|
| 44 | - ->with( 'errors' ) |
|
| 45 | - ->with( 'messages' ) |
|
| 46 | - ->with( 'validator' ) |
|
| 47 | - ->compose( $builder ); |
|
| 40 | + $builder = new FormArtisan($form); |
|
| 41 | + $form = (new Director($action)) |
|
| 42 | + ->with('input', [$input, $trim]) |
|
| 43 | + ->with('rules') |
|
| 44 | + ->with('errors') |
|
| 45 | + ->with('messages') |
|
| 46 | + ->with('validator') |
|
| 47 | + ->compose($builder); |
|
| 48 | 48 | |
| 49 | - if( $input ) { |
|
| 50 | - wfv_validate( $form ); |
|
| 49 | + if ($input) { |
|
| 50 | + wfv_validate($form); |
|
| 51 | 51 | } |
| 52 | 52 | } |
| 53 | 53 | |
@@ -59,21 +59,21 @@ discard block |
||
| 59 | 59 | * @param FormComposite $form |
| 60 | 60 | * @return bool |
| 61 | 61 | */ |
| 62 | -function wfv_validate( FormComposite $form ) { |
|
| 62 | +function wfv_validate(FormComposite $form) { |
|
| 63 | 63 | $rules = $form->rules()->get_array(); |
| 64 | 64 | $messages = $form->messages()->get_array(); |
| 65 | - $factory = new ValidatorFactory( $messages ); |
|
| 65 | + $factory = new ValidatorFactory($messages); |
|
| 66 | 66 | |
| 67 | - foreach( $rules as $field => $ruleset ) { |
|
| 67 | + foreach ($rules as $field => $ruleset) { |
|
| 68 | 68 | $optional = in_array('optional', $ruleset); |
| 69 | - if( $optional ) { |
|
| 70 | - array_shift( $ruleset ); |
|
| 69 | + if ($optional) { |
|
| 70 | + array_shift($ruleset); |
|
| 71 | 71 | } |
| 72 | - foreach( $ruleset as $rule ) { |
|
| 73 | - $params = ( is_array( $rule ) ) ? $rule['params'] : null; |
|
| 74 | - $rule_name = ( is_string( $rule ) ) ? $rule : $rule['rule']; |
|
| 75 | - $validator = $factory->create( $rule_name, $field, $params, $optional ); |
|
| 76 | - $form->validate( $validator, $field ); |
|
| 72 | + foreach ($ruleset as $rule) { |
|
| 73 | + $params = (is_array($rule)) ? $rule['params'] : null; |
|
| 74 | + $rule_name = (is_string($rule)) ? $rule : $rule['rule']; |
|
| 75 | + $validator = $factory->create($rule_name, $field, $params, $optional); |
|
| 76 | + $form->validate($validator, $field); |
|
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | return $form->is_valid(); |
@@ -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 | use WFV\Abstraction\Collectable; |
| 5 | 5 | /** |
| 6 | 6 | * |
@@ -16,8 +16,8 @@ discard block |
||
| 16 | 16 | * |
| 17 | 17 | * @param array $form Config array |
| 18 | 18 | */ |
| 19 | - function __construct( array $form ) { |
|
| 20 | - $this->set_messages( $form ); |
|
| 19 | + function __construct(array $form) { |
|
| 20 | + $this->set_messages($form); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -41,9 +41,9 @@ discard block |
||
| 41 | 41 | * @param array $form |
| 42 | 42 | * @return array |
| 43 | 43 | */ |
| 44 | - protected function filter_config( array $form ) { |
|
| 45 | - return array_filter( $form, function( $item ) { |
|
| 46 | - return array_key_exists('messages', $item ); |
|
| 44 | + protected function filter_config(array $form) { |
|
| 45 | + return array_filter($form, function($item) { |
|
| 46 | + return array_key_exists('messages', $item); |
|
| 47 | 47 | }); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -55,9 +55,9 @@ discard block |
||
| 55 | 55 | * |
| 56 | 56 | * @param array |
| 57 | 57 | */ |
| 58 | - protected function make_array( array $filtered ) { |
|
| 59 | - foreach( $filtered as $field => $options ) { |
|
| 60 | - $messages[ $field ] = $options['messages']; |
|
| 58 | + protected function make_array(array $filtered) { |
|
| 59 | + foreach ($filtered as $field => $options) { |
|
| 60 | + $messages[$field] = $options['messages']; |
|
| 61 | 61 | } |
| 62 | 62 | return $messages; |
| 63 | 63 | } |
@@ -70,8 +70,8 @@ discard block |
||
| 70 | 70 | * |
| 71 | 71 | * @param array $form |
| 72 | 72 | */ |
| 73 | - protected function set_messages( array $form ) { |
|
| 74 | - $filtered = $this->filter_config( $form ); |
|
| 75 | - $this->data = $this->make_array( $filtered ); |
|
| 73 | + protected function set_messages(array $form) { |
|
| 74 | + $filtered = $this->filter_config($form); |
|
| 75 | + $this->data = $this->make_array($filtered); |
|
| 76 | 76 | } |
| 77 | 77 | } |
@@ -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; |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * @since 0.11.0 |
| 47 | 47 | * |
| 48 | 48 | */ |
| 49 | - abstract protected function set_policy( $optional = false ); |
|
| 49 | + abstract protected function set_policy($optional = false); |
|
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | 52 | * |
@@ -54,11 +54,11 @@ discard block |
||
| 54 | 54 | * @since 0.11.0 |
| 55 | 55 | * |
| 56 | 56 | */ |
| 57 | - function __construct( $field ) { |
|
| 57 | + function __construct($field) { |
|
| 58 | 58 | $this->validator = new RespectValidator(); |
| 59 | 59 | $this->field = $field; |
| 60 | 60 | $args = func_get_args(); |
| 61 | - $this->params = ( isset( $args[1] ) ) ? $args[1] : null; |
|
| 61 | + $this->params = (isset($args[1])) ? $args[1] : null; |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | * |
| 91 | 91 | * @param |
| 92 | 92 | */ |
| 93 | - public function set_message( $message ) { |
|
| 93 | + public function set_message($message) { |
|
| 94 | 94 | $this->template['message'] = $message; |
| 95 | 95 | } |
| 96 | 96 | |
@@ -113,8 +113,8 @@ discard block |
||
| 113 | 113 | * @param string|array $input |
| 114 | 114 | * @return bool |
| 115 | 115 | */ |
| 116 | - public function validate( $value ) { |
|
| 117 | - $is_valid = $this->validator->validate( $value ); |
|
| 116 | + public function validate($value) { |
|
| 117 | + $is_valid = $this->validator->validate($value); |
|
| 118 | 118 | return $is_valid; |
| 119 | 119 | } |
| 120 | 120 | } |
@@ -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 | |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * @since 0.11.0 |
| 31 | 31 | * |
| 32 | 32 | */ |
| 33 | - public function set_policy( $optional = false ) { |
|
| 33 | + public function set_policy($optional = false) { |
|
| 34 | 34 | $this->validator->notEmpty(); |
| 35 | 35 | return $this; |
| 36 | 36 | } |
@@ -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 | |
@@ -29,10 +29,10 @@ discard block |
||
| 29 | 29 | * @since 0.11.0 |
| 30 | 30 | * |
| 31 | 31 | */ |
| 32 | - public function set_policy( $optional = false ) { |
|
| 32 | + public function set_policy($optional = false) { |
|
| 33 | 33 | $v = $this->validator; |
| 34 | - $v = ( $optional ) |
|
| 35 | - ? $v->optional( $v->create()->boolVal() ) |
|
| 34 | + $v = ($optional) |
|
| 35 | + ? $v->optional($v->create()->boolVal()) |
|
| 36 | 36 | : $v->boolVal(); |
| 37 | 37 | return $this; |
| 38 | 38 | } |
@@ -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 | |
@@ -29,10 +29,10 @@ discard block |
||
| 29 | 29 | * @since 0.11.0 |
| 30 | 30 | * |
| 31 | 31 | */ |
| 32 | - public function set_policy( $optional = false ) { |
|
| 32 | + public function set_policy($optional = false) { |
|
| 33 | 33 | $v = $this->validator; |
| 34 | - $v = ( $optional ) |
|
| 35 | - ? $v->optional( $v->create()->alpha('-_') ) |
|
| 34 | + $v = ($optional) |
|
| 35 | + ? $v->optional($v->create()->alpha('-_')) |
|
| 36 | 36 | : $v->alpha('-_'); |
| 37 | 37 | return $this; |
| 38 | 38 | } |
@@ -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 | |
@@ -29,11 +29,11 @@ discard block |
||
| 29 | 29 | * @since 0.11.0 |
| 30 | 30 | * |
| 31 | 31 | */ |
| 32 | - public function set_policy( $optional = false ) { |
|
| 32 | + public function set_policy($optional = false) { |
|
| 33 | 33 | $v = $this->validator; |
| 34 | - $v = ( $optional ) |
|
| 35 | - ? $v->optional( $v->create()->callback( $this->params[0] ) ) |
|
| 36 | - : $v->callback( $this->params[0] ); |
|
| 34 | + $v = ($optional) |
|
| 35 | + ? $v->optional($v->create()->callback($this->params[0])) |
|
| 36 | + : $v->callback($this->params[0]); |
|
| 37 | 37 | return $this; |
| 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 | |
@@ -29,14 +29,14 @@ discard block |
||
| 29 | 29 | * @since 0.11.0 |
| 30 | 30 | * |
| 31 | 31 | */ |
| 32 | - public function set_policy( $optional = false ) { |
|
| 32 | + public function set_policy($optional = false) { |
|
| 33 | 33 | $start = $this->params[0]; |
| 34 | 34 | $end = $this->params[1]; |
| 35 | 35 | |
| 36 | 36 | $v = $this->validator; |
| 37 | - $v = ( $optional ) |
|
| 38 | - ? $v->optional( $v->create()->between( $start, $end ) ) |
|
| 39 | - : $v->between( $start, $end ); |
|
| 37 | + $v = ($optional) |
|
| 38 | + ? $v->optional($v->create()->between($start, $end)) |
|
| 39 | + : $v->between($start, $end); |
|
| 40 | 40 | return $this; |
| 41 | 41 | } |
| 42 | 42 | } |
@@ -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 | |
@@ -31,14 +31,14 @@ discard block |
||
| 31 | 31 | * @since 0.11.0 |
| 32 | 32 | * |
| 33 | 33 | */ |
| 34 | - public function set_policy( $optional = false ) { |
|
| 34 | + public function set_policy($optional = false) { |
|
| 35 | 35 | $other_field = $this->params[0]; |
| 36 | 36 | $v = $this->validator; |
| 37 | 37 | |
| 38 | 38 | $v->when( |
| 39 | - $v->create()->key( $other_field, $v->create()->notEmpty() ), |
|
| 40 | - $v->create()->key( $this->field, $v->create()->notEmpty() ), |
|
| 41 | - $v->create()->key( $this->field, $v->create()->alwaysValid() ) |
|
| 39 | + $v->create()->key($other_field, $v->create()->notEmpty()), |
|
| 40 | + $v->create()->key($this->field, $v->create()->notEmpty()), |
|
| 41 | + $v->create()->key($this->field, $v->create()->alwaysValid()) |
|
| 42 | 42 | ); |
| 43 | 43 | return $this; |
| 44 | 44 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | * @param string|array $input |
| 52 | 52 | * @return bool |
| 53 | 53 | */ |
| 54 | - public function validate( $input ){ |
|
| 55 | - return $this->validator->validate( $_POST ); |
|
| 54 | + public function validate($input) { |
|
| 55 | + return $this->validator->validate($_POST); |
|
| 56 | 56 | } |
| 57 | 57 | } |