@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -32,14 +32,14 @@ discard block |
||
| 32 | 32 | * @param bool (optional) $optional |
| 33 | 33 | * @return bool |
| 34 | 34 | */ |
| 35 | - public function validate( $input = null, $optional = false ) { |
|
| 35 | + public function validate($input = null, $optional = false) { |
|
| 36 | 36 | $args = func_get_args(); |
| 37 | 37 | $params = $args[2]; |
| 38 | 38 | $min_value = $params[0]; |
| 39 | 39 | |
| 40 | 40 | $v = $this->validator->create(); |
| 41 | - return ( $optional ) |
|
| 42 | - ? $v->optional( $v->create()->length( $min_value, null ) )->validate( $input ) |
|
| 43 | - : $v->length( $min_value, null )->validate( $input ); |
|
| 41 | + return ($optional) |
|
| 42 | + ? $v->optional($v->create()->length($min_value, null))->validate($input) |
|
| 43 | + : $v->length($min_value, null)->validate($input); |
|
| 44 | 44 | } |
| 45 | 45 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -32,10 +32,10 @@ discard block |
||
| 32 | 32 | * @param bool (optional) $optional |
| 33 | 33 | * @return bool |
| 34 | 34 | */ |
| 35 | - public function validate( $input = null, $optional = false ) { |
|
| 35 | + public function validate($input = null, $optional = false) { |
|
| 36 | 36 | $v = $this->validator->create(); |
| 37 | - return ( $optional ) |
|
| 38 | - ? $v->optional( $v->create()->url() )->validate( $input ) |
|
| 39 | - : $v->url()->validate( $input ); |
|
| 37 | + return ($optional) |
|
| 38 | + ? $v->optional($v->create()->url())->validate($input) |
|
| 39 | + : $v->url()->validate($input); |
|
| 40 | 40 | } |
| 41 | 41 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -32,10 +32,10 @@ discard block |
||
| 32 | 32 | * @param bool (optional) $optional |
| 33 | 33 | * @return bool |
| 34 | 34 | */ |
| 35 | - public function validate( $input = null, $optional = false ) { |
|
| 35 | + public function validate($input = null, $optional = false) { |
|
| 36 | 36 | $v = $this->validator->create(); |
| 37 | - return ( $optional ) |
|
| 38 | - ? $v->optional( $v->create()->phone() )->validate( $input ) |
|
| 39 | - : $v->phone()->validate( $input ); |
|
| 37 | + return ($optional) |
|
| 38 | + ? $v->optional($v->create()->phone())->validate($input) |
|
| 39 | + : $v->phone()->validate($input); |
|
| 40 | 40 | } |
| 41 | 41 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Collection; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || 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 | |
@@ -32,8 +32,8 @@ discard block |
||
| 32 | 32 | * @param bool (optional) $flat |
| 33 | 33 | * @return array |
| 34 | 34 | */ |
| 35 | - public function get_array( $flat = false ) { |
|
| 36 | - return ( $flat ) ? $this->remove_params() : $this->data; |
|
| 35 | + public function get_array($flat = false) { |
|
| 36 | + return ($flat) ? $this->remove_params() : $this->data; |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | /** |
@@ -45,9 +45,9 @@ discard block |
||
| 45 | 45 | * @param int $index |
| 46 | 46 | * @return array|bool |
| 47 | 47 | */ |
| 48 | - public function get_params( $field, $index ) { |
|
| 49 | - return ( $this-> has_params( $field, $index ) ) |
|
| 50 | - ? $this->data[ $field ][ $index ]['params'] |
|
| 48 | + public function get_params($field, $index) { |
|
| 49 | + return ($this-> has_params($field, $index)) |
|
| 50 | + ? $this->data[$field][$index]['params'] |
|
| 51 | 51 | : false; |
| 52 | 52 | } |
| 53 | 53 | |
@@ -59,9 +59,9 @@ discard block |
||
| 59 | 59 | * @param string $field |
| 60 | 60 | * @return bool |
| 61 | 61 | */ |
| 62 | - public function is_optional( $field ) { |
|
| 63 | - if( $this->has( $field ) ){ |
|
| 64 | - return in_array('optional', $this->data[ $field ] ); |
|
| 62 | + public function is_optional($field) { |
|
| 63 | + if ($this->has($field)) { |
|
| 64 | + return in_array('optional', $this->data[$field]); |
|
| 65 | 65 | } |
| 66 | 66 | return false; |
| 67 | 67 | } |
@@ -74,8 +74,8 @@ discard block |
||
| 74 | 74 | * @return array |
| 75 | 75 | */ |
| 76 | 76 | public function unique() { |
| 77 | - $flat = $this->flatten( $this->remove_params() ); |
|
| 78 | - return array_values( array_unique( $flat ) ); |
|
| 77 | + $flat = $this->flatten($this->remove_params()); |
|
| 78 | + return array_values(array_unique($flat)); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | /** |
@@ -87,8 +87,8 @@ discard block |
||
| 87 | 87 | * @param string $rule |
| 88 | 88 | * @return string |
| 89 | 89 | */ |
| 90 | - protected function extract_name( $rule ) { |
|
| 91 | - return strstr( $rule, ':', true ); |
|
| 90 | + protected function extract_name($rule) { |
|
| 91 | + return strstr($rule, ':', true); |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | /** |
@@ -100,8 +100,8 @@ discard block |
||
| 100 | 100 | * @param string $rule |
| 101 | 101 | * @return string |
| 102 | 102 | */ |
| 103 | - protected function extract_params( $rule ) { |
|
| 104 | - return ltrim( strstr($rule, ':'), ':'); |
|
| 103 | + protected function extract_params($rule) { |
|
| 104 | + return ltrim(strstr($rule, ':'), ':'); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -113,11 +113,11 @@ discard block |
||
| 113 | 113 | * @param array $array |
| 114 | 114 | * @return array |
| 115 | 115 | */ |
| 116 | - protected function flatten( array $array ) { |
|
| 116 | + protected function flatten(array $array) { |
|
| 117 | 117 | $flat = array(); |
| 118 | - foreach( $array as $rule ) { |
|
| 119 | - if( is_array( $rule ) ){ |
|
| 120 | - $flat = array_merge( $flat, $this->flatten( $rule ) ); |
|
| 118 | + foreach ($array as $rule) { |
|
| 119 | + if (is_array($rule)) { |
|
| 120 | + $flat = array_merge($flat, $this->flatten($rule)); |
|
| 121 | 121 | } else { |
| 122 | 122 | $flat[] = $rule; |
| 123 | 123 | } |
@@ -135,8 +135,8 @@ discard block |
||
| 135 | 135 | * @param int $index |
| 136 | 136 | * @return bool |
| 137 | 137 | */ |
| 138 | - protected function has_params( $field, $index ) { |
|
| 139 | - return is_array( $this->data[ $field ][ $index ] ); |
|
| 138 | + protected function has_params($field, $index) { |
|
| 139 | + return is_array($this->data[$field][$index]); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | /** |
@@ -148,8 +148,8 @@ discard block |
||
| 148 | 148 | * @param string $rule |
| 149 | 149 | * @return bool |
| 150 | 150 | */ |
| 151 | - protected function string_has_params( $rule ) { |
|
| 152 | - return strpos( $rule, ':' ); |
|
| 151 | + protected function string_has_params($rule) { |
|
| 152 | + return strpos($rule, ':'); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | /** |
@@ -162,20 +162,20 @@ discard block |
||
| 162 | 162 | * @param array $rules |
| 163 | 163 | * @return array |
| 164 | 164 | */ |
| 165 | - protected function parse_rules( array $rules ) { |
|
| 165 | + protected function parse_rules(array $rules) { |
|
| 166 | 166 | // WIP - works, but confusing - simplify or breakdown into small methods |
| 167 | 167 | $parsed = array(); |
| 168 | - $this->split_rules( $rules ); |
|
| 169 | - foreach( $rules as $field => $ruleset ) { |
|
| 170 | - $parsed[ $field ] = array_map( function( $rule ) { |
|
| 171 | - if ( $this->string_has_params( $rule ) ) { |
|
| 168 | + $this->split_rules($rules); |
|
| 169 | + foreach ($rules as $field => $ruleset) { |
|
| 170 | + $parsed[$field] = array_map(function($rule) { |
|
| 171 | + if ($this->string_has_params($rule)) { |
|
| 172 | 172 | return array( |
| 173 | - 'rule' => $this->extract_name( $rule ), |
|
| 174 | - 'params' => explode( ',', $this->extract_params( $rule ) ) |
|
| 173 | + 'rule' => $this->extract_name($rule), |
|
| 174 | + 'params' => explode(',', $this->extract_params($rule)) |
|
| 175 | 175 | ); |
| 176 | 176 | } |
| 177 | 177 | return $rule; |
| 178 | - }, $ruleset ); |
|
| 178 | + }, $ruleset); |
|
| 179 | 179 | } |
| 180 | 180 | return $parsed; |
| 181 | 181 | } |
@@ -190,14 +190,14 @@ discard block |
||
| 190 | 190 | * @return array |
| 191 | 191 | */ |
| 192 | 192 | protected function remove_params() { |
| 193 | - return array_map( function( $item ) { |
|
| 194 | - foreach( $item as $rule ) { |
|
| 195 | - if( $rule !== 'optional' ) { |
|
| 196 | - $rules[] = ( is_string( $rule ) ) ? $rule : $rule['rule']; |
|
| 193 | + return array_map(function($item) { |
|
| 194 | + foreach ($item as $rule) { |
|
| 195 | + if ($rule !== 'optional') { |
|
| 196 | + $rules[] = (is_string($rule)) ? $rule : $rule['rule']; |
|
| 197 | 197 | } |
| 198 | 198 | } |
| 199 | 199 | return $rules; |
| 200 | - }, $this->data ); |
|
| 200 | + }, $this->data); |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /** |
@@ -208,10 +208,10 @@ discard block |
||
| 208 | 208 | * |
| 209 | 209 | * @param array $rules |
| 210 | 210 | */ |
| 211 | - protected function split_rules( array &$rules ) { |
|
| 211 | + protected function split_rules(array &$rules) { |
|
| 212 | 212 | // perhaps the $rules array structure should be validated here?... |
| 213 | - $rules = array_map( function( $item ) { |
|
| 214 | - return explode( '|', $item ); |
|
| 215 | - }, $rules ); |
|
| 213 | + $rules = array_map(function($item) { |
|
| 214 | + return explode('|', $item); |
|
| 215 | + }, $rules); |
|
| 216 | 216 | } |
| 217 | 217 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Collection\MessageCollection; |
| 6 | 6 | use WFV\Contract\ValidateInterface; |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @param |
| 40 | 40 | */ |
| 41 | - public function __construct( MessageCollection $messages ) { |
|
| 41 | + public function __construct(MessageCollection $messages) { |
|
| 42 | 42 | $this->messages = $messages; |
| 43 | 43 | } |
| 44 | 44 | |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | * @return bool |
| 62 | 62 | */ |
| 63 | 63 | public function is_valid() { |
| 64 | - return empty( $this->errors ); |
|
| 64 | + return empty($this->errors); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | /** |
@@ -76,11 +76,11 @@ discard block |
||
| 76 | 76 | * @param array (optional) $params |
| 77 | 77 | * @return self |
| 78 | 78 | */ |
| 79 | - public function validate( ValidateInterface $rule, $field, $value, $optional, $params = false ) { |
|
| 80 | - $params[] = ( $params ) ? $field : false; |
|
| 81 | - $valid = $rule->validate( $value, $optional, $params ); |
|
| 82 | - if( !$valid ){ |
|
| 83 | - $this->add_error( $field, $rule->template() ); |
|
| 79 | + public function validate(ValidateInterface $rule, $field, $value, $optional, $params = false) { |
|
| 80 | + $params[] = ($params) ? $field : false; |
|
| 81 | + $valid = $rule->validate($value, $optional, $params); |
|
| 82 | + if (!$valid) { |
|
| 83 | + $this->add_error($field, $rule->template()); |
|
| 84 | 84 | } |
| 85 | 85 | return $this; |
| 86 | 86 | } |
@@ -94,10 +94,10 @@ discard block |
||
| 94 | 94 | * @param string $field |
| 95 | 95 | * @param array $template |
| 96 | 96 | */ |
| 97 | - protected function add_error( $field, array $template ) { |
|
| 98 | - $message = ( $this->messages->has( $field ) ) |
|
| 99 | - ? $this->messages->get_msg( $field, $template['name'] ) |
|
| 97 | + protected function add_error($field, array $template) { |
|
| 98 | + $message = ($this->messages->has($field)) |
|
| 99 | + ? $this->messages->get_msg($field, $template['name']) |
|
| 100 | 100 | : $template['message']; |
| 101 | - $this->errors[ $field ][ $template['name'] ] = $message; |
|
| 101 | + $this->errors[$field][$template['name']] = $message; |
|
| 102 | 102 | } |
| 103 | 103 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Agent; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | * |
| 27 | 27 | * @param string $action |
| 28 | 28 | */ |
| 29 | - public function __construct( $action ) { |
|
| 29 | + public function __construct($action) { |
|
| 30 | 30 | $this->action = $action; |
| 31 | 31 | } |
| 32 | 32 | |
@@ -40,8 +40,8 @@ discard block |
||
| 40 | 40 | * @return bool |
| 41 | 41 | */ |
| 42 | 42 | public function safe_submit() { |
| 43 | - if( $this->submit_has_action() ) { |
|
| 44 | - return ( $_POST['action'] === $this->action ) ? $this->nonce() : false; |
|
| 43 | + if ($this->submit_has_action()) { |
|
| 44 | + return ($_POST['action'] === $this->action) ? $this->nonce() : false; |
|
| 45 | 45 | } |
| 46 | 46 | return false; |
| 47 | 47 | } |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | * @return bool |
| 55 | 55 | */ |
| 56 | 56 | private function submit_has_action() { |
| 57 | - return isset( $_POST['action'] ); |
|
| 57 | + return isset($_POST['action']); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | /** |
@@ -66,8 +66,8 @@ discard block |
||
| 66 | 66 | * @return bool |
| 67 | 67 | */ |
| 68 | 68 | private function nonce() { |
| 69 | - $nonce = $_REQUEST[ $this->action.'_token' ]; |
|
| 70 | - return ( wp_verify_nonce( $nonce, $this->action ) ) ? true : false; |
|
| 69 | + $nonce = $_REQUEST[$this->action.'_token']; |
|
| 70 | + return (wp_verify_nonce($nonce, $this->action)) ? true : false; |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Artisan; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Agent\InspectionAgent; |
| 6 | 6 | use WFV\Contract\ArtisanInterface; |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | * |
| 61 | 61 | * @param array $config |
| 62 | 62 | */ |
| 63 | - public function __construct( array $config ) { |
|
| 63 | + public function __construct(array $config) { |
|
| 64 | 64 | $this->config = $config; |
| 65 | 65 | } |
| 66 | 66 | |
@@ -83,8 +83,8 @@ discard block |
||
| 83 | 83 | * @param string $action |
| 84 | 84 | * @return WFV\Artisan\FormArtisan |
| 85 | 85 | */ |
| 86 | - public function create( $action ) { |
|
| 87 | - $this->form = new FormComposite( $this, $action ); |
|
| 86 | + public function create($action) { |
|
| 87 | + $this->form = new FormComposite($this, $action); |
|
| 88 | 88 | return $this; |
| 89 | 89 | } |
| 90 | 90 | |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | * @return WFV\Artisan\FormArtisan |
| 98 | 98 | */ |
| 99 | 99 | public function errors() { |
| 100 | - $this->collection['errors'] = new ErrorCollection( $this->labels() ); |
|
| 100 | + $this->collection['errors'] = new ErrorCollection($this->labels()); |
|
| 101 | 101 | return $this; |
| 102 | 102 | } |
| 103 | 103 | |
@@ -110,8 +110,8 @@ discard block |
||
| 110 | 110 | * @param InspectionAgent $guard |
| 111 | 111 | * @return WFV\Artisan\FormArtisan |
| 112 | 112 | */ |
| 113 | - public function input( InspectionAgent $guard ) { |
|
| 114 | - $this->collection['input'] = new InputCollection( $guard ); |
|
| 113 | + public function input(InspectionAgent $guard) { |
|
| 114 | + $this->collection['input'] = new InputCollection($guard); |
|
| 115 | 115 | return $this; |
| 116 | 116 | } |
| 117 | 117 | |
@@ -125,10 +125,10 @@ discard block |
||
| 125 | 125 | */ |
| 126 | 126 | public function rules() { |
| 127 | 127 | $rules = array(); |
| 128 | - foreach( $this->config as $field => $options ) { |
|
| 129 | - $rules[ $field ] = $options['rules']; |
|
| 128 | + foreach ($this->config as $field => $options) { |
|
| 129 | + $rules[$field] = $options['rules']; |
|
| 130 | 130 | } |
| 131 | - $this->collection['rules'] = new RuleCollection( $rules ); |
|
| 131 | + $this->collection['rules'] = new RuleCollection($rules); |
|
| 132 | 132 | return $this; |
| 133 | 133 | } |
| 134 | 134 | |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | * @return WFV\Artisan\FormArtisan |
| 142 | 142 | */ |
| 143 | 143 | public function validator() { |
| 144 | - $this->validator = new Validator( new MessageCollection( $this->config ) ); |
|
| 144 | + $this->validator = new Validator(new MessageCollection($this->config)); |
|
| 145 | 145 | return $this; |
| 146 | 146 | } |
| 147 | 147 | |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | * @return array |
| 156 | 156 | */ |
| 157 | 157 | protected function labels() { |
| 158 | - return array_map( function( $item ) { |
|
| 158 | + return array_map(function($item) { |
|
| 159 | 159 | return $item['label']; |
| 160 | 160 | }, $this->config); |
| 161 | 161 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -defined( 'ABSPATH' ) || die(); |
|
| 2 | +defined('ABSPATH') || die(); |
|
| 3 | 3 | /* |
| 4 | 4 | Plugin Name: WFV - Form Validation |
| 5 | 5 | Plugin URI: https://macder.github.io/wfv/ |
@@ -11,11 +11,11 @@ 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__ ) ); |
|
| 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 | 17 | |
| 18 | -require_once WFV_VALIDATE__PLUGIN_DIR . '/vendor/autoload.php'; |
|
| 18 | +require_once WFV_VALIDATE__PLUGIN_DIR.'/vendor/autoload.php'; |
|
| 19 | 19 | |
| 20 | 20 | use WFV\FormComposite; |
| 21 | 21 | use WFV\Agent\InspectionAgent; |
@@ -32,19 +32,19 @@ discard block |
||
| 32 | 32 | * @param array $form Form arguments |
| 33 | 33 | * @param bool $trim Trim whitespace from beginning and end of string |
| 34 | 34 | */ |
| 35 | -function wfv_create( $action, array &$form, $trim = true ) { |
|
| 36 | - $guard = new InspectionAgent( $action ); |
|
| 35 | +function wfv_create($action, array &$form, $trim = true) { |
|
| 36 | + $guard = new InspectionAgent($action); |
|
| 37 | 37 | |
| 38 | - $builder = new FormArtisan( $form ); |
|
| 39 | - $form = ( new Director( $action ) ) |
|
| 40 | - ->with( 'input', $guard ) |
|
| 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', $guard) |
|
| 41 | + ->with('rules') |
|
| 42 | + ->with('errors') |
|
| 43 | + ->with('validator') |
|
| 44 | + ->compose($builder); |
|
| 45 | 45 | |
| 46 | - if( $form->input()->is_populated() ) { |
|
| 47 | - wfv_validate( $form ); |
|
| 46 | + if ($form->input()->is_populated()) { |
|
| 47 | + wfv_validate($form); |
|
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | 50 | |
@@ -56,8 +56,8 @@ discard block |
||
| 56 | 56 | * @param FormComposite $form |
| 57 | 57 | * @return bool |
| 58 | 58 | */ |
| 59 | -function wfv_validate( FormComposite $form ) { |
|
| 60 | - $factory = ( new ValidatorFactory() ) |
|
| 61 | - ->add( $form->rules()->unique() ); |
|
| 62 | - return $form->validate( $factory )->is_valid(); |
|
| 59 | +function wfv_validate(FormComposite $form) { |
|
| 60 | + $factory = (new ValidatorFactory()) |
|
| 61 | + ->add($form->rules()->unique()); |
|
| 62 | + return $form->validate($factory)->is_valid(); |
|
| 63 | 63 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Artisan\FormArtisan; |
| 6 | 6 | use WFV\Contract\ValidateInterface; |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * @param ArtisanInterface $builder |
| 49 | 49 | * @param string $action |
| 50 | 50 | */ |
| 51 | - public function __construct( FormArtisan $builder, $action ) { |
|
| 51 | + public function __construct(FormArtisan $builder, $action) { |
|
| 52 | 52 | $this->alias = $action; |
| 53 | 53 | $this->collection = $builder->collection; |
| 54 | 54 | $this->validator = $builder->validator; |
@@ -63,8 +63,8 @@ discard block |
||
| 63 | 63 | * @param string $value Value to compare against. |
| 64 | 64 | * @return string|null |
| 65 | 65 | */ |
| 66 | - public function checked_if( $field = null, $value = null ) { |
|
| 67 | - return $this->string_or_null( 'checked', $field, $value ); |
|
| 66 | + public function checked_if($field = null, $value = null) { |
|
| 67 | + return $this->string_or_null('checked', $field, $value); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -78,8 +78,8 @@ discard block |
||
| 78 | 78 | * @param callable (optional) $callback |
| 79 | 79 | * @return string |
| 80 | 80 | */ |
| 81 | - public function display( $field = null, callable $callback = null ) { |
|
| 82 | - echo $input = $this->utilize('input')->escape( $field ); |
|
| 81 | + public function display($field = null, callable $callback = null) { |
|
| 82 | + echo $input = $this->utilize('input')->escape($field); |
|
| 83 | 83 | return $input; |
| 84 | 84 | } |
| 85 | 85 | |
@@ -117,10 +117,10 @@ discard block |
||
| 117 | 117 | */ |
| 118 | 118 | public function is_valid() { |
| 119 | 119 | $is_valid = $this->validator->is_valid(); |
| 120 | - if( false === $is_valid ) { |
|
| 121 | - $this->utilize('errors')->set_errors( $this->validator->errors() ); |
|
| 120 | + if (false === $is_valid) { |
|
| 121 | + $this->utilize('errors')->set_errors($this->validator->errors()); |
|
| 122 | 122 | } |
| 123 | - $this->trigger_post_validate_action( $is_valid ); |
|
| 123 | + $this->trigger_post_validate_action($is_valid); |
|
| 124 | 124 | return $is_valid; |
| 125 | 125 | } |
| 126 | 126 | |
@@ -144,8 +144,8 @@ discard block |
||
| 144 | 144 | * @param string $value Value to compare against. |
| 145 | 145 | * @return string|null |
| 146 | 146 | */ |
| 147 | - public function selected_if( $field = null, $value = null ) { |
|
| 148 | - return $this->string_or_null( 'selected', $field, $value ); |
|
| 147 | + public function selected_if($field = null, $value = null) { |
|
| 148 | + return $this->string_or_null('selected', $field, $value); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | /** |
@@ -157,9 +157,9 @@ discard block |
||
| 157 | 157 | */ |
| 158 | 158 | public function token_fields() { |
| 159 | 159 | // TODO - Move markup into something - perhaps a renderable interface? |
| 160 | - $token_name = $this->alias . '_token'; |
|
| 161 | - echo $nonce_field = wp_nonce_field( $this->alias, $token_name, false, false ); |
|
| 162 | - echo $action_field = '<input type="hidden" name="action" value="'. $this->alias .'">'; |
|
| 160 | + $token_name = $this->alias.'_token'; |
|
| 161 | + echo $nonce_field = wp_nonce_field($this->alias, $token_name, false, false); |
|
| 162 | + echo $action_field = '<input type="hidden" name="action" value="'.$this->alias.'">'; |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
@@ -170,17 +170,17 @@ discard block |
||
| 170 | 170 | * @param ValidatorFactory $factory |
| 171 | 171 | * @return self |
| 172 | 172 | */ |
| 173 | - public function validate( ValidatorFactory $factory ) { |
|
| 173 | + public function validate(ValidatorFactory $factory) { |
|
| 174 | 174 | $rule_collection = $this->utilize('rules'); |
| 175 | - $rules = $rule_collection->get_array( true ); |
|
| 175 | + $rules = $rule_collection->get_array(true); |
|
| 176 | 176 | |
| 177 | - foreach( $rules as $field => $ruleset ) { |
|
| 178 | - $input = $this->field_value( $field ); |
|
| 179 | - $optional = $rule_collection->is_optional( $field ); |
|
| 177 | + foreach ($rules as $field => $ruleset) { |
|
| 178 | + $input = $this->field_value($field); |
|
| 179 | + $optional = $rule_collection->is_optional($field); |
|
| 180 | 180 | |
| 181 | - foreach( $ruleset as $index => $rule ) { |
|
| 182 | - $params = $rule_collection->get_params( $field, $index ); |
|
| 183 | - $this->validator->validate( $factory->get( $rule ), $field, $input, $optional, $params ); |
|
| 181 | + foreach ($ruleset as $index => $rule) { |
|
| 182 | + $params = $rule_collection->get_params($field, $index); |
|
| 183 | + $this->validator->validate($factory->get($rule), $field, $input, $optional, $params); |
|
| 184 | 184 | } |
| 185 | 185 | } |
| 186 | 186 | return $this; |
@@ -196,11 +196,11 @@ discard block |
||
| 196 | 196 | * @param string $field |
| 197 | 197 | * @return string|array|null |
| 198 | 198 | */ |
| 199 | - protected function field_value( $field ) { |
|
| 199 | + protected function field_value($field) { |
|
| 200 | 200 | $input = $this->utilize('input'); |
| 201 | - if( $input->has( $field ) ) { |
|
| 201 | + if ($input->has($field)) { |
|
| 202 | 202 | $input = $input->get_array(); |
| 203 | - return $input[ $field ]; |
|
| 203 | + return $input[$field]; |
|
| 204 | 204 | } |
| 205 | 205 | return null; |
| 206 | 206 | } |
@@ -216,9 +216,9 @@ discard block |
||
| 216 | 216 | * @param string (optional) $value |
| 217 | 217 | * @return string|null |
| 218 | 218 | */ |
| 219 | - protected function string_or_null( $response, $field = null, $value = null ) { |
|
| 219 | + protected function string_or_null($response, $field = null, $value = null) { |
|
| 220 | 220 | $input = $this->utilize('input'); |
| 221 | - return ( $input->contains( $field, $value ) ) ? $response : null; |
|
| 221 | + return ($input->contains($field, $value)) ? $response : null; |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
@@ -229,9 +229,9 @@ discard block |
||
| 229 | 229 | * |
| 230 | 230 | * @param bool $is_valid |
| 231 | 231 | */ |
| 232 | - protected function trigger_post_validate_action( $is_valid = false ) { |
|
| 233 | - $action = ( true === $is_valid ) ? $this->alias : $this->alias .'_fail'; |
|
| 234 | - do_action( $action, $this ); |
|
| 232 | + protected function trigger_post_validate_action($is_valid = false) { |
|
| 233 | + $action = (true === $is_valid) ? $this->alias : $this->alias.'_fail'; |
|
| 234 | + do_action($action, $this); |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | /** |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | * |
| 243 | 243 | * @param string $component Key indentifier. |
| 244 | 244 | */ |
| 245 | - protected function utilize( $component ) { |
|
| 246 | - return $this->collection[ $component ]; |
|
| 245 | + protected function utilize($component) { |
|
| 246 | + return $this->collection[$component]; |
|
| 247 | 247 | } |
| 248 | 248 | } |