← All changes
|
modules/atomic-widgets/parsers/props-parser.php
+33
-15
4.2.0-beta1
→
3.28.0-dev2
View file →
| @@ -2,9 +2,8 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\AtomicWidgets\Parsers; |
| 4 | 4 | |
| 5 | 5 | use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type; |
| 6 | -use Elementor\Core\Utils\Api\Parse_Result; | |
| 7 | 6 | |
| 8 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | 8 | exit; // Exit if accessed directly. |
| 10 | 9 | } |
| @@ -11,8 +10,9 @@ | ||
| 11 | 10 | |
| 12 | 11 | class Props_Parser { |
| 13 | 12 | |
| 14 | 13 | private array $schema; |
| 14 | + private array $errors_bag = []; | |
| 15 | 15 | |
| 16 | 16 | public function __construct( array $schema ) { |
| 17 | 17 | $this->schema = $schema; |
| 18 | 18 | } |
| @@ -24,12 +24,16 @@ | ||
| 24 | 24 | /** |
| 25 | 25 | * @param array $props |
| 26 | 26 | * The key of each item represents the prop name (should match the schema), |
| 27 | 27 | * and the value is the prop value to validate |
| 28 | + * | |
| 29 | + * @return array{ | |
| 30 | + * 0: bool, | |
| 31 | + * 1: array<string, mixed>, | |
| 32 | + * 2: array<string> | |
| 33 | + * } | |
| 28 | 34 | */ |
| 29 | - public function validate( array $props ): Parse_Result { | |
| 30 | - $result = Parse_Result::make(); | |
| 31 | - | |
| 35 | + public function validate( array $props ): array { | |
| 32 | 36 | $validated = []; |
| 33 | 37 | |
| 34 | 38 | foreach ( $this->schema as $key => $prop_type ) { |
| 35 | 39 | if ( ! ( $prop_type instanceof Prop_Type ) ) { |
| @@ -40,9 +44,9 @@ | ||
| 40 | 44 | |
| 41 | 45 | $is_valid = $prop_type->validate( $value ?? $prop_type->get_default() ); |
| 42 | 46 | |
| 43 | 47 | if ( ! $is_valid ) { |
| 44 | - $result->errors()->add( $key, 'invalid_value' ); | |
| 48 | + $this->errors_bag[] = $key; | |
| 45 | 49 | |
| 46 | 50 | continue; |
| 47 | 51 | } |
| 48 | 52 | |
| @@ -50,9 +54,15 @@ | ||
| 50 | 54 | $validated[ $key ] = $value; |
| 51 | 55 | } |
| 52 | 56 | } |
| 53 | 57 | |
| 54 | - return $result->wrap( $validated ); | |
| 58 | + $is_valid = empty( $this->errors_bag ); | |
| 59 | + | |
| 60 | + return [ | |
| 61 | + $is_valid, | |
| 62 | + $validated, | |
| 63 | + $this->errors_bag, | |
| 64 | + ]; | |
| 55 | 65 | } |
| 56 | 66 | |
| 57 | 67 | /** |
| 58 | 68 | * @param array $props |
| @@ -57,10 +67,12 @@ | ||
| 57 | 67 | /** |
| 58 | 68 | * @param array $props |
| 59 | 69 | * The key of each item represents the prop name (should match the schema), |
| 60 | 70 | * and the value is the prop value to sanitize |
| 71 | + * | |
| 72 | + * @return array<string, mixed> | |
| 61 | 73 | */ |
| 62 | - public function sanitize( array $props ): Parse_Result { | |
| 74 | + public function sanitize( array $props ): array { | |
| 63 | 75 | $sanitized = []; |
| 64 | 76 | |
| 65 | 77 | foreach ( $this->schema as $key => $prop_type ) { |
| 66 | 78 | if ( ! isset( $props[ $key ] ) ) { |
| @@ -69,9 +81,9 @@ | ||
| 69 | 81 | |
| 70 | 82 | $sanitized[ $key ] = $prop_type->sanitize( $props[ $key ] ); |
| 71 | 83 | } |
| 72 | 84 | |
| 73 | - return Parse_Result::make()->wrap( $sanitized ); | |
| 85 | + return $sanitized; | |
| 74 | 86 | } |
| 75 | 87 | |
| 76 | 88 | /** |
| 77 | 89 | * @param array $props |
| @@ -76,15 +88,21 @@ | ||
| 76 | 88 | /** |
| 77 | 89 | * @param array $props |
| 78 | 90 | * The key of each item represents the prop name (should match the schema), |
| 79 | 91 | * and the value is the prop value to parse |
| 92 | + * | |
| 93 | + * @return array{ | |
| 94 | + * 0: bool, | |
| 95 | + * 1: array<string, mixed>, | |
| 96 | + * 2: array<string> | |
| 97 | + * } | |
| 80 | 98 | */ |
| 81 | - public function parse( array $props ): Parse_Result { | |
| 82 | - $validate_result = $this->validate( $props ); | |
| 99 | + public function parse( array $props ): array { | |
| 100 | + [ $is_valid, $validated, $errors_bag ] = $this->validate( $props ); | |
| 83 | 101 | |
| 84 | - $sanitize_result = $this->sanitize( $validate_result->unwrap() ); | |
| 85 | - | |
| 86 | - $sanitize_result->errors()->merge( $validate_result->errors() ); | |
| 87 | - | |
| 88 | - return $sanitize_result; | |
| 102 | + return [ | |
| 103 | + $is_valid, | |
| 104 | + $this->sanitize( $validated ), | |
| 105 | + $errors_bag, | |
| 106 | + ]; | |
| 89 | 107 | } |
| 90 | 108 | } |