@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | * |
| 70 | 70 | * @since 0.10.0 |
| 71 | 71 | * |
| 72 | - * @return WFV\FormComposite |
|
| 72 | + * @return FormComposite |
|
| 73 | 73 | */ |
| 74 | 74 | public function actualize() { |
| 75 | 75 | return $this->form; |
@@ -80,8 +80,7 @@ discard block |
||
| 80 | 80 | * |
| 81 | 81 | * @since 0.10.0 |
| 82 | 82 | * |
| 83 | - * @param string $action |
|
| 84 | - * @return WFV\Artisan\FormArtisan |
|
| 83 | + * @return FormArtisan |
|
| 85 | 84 | */ |
| 86 | 85 | public function create() { |
| 87 | 86 | $this->form = new FormComposite( $this ); |
@@ -94,7 +93,7 @@ discard block |
||
| 94 | 93 | * |
| 95 | 94 | * @since 0.10.0 |
| 96 | 95 | * |
| 97 | - * @return WFV\Artisan\FormArtisan |
|
| 96 | + * @return FormArtisan |
|
| 98 | 97 | */ |
| 99 | 98 | public function errors() { |
| 100 | 99 | $this->collection['errors'] = new ErrorCollection( $this->labels() ); |
@@ -107,7 +106,7 @@ discard block |
||
| 107 | 106 | * |
| 108 | 107 | * @since 0.10.0 |
| 109 | 108 | * |
| 110 | - * @return WFV\Artisan\FormArtisan |
|
| 109 | + * @return FormArtisan |
|
| 111 | 110 | */ |
| 112 | 111 | public function input() { |
| 113 | 112 | $guard = new InspectionAgent( $this->action ); |
@@ -121,7 +120,7 @@ discard block |
||
| 121 | 120 | * |
| 122 | 121 | * @since 0.10.0 |
| 123 | 122 | * |
| 124 | - * @return WFV\Artisan\FormArtisan |
|
| 123 | + * @return FormArtisan |
|
| 125 | 124 | */ |
| 126 | 125 | public function rules() { |
| 127 | 126 | $this->collection['rules'] = new RuleCollection( $this->config ); |
@@ -7,7 +7,6 @@ |
||
| 7 | 7 | use WFV\Collection\ErrorCollection; |
| 8 | 8 | use WFV\Collection\InputCollection; |
| 9 | 9 | use WFV\Collection\RuleCollection; |
| 10 | - |
|
| 11 | 10 | use WFV\FormComposite; |
| 12 | 11 | |
| 13 | 12 | /** |
@@ -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; |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | * @param array $config |
| 60 | 60 | * @param string $action |
| 61 | 61 | */ |
| 62 | - public function __construct( array $config, $action ) { |
|
| 62 | + public function __construct(array $config, $action) { |
|
| 63 | 63 | $this->config = $config; |
| 64 | 64 | $this->action = $action; |
| 65 | 65 | } |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | * @return WFV\Artisan\FormArtisan |
| 85 | 85 | */ |
| 86 | 86 | public function create() { |
| 87 | - $this->form = new FormComposite( $this ); |
|
| 87 | + $this->form = new FormComposite($this); |
|
| 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 | * @return WFV\Artisan\FormArtisan |
| 111 | 111 | */ |
| 112 | 112 | public function input() { |
| 113 | - $guard = new InspectionAgent( $this->action ); |
|
| 114 | - $this->collection['input'] = new InputCollection( $guard ); |
|
| 113 | + $guard = new InspectionAgent($this->action); |
|
| 114 | + $this->collection['input'] = new InputCollection($guard); |
|
| 115 | 115 | return $this; |
| 116 | 116 | } |
| 117 | 117 | |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | * @return WFV\Artisan\FormArtisan |
| 125 | 125 | */ |
| 126 | 126 | public function rules() { |
| 127 | - $this->collection['rules'] = new RuleCollection( $this->config ); |
|
| 127 | + $this->collection['rules'] = new RuleCollection($this->config); |
|
| 128 | 128 | return $this; |
| 129 | 129 | } |
| 130 | 130 | |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | * @return array |
| 139 | 139 | */ |
| 140 | 140 | protected function labels() { |
| 141 | - return array_map( function( $item ) { |
|
| 141 | + return array_map(function($item) { |
|
| 142 | 142 | return $item['label']; |
| 143 | 143 | }, $this->config); |
| 144 | 144 | } |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | * |
| 37 | 37 | * @since 0.10.0 |
| 38 | 38 | * |
| 39 | - * @param ArtisanInterface $builder |
|
| 39 | + * @param FormArtisan $builder |
|
| 40 | 40 | */ |
| 41 | 41 | public function __construct( FormArtisan $builder ) { |
| 42 | 42 | $this->alias = $builder->action; |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | * |
| 90 | 90 | * @since 0.10.0 |
| 91 | 91 | * |
| 92 | - * @return WFV\Collection\InputCollection |
|
| 92 | + * @return Collection\InputCollection |
|
| 93 | 93 | */ |
| 94 | 94 | public function input() { |
| 95 | 95 | return $this->utilize('input'); |
@@ -153,6 +153,8 @@ discard block |
||
| 153 | 153 | * @param string $response |
| 154 | 154 | * @param string (optional) $field |
| 155 | 155 | * @param string (optional) $value |
| 156 | + * @param string $field |
|
| 157 | + * @param string $value |
|
| 156 | 158 | * @return string|null |
| 157 | 159 | */ |
| 158 | 160 | protected function string_or_null( $response, $field = null, $value = null ) { |
@@ -3,8 +3,6 @@ |
||
| 3 | 3 | defined( 'ABSPATH' ) || die(); |
| 4 | 4 | |
| 5 | 5 | use WFV\Artisan\FormArtisan; |
| 6 | -use WFV\Contract\ValidateInterface; |
|
| 7 | -use WFV\RuleFactory; |
|
| 8 | 6 | |
| 9 | 7 | /** |
| 10 | 8 | * Form Composition |
@@ -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; |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @param ArtisanInterface $builder |
| 40 | 40 | */ |
| 41 | - public function __construct( FormArtisan $builder ) { |
|
| 41 | + public function __construct(FormArtisan $builder) { |
|
| 42 | 42 | $this->alias = $builder->action; |
| 43 | 43 | $this->collection = $builder->collection; |
| 44 | 44 | } |
@@ -52,8 +52,8 @@ discard block |
||
| 52 | 52 | * @param string $value Value to compare against. |
| 53 | 53 | * @return string|null |
| 54 | 54 | */ |
| 55 | - public function checked_if( $field = null, $value = null ) { |
|
| 56 | - return $this->string_or_null( 'checked', $field, $value ); |
|
| 55 | + public function checked_if($field = null, $value = null) { |
|
| 56 | + return $this->string_or_null('checked', $field, $value); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
@@ -67,8 +67,8 @@ discard block |
||
| 67 | 67 | * @param callable (optional) $callback |
| 68 | 68 | * @return string |
| 69 | 69 | */ |
| 70 | - public function display( $field = null, callable $callback = null ) { |
|
| 71 | - echo $input = $this->utilize('input')->escape( $field ); |
|
| 70 | + public function display($field = null, callable $callback = null) { |
|
| 71 | + echo $input = $this->utilize('input')->escape($field); |
|
| 72 | 72 | return $input; |
| 73 | 73 | } |
| 74 | 74 | |
@@ -126,8 +126,8 @@ discard block |
||
| 126 | 126 | * @param string $value Value to compare against. |
| 127 | 127 | * @return string|null |
| 128 | 128 | */ |
| 129 | - public function selected_if( $field = null, $value = null ) { |
|
| 130 | - return $this->string_or_null( 'selected', $field, $value ); |
|
| 129 | + public function selected_if($field = null, $value = null) { |
|
| 130 | + return $this->string_or_null('selected', $field, $value); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | /** |
@@ -139,9 +139,9 @@ discard block |
||
| 139 | 139 | */ |
| 140 | 140 | public function token_fields() { |
| 141 | 141 | // TODO - Move markup into something - perhaps a renderable interface? |
| 142 | - $token_name = $this->alias . '_token'; |
|
| 143 | - echo $nonce_field = wp_nonce_field( $this->alias, $token_name, false, false ); |
|
| 144 | - echo $action_field = '<input type="hidden" name="action" value="'. $this->alias .'">'; |
|
| 142 | + $token_name = $this->alias.'_token'; |
|
| 143 | + echo $nonce_field = wp_nonce_field($this->alias, $token_name, false, false); |
|
| 144 | + echo $action_field = '<input type="hidden" name="action" value="'.$this->alias.'">'; |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
@@ -155,9 +155,9 @@ discard block |
||
| 155 | 155 | * @param string (optional) $value |
| 156 | 156 | * @return string|null |
| 157 | 157 | */ |
| 158 | - protected function string_or_null( $response, $field = null, $value = null ) { |
|
| 158 | + protected function string_or_null($response, $field = null, $value = null) { |
|
| 159 | 159 | $input = $this->utilize('input'); |
| 160 | - return ( $input->contains( $field, $value ) ) ? $response : null; |
|
| 160 | + return ($input->contains($field, $value)) ? $response : null; |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | /** |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | * |
| 169 | 169 | * @param string $component Key indentifier. |
| 170 | 170 | */ |
| 171 | - protected function utilize( $component ) { |
|
| 172 | - return $this->collection[ $component ]; |
|
| 171 | + protected function utilize($component) { |
|
| 172 | + return $this->collection[$component]; |
|
| 173 | 173 | } |
| 174 | 174 | } |
@@ -17,7 +17,6 @@ |
||
| 17 | 17 | |
| 18 | 18 | require_once WFV_VALIDATE__PLUGIN_DIR . '/vendor/autoload.php'; |
| 19 | 19 | |
| 20 | -use WFV\FormComposite; |
|
| 21 | 20 | use WFV\RuleFactory; |
| 22 | 21 | use WFV\Validator; |
| 23 | 22 | use WFV\Artisan\Director; |
@@ -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/wfv-validation/blob/master/LICENSE |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | -define( 'WFV_VALIDATE_VERSION', '0.11.3' ); |
|
| 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.3'); |
|
| 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\RuleFactory; |
@@ -33,17 +33,17 @@ 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 | - $messages = new MessageCollection( $form ); |
|
| 36 | +function wfv_create($action, array &$form, $trim = true) { |
|
| 37 | + $messages = new MessageCollection($form); |
|
| 38 | 38 | |
| 39 | - $builder = new FormArtisan( $form, $action ); |
|
| 40 | - $form = ( new Director() ) |
|
| 41 | - ->with( 'input' ) |
|
| 42 | - ->with( 'rules' ) |
|
| 43 | - ->with( 'errors' ) |
|
| 44 | - ->compose( $builder ); |
|
| 39 | + $builder = new FormArtisan($form, $action); |
|
| 40 | + $form = (new Director()) |
|
| 41 | + ->with('input') |
|
| 42 | + ->with('rules') |
|
| 43 | + ->with('errors') |
|
| 44 | + ->compose($builder); |
|
| 45 | 45 | |
| 46 | - if( $form->input()->is_populated() ) { |
|
| 47 | - ( new Validator( new RuleFactory(), $messages ) )->validate( $form ); |
|
| 46 | + if ($form->input()->is_populated()) { |
|
| 47 | + (new Validator(new RuleFactory(), $messages))->validate($form); |
|
| 48 | 48 | } |
| 49 | 49 | } |
@@ -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 $form |
| 20 | 20 | */ |
| 21 | - public function __construct( array $form ) { |
|
| 22 | - $this->data = $this->parse_rules( $form ); |
|
| 21 | + public function __construct(array $form) { |
|
| 22 | + $this->data = $this->parse_rules($form); |
|
| 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,9 +113,9 @@ discard block |
||
| 113 | 113 | * @param array $form |
| 114 | 114 | * @return array |
| 115 | 115 | */ |
| 116 | - protected function extract_rules( array $form ) { |
|
| 117 | - foreach( $form as $field => $options ) { |
|
| 118 | - $rules[ $field ] = $options['rules']; |
|
| 116 | + protected function extract_rules(array $form) { |
|
| 117 | + foreach ($form as $field => $options) { |
|
| 118 | + $rules[$field] = $options['rules']; |
|
| 119 | 119 | } |
| 120 | 120 | return $rules; |
| 121 | 121 | } |
@@ -129,11 +129,11 @@ discard block |
||
| 129 | 129 | * @param array $array |
| 130 | 130 | * @return array |
| 131 | 131 | */ |
| 132 | - protected function flatten( array $array ) { |
|
| 132 | + protected function flatten(array $array) { |
|
| 133 | 133 | $flat = array(); |
| 134 | - foreach( $array as $rule ) { |
|
| 135 | - if( is_array( $rule ) ){ |
|
| 136 | - $flat = array_merge( $flat, $this->flatten( $rule ) ); |
|
| 134 | + foreach ($array as $rule) { |
|
| 135 | + if (is_array($rule)) { |
|
| 136 | + $flat = array_merge($flat, $this->flatten($rule)); |
|
| 137 | 137 | } else { |
| 138 | 138 | $flat[] = $rule; |
| 139 | 139 | } |
@@ -151,8 +151,8 @@ discard block |
||
| 151 | 151 | * @param int $index |
| 152 | 152 | * @return bool |
| 153 | 153 | */ |
| 154 | - protected function has_params( $field, $index ) { |
|
| 155 | - return is_array( $this->data[ $field ][ $index ] ); |
|
| 154 | + protected function has_params($field, $index) { |
|
| 155 | + return is_array($this->data[$field][$index]); |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | /** |
@@ -165,22 +165,22 @@ discard block |
||
| 165 | 165 | * @param array $form |
| 166 | 166 | * @return array |
| 167 | 167 | */ |
| 168 | - protected function parse_rules( array $form ) { |
|
| 168 | + protected function parse_rules(array $form) { |
|
| 169 | 169 | // WIP - works, but confusing - simplify or breakdown into small methods |
| 170 | - $rules = $this->extract_rules( $form ); |
|
| 170 | + $rules = $this->extract_rules($form); |
|
| 171 | 171 | |
| 172 | 172 | $parsed = array(); |
| 173 | - $this->split_rules( $rules ); |
|
| 174 | - foreach( $rules as $field => $ruleset ) { |
|
| 175 | - $parsed[ $field ] = array_map( function( $rule ) { |
|
| 176 | - if ( $this->string_has_params( $rule ) ) { |
|
| 173 | + $this->split_rules($rules); |
|
| 174 | + foreach ($rules as $field => $ruleset) { |
|
| 175 | + $parsed[$field] = array_map(function($rule) { |
|
| 176 | + if ($this->string_has_params($rule)) { |
|
| 177 | 177 | return array( |
| 178 | - 'rule' => $this->extract_name( $rule ), |
|
| 179 | - 'params' => explode( ',', $this->extract_params( $rule ) ) |
|
| 178 | + 'rule' => $this->extract_name($rule), |
|
| 179 | + 'params' => explode(',', $this->extract_params($rule)) |
|
| 180 | 180 | ); |
| 181 | 181 | } |
| 182 | 182 | return $rule; |
| 183 | - }, $ruleset ); |
|
| 183 | + }, $ruleset); |
|
| 184 | 184 | } |
| 185 | 185 | return $parsed; |
| 186 | 186 | } |
@@ -195,14 +195,14 @@ discard block |
||
| 195 | 195 | * @return array |
| 196 | 196 | */ |
| 197 | 197 | protected function remove_params() { |
| 198 | - return array_map( function( $item ) { |
|
| 199 | - foreach( $item as $index => $rule ) { |
|
| 200 | - if( $rule !== 'optional' ) { |
|
| 201 | - $rules[ $index ] = ( is_string( $rule ) ) ? $rule : $rule['rule']; |
|
| 198 | + return array_map(function($item) { |
|
| 199 | + foreach ($item as $index => $rule) { |
|
| 200 | + if ($rule !== 'optional') { |
|
| 201 | + $rules[$index] = (is_string($rule)) ? $rule : $rule['rule']; |
|
| 202 | 202 | } |
| 203 | 203 | } |
| 204 | 204 | return $rules; |
| 205 | - }, $this->data ); |
|
| 205 | + }, $this->data); |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | /** |
@@ -213,11 +213,11 @@ discard block |
||
| 213 | 213 | * |
| 214 | 214 | * @param array $rules |
| 215 | 215 | */ |
| 216 | - protected function split_rules( array &$rules ) { |
|
| 216 | + protected function split_rules(array &$rules) { |
|
| 217 | 217 | // perhaps the $rules array structure should be validated here?... |
| 218 | - $rules = array_map( function( $item ) { |
|
| 219 | - return explode( '|', $item ); |
|
| 220 | - }, $rules ); |
|
| 218 | + $rules = array_map(function($item) { |
|
| 219 | + return explode('|', $item); |
|
| 220 | + }, $rules); |
|
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | /** |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | * @param string $rule |
| 230 | 230 | * @return bool |
| 231 | 231 | */ |
| 232 | - protected function string_has_params( $rule ) { |
|
| 233 | - return strpos( $rule, ':' ); |
|
| 232 | + protected function string_has_params($rule) { |
|
| 233 | + return strpos($rule, ':'); |
|
| 234 | 234 | } |
| 235 | 235 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Contract; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | /** |
| 6 | 6 | * |
@@ -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\FormComposite; |
| 6 | 6 | use WFV\Collection\InputCollection; |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | * |
| 50 | 50 | * @param RuleFactory $factory |
| 51 | 51 | */ |
| 52 | - public function __construct( RuleFactory $factory, MessageCollection $messages ) { |
|
| 52 | + public function __construct(RuleFactory $factory, MessageCollection $messages) { |
|
| 53 | 53 | $this->factory = $factory; |
| 54 | 54 | $this->messages = $messages; |
| 55 | 55 | } |
@@ -62,20 +62,20 @@ discard block |
||
| 62 | 62 | * @param FormComposite $form |
| 63 | 63 | * @return bool |
| 64 | 64 | */ |
| 65 | - public function validate( FormComposite $form ) { |
|
| 65 | + public function validate(FormComposite $form) { |
|
| 66 | 66 | $rule_collection = $form->rules(); |
| 67 | - $rules = $rule_collection->get_array( true ); |
|
| 67 | + $rules = $rule_collection->get_array(true); |
|
| 68 | 68 | |
| 69 | - foreach( $rules as $field => $ruleset ) { |
|
| 70 | - $input = $this->field_value( $form->input(), $field ); |
|
| 71 | - $optional = $rule_collection->is_optional( $field ); |
|
| 69 | + foreach ($rules as $field => $ruleset) { |
|
| 70 | + $input = $this->field_value($form->input(), $field); |
|
| 71 | + $optional = $rule_collection->is_optional($field); |
|
| 72 | 72 | |
| 73 | - foreach( $ruleset as $index => $rule ) { |
|
| 74 | - $params = $rule_collection->get_params( $field, $index ); |
|
| 75 | - $this->is_valid( $this->factory->get( $rule ), $field, $input, $optional, $params ); |
|
| 73 | + foreach ($ruleset as $index => $rule) { |
|
| 74 | + $params = $rule_collection->get_params($field, $index); |
|
| 75 | + $this->is_valid($this->factory->get($rule), $field, $input, $optional, $params); |
|
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | - return $this->result( $form ); |
|
| 78 | + return $this->result($form); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | /** |
@@ -87,11 +87,11 @@ discard block |
||
| 87 | 87 | * @param string $field |
| 88 | 88 | * @param array $template |
| 89 | 89 | */ |
| 90 | - protected function add_error( $field, array $template ) { |
|
| 91 | - $message = ( $this->messages->has( $field ) ) |
|
| 92 | - ? $this->messages->get_msg( $field, $template['name'] ) |
|
| 90 | + protected function add_error($field, array $template) { |
|
| 91 | + $message = ($this->messages->has($field)) |
|
| 92 | + ? $this->messages->get_msg($field, $template['name']) |
|
| 93 | 93 | : $template['message']; |
| 94 | - $this->errors[ $field ][ $template['name'] ] = $message; |
|
| 94 | + $this->errors[$field][$template['name']] = $message; |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -118,10 +118,10 @@ discard block |
||
| 118 | 118 | * @param string $field |
| 119 | 119 | * @return string|array|null |
| 120 | 120 | */ |
| 121 | - protected function field_value( InputCollection $input, $field ) { |
|
| 122 | - if( $input->has( $field ) ) { |
|
| 121 | + protected function field_value(InputCollection $input, $field) { |
|
| 122 | + if ($input->has($field)) { |
|
| 123 | 123 | $input = $input->get_array(); |
| 124 | - return $input[ $field ]; |
|
| 124 | + return $input[$field]; |
|
| 125 | 125 | } |
| 126 | 126 | return null; |
| 127 | 127 | } |
@@ -137,11 +137,11 @@ discard block |
||
| 137 | 137 | * @param bool $optional |
| 138 | 138 | * @param array (optional) $params |
| 139 | 139 | */ |
| 140 | - protected function is_valid( ValidateInterface $rule, $field, $value, $optional, $params = false ) { |
|
| 141 | - $params[] = ( $params ) ? $field : false; |
|
| 142 | - $valid = $rule->validate( $value, $optional, $params ); |
|
| 143 | - if( !$valid ){ |
|
| 144 | - $this->add_error( $field, $rule->template() ); |
|
| 140 | + protected function is_valid(ValidateInterface $rule, $field, $value, $optional, $params = false) { |
|
| 141 | + $params[] = ($params) ? $field : false; |
|
| 142 | + $valid = $rule->validate($value, $optional, $params); |
|
| 143 | + if (!$valid) { |
|
| 144 | + $this->add_error($field, $rule->template()); |
|
| 145 | 145 | } |
| 146 | 146 | } |
| 147 | 147 | |
@@ -154,12 +154,12 @@ discard block |
||
| 154 | 154 | * @param FormComposite $form |
| 155 | 155 | * @return bool |
| 156 | 156 | */ |
| 157 | - protected function result( FormComposite $form ) { |
|
| 158 | - $valid = empty( $this->errors ); |
|
| 159 | - if( !$valid ) { |
|
| 160 | - $form->errors()->set_errors( $this->errors ); |
|
| 157 | + protected function result(FormComposite $form) { |
|
| 158 | + $valid = empty($this->errors); |
|
| 159 | + if (!$valid) { |
|
| 160 | + $form->errors()->set_errors($this->errors); |
|
| 161 | 161 | } |
| 162 | - $this->trigger_post_validate_action( $form, $valid ); |
|
| 162 | + $this->trigger_post_validate_action($form, $valid); |
|
| 163 | 163 | return $valid; |
| 164 | 164 | } |
| 165 | 165 | |
@@ -173,8 +173,8 @@ discard block |
||
| 173 | 173 | * @param FormComposite $form |
| 174 | 174 | * @param bool $is_valid |
| 175 | 175 | */ |
| 176 | - protected function trigger_post_validate_action( FormComposite $form, $is_valid = false ) { |
|
| 177 | - $action = ( true === $is_valid ) ? $form->name() : $form->name() .'_fail'; |
|
| 178 | - do_action( $action, $form ); |
|
| 176 | + protected function trigger_post_validate_action(FormComposite $form, $is_valid = false) { |
|
| 177 | + $action = (true === $is_valid) ? $form->name() : $form->name().'_fail'; |
|
| 178 | + do_action($action, $form); |
|
| 179 | 179 | } |
| 180 | 180 | } |
@@ -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\Contract\ArtisanInterface; |
| 6 | 6 | |
@@ -29,8 +29,8 @@ discard block |
||
| 29 | 29 | * @param ArtisanInterface $builder |
| 30 | 30 | * @return object |
| 31 | 31 | */ |
| 32 | - public function compose( ArtisanInterface $builder ) { |
|
| 33 | - $this->amalgamate( $this->component, $builder ); |
|
| 32 | + public function compose(ArtisanInterface $builder) { |
|
| 33 | + $this->amalgamate($this->component, $builder); |
|
| 34 | 34 | return $builder |
| 35 | 35 | ->create() |
| 36 | 36 | ->actualize(); |
@@ -45,8 +45,8 @@ discard block |
||
| 45 | 45 | * @param string|array|object (optional) $params |
| 46 | 46 | * @return self |
| 47 | 47 | */ |
| 48 | - public function with( $name, $params = null ) { |
|
| 49 | - $this->component[ $name ] = $params; |
|
| 48 | + public function with($name, $params = null) { |
|
| 49 | + $this->component[$name] = $params; |
|
| 50 | 50 | return $this; |
| 51 | 51 | } |
| 52 | 52 | |
@@ -59,9 +59,9 @@ discard block |
||
| 59 | 59 | * @param array $components |
| 60 | 60 | * @param ArtisanInterface $builder |
| 61 | 61 | */ |
| 62 | - private function amalgamate( array $components, ArtisanInterface &$builder ) { |
|
| 63 | - foreach( $components as $component => $params ) { |
|
| 64 | - $builder->$component( $params ); |
|
| 62 | + private function amalgamate(array $components, ArtisanInterface&$builder) { |
|
| 63 | + foreach ($components as $component => $params) { |
|
| 64 | + $builder->$component($params); |
|
| 65 | 65 | } |
| 66 | 66 | } |
| 67 | 67 | } |