PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / modules / atomic-widgets / plain-resolvers / plain-values-resolver.php

plain-values-resolver.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/atomic-widgets/plain-resolvers/plain-values-resolver.php

202 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\AtomicWidgets\PlainResolvers;
4
5 use Elementor\Modules\AtomicWidgets\PropTypes\Base\Array_Prop_Type;
6 use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
7 use Elementor\Modules\AtomicWidgets\PropTypes\Base\Plain_Prop_Type;
8 use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
9 use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type;
10 use Elementor\Modules\AtomicWidgets\PropTypes\Union_Prop_Type;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 class Plain_Values_Resolver {
17 private Plain_Resolvers_Registry $registry;
18
19 public function __construct( Plain_Resolvers_Registry $registry ) {
20 $this->registry = $registry;
21 }
22
23 public function resolve( $plain_value, Prop_Type $prop_type ) {
24 if ( null === $plain_value ) {
25 return null;
26 }
27
28 if ( $this->registry->has( $prop_type::get_key() ) ) {
29 return $this->resolve_with_registered( $plain_value, $prop_type );
30 }
31
32 if ( $prop_type instanceof Union_Prop_Type ) {
33 return $this->resolve_union( $plain_value, $prop_type );
34 }
35
36 if ( $prop_type instanceof Object_Prop_Type ) {
37 return $this->resolve_object( $plain_value, $prop_type );
38 }
39
40 if ( $prop_type instanceof Array_Prop_Type ) {
41 return $this->resolve_array( $plain_value, $prop_type );
42 }
43
44 return $this->resolve_with_fallback( $plain_value, $prop_type );
45 }
46
47 private function resolve_union( $plain_value, Union_Prop_Type $prop_type ) {
48 if ( is_array( $plain_value ) && isset( $plain_value['$$type'] ) ) {
49 $variant = $prop_type->get_prop_type( $plain_value['$$type'] );
50
51 if ( null === $variant ) {
52 return null;
53 }
54
55 return $this->resolve( $plain_value['value'] ?? null, $variant );
56 }
57
58 $variants = $prop_type->get_prop_types();
59
60 if ( is_int( $plain_value ) || is_float( $plain_value ) ) {
61 $variants = $this->prioritize_numeric_variants( $variants );
62 }
63
64 foreach ( $variants as $variant ) {
65 $result = $this->resolve_variant( $plain_value, $variant );
66
67 if ( null !== $result ) {
68 return $result;
69 }
70 }
71
72 return null;
73 }
74
75 private function prioritize_numeric_variants( array $variants ): array {
76 $numeric_variants = [];
77 $other_variants = [];
78
79 foreach ( $variants as $variant ) {
80 if ( Number_Prop_Type::get_key() === $variant::get_key() ) {
81 $numeric_variants[] = $variant;
82 continue;
83 }
84
85 $other_variants[] = $variant;
86 }
87
88 return array_merge( $numeric_variants, $other_variants );
89 }
90
91 private function resolve_variant( $plain_value, Prop_Type $prop_type ) {
92 if ( $this->registry->has( $prop_type::get_key() ) ) {
93 return $this->resolve_with_registered( $plain_value, $prop_type );
94 }
95
96 if ( $prop_type instanceof Union_Prop_Type ) {
97 return $this->resolve_union( $plain_value, $prop_type );
98 }
99
100 if ( $prop_type instanceof Object_Prop_Type ) {
101 return $this->resolve_object( $plain_value, $prop_type );
102 }
103
104 if ( $prop_type instanceof Array_Prop_Type ) {
105 return $this->resolve_array( $plain_value, $prop_type );
106 }
107
108 return $this->resolve_with_fallback( $plain_value, $prop_type );
109 }
110
111 private function resolve_object( $plain_value, Object_Prop_Type $prop_type ) {
112 if ( ! is_array( $plain_value ) ) {
113 return null;
114 }
115
116 $shape = $prop_type->get_shape();
117
118 if ( empty( array_intersect_key( $plain_value, $shape ) ) ) {
119 return null;
120 }
121
122 $converted = [];
123
124 foreach ( $shape as $key => $field_prop_type ) {
125 if ( ! array_key_exists( $key, $plain_value ) ) {
126 continue;
127 }
128
129 $resolved = $this->resolve( $plain_value[ $key ], $field_prop_type );
130
131 if ( null !== $resolved ) {
132 $converted[ $key ] = $resolved;
133 }
134 }
135
136 return $prop_type::generate( $converted );
137 }
138
139 private function resolve_array( $plain_value, Array_Prop_Type $prop_type ) {
140 if ( ! is_array( $plain_value ) ) {
141 return null;
142 }
143
144 $item_type = $prop_type->get_item_type();
145 $converted = [];
146
147 foreach ( $plain_value as $item ) {
148 $resolved = $this->resolve( $item, $item_type );
149
150 if ( null !== $resolved ) {
151 $converted[] = $resolved;
152 }
153 }
154
155 return $prop_type::generate( $converted );
156 }
157
158 private function resolve_with_registered( $plain_value, Prop_Type $prop_type ) {
159 $resolver = $this->registry->get( $prop_type::get_key() );
160
161 if ( ! ( $resolver instanceof Plain_Resolver_Base ) ) {
162 return null;
163 }
164
165 $resolved = $resolver->resolve( $plain_value );
166
167 if ( null === $resolved ) {
168 return null;
169 }
170
171 return $this->normalize_resolver_output( $resolved, $prop_type );
172 }
173
174 private function resolve_with_fallback( $plain_value, Prop_Type $prop_type ) {
175 if ( $prop_type instanceof Plain_Prop_Type && ! is_scalar( $plain_value ) ) {
176 return null;
177 }
178
179 $resolver = $this->registry->get( $prop_type::get_key() );
180
181 if ( ! ( $resolver instanceof Plain_Resolver_Base ) ) {
182 return null;
183 }
184
185 $resolved = $resolver->resolve( $plain_value );
186
187 if ( null === $resolved ) {
188 return null;
189 }
190
191 return $this->normalize_resolver_output( $resolved, $prop_type );
192 }
193
194 private function normalize_resolver_output( $resolved, Prop_Type $prop_type ) {
195 if ( is_array( $resolved ) && isset( $resolved['$$type'] ) ) {
196 return $resolved;
197 }
198
199 return $prop_type::generate( $resolved );
200 }
201 }
202