1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
<?php
namespace Elementor\Modules\AtomicWidgets\Parsers;
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. }
class Style_Parser { const VALID_STATES = [ 'hover', 'active', 'focus', null, ];
private array $schema; private array $errors_bag = []; private $should_validate_id = true;
public function __construct( array $schema ) { $this->schema = $schema; }
public static function make( array $schema ): self { return new static( $schema ); }
public function without_id() { $this->should_validate_id = false;
return $this; }
/** * @param array $style * the style object to validate * * @return array{ * 0: bool, * 1: array<string, mixed>, * 2: array<string> * } */ public function validate( array $style ): array { $validated_style = $style;
if ( $this->should_validate_id && ( ! isset( $style['id'] ) || ! is_string( $style['id'] ) ) ) { $this->errors_bag[] = 'id'; }
if ( ! isset( $style['variants'] ) || ! is_array( $style['variants'] ) ) { $this->errors_bag[] = 'variants'; unset( $validated_style['variants'] );
return [ false, $validated_style, $this->errors_bag, ]; }
$props_parser = Props_Parser::make( $this->schema );
foreach ( $style['variants'] as $variant_index => $variant ) { if ( ! isset( $variant['meta'] ) ) { $this->errors_bag[] = 'meta'; continue; }
$is_variant_meta_valid = $this->validate_meta( $variant['meta'] );
if ( $is_variant_meta_valid ) { [, $validated_props, $variant_errors] = $props_parser->validate( $variant['props'] ); $this->errors_bag = array_merge( $this->errors_bag, $variant_errors );
$validated_style['variants'][ $variant_index ]['props'] = $validated_props; } else { unset( $validated_style['variants'][ $variant_index ] ); } }
$is_valid = empty( $this->errors_bag );
return [ $is_valid, $validated_style, $this->errors_bag, ]; }
public function validate_meta( $meta ): bool { if ( ! is_array( $meta ) ) { $this->errors_bag[] = 'meta'; return false; }
if ( ! array_key_exists( 'state', $meta ) || ! in_array( $meta['state'], self::VALID_STATES, true ) ) { $this->errors_bag[] = 'meta'; return false; }
// TODO: Validate breakpoint based on the existing breakpoints in the system [EDS-528] if ( ! isset( $meta['breakpoint'] ) || ! is_string( $meta['breakpoint'] ) ) { $this->errors_bag[] = 'meta'; return false; }
return true; }
/** * @param array $style * the style object to sanitize * * @return array<string, mixed> */ public function sanitize( array $style ): array { $props_parser = Props_Parser::make( $this->schema );
if ( ! empty( $style['variants'] ) ) { foreach ( $style['variants'] as $variant_index => $variant ) { $style['variants'][ $variant_index ]['props'] = $props_parser->sanitize( $variant['props'] ); } }
return $style; }
/** * @param array $style * the style object to parse * * @return array{ * 0: bool, * 1: array<string, mixed>, * 2: array<string> * } */ public function parse( array $style ): array { [ $is_valid, $validated, $errors_bag ] = $this->validate( $style );
return [ $is_valid, $this->sanitize( $validated ), $errors_bag, ]; } }
|