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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
<?php
function convert_element_type_to_class_name( $element_type ) { $element_class_name = str_replace( '_', ' ', $element_type ); $element_class_name = ucwords( $element_class_name ); $element_class_name = str_replace( ' ', '', $element_class_name ); return $element_class_name; }
function convert_json_elements_to_php( $json, $class_name, $section_type, $pattern_type, $pattern_name, $position = 10 ) { $elements = json_decode( $json, true );
// Start building the PHP code $php = "<?php\nnamespace YayMail\\TemplatePatterns\\Patterns;\n\n";
// Add imports $php .= "use YayMail\\Abstracts\\BasePattern;\n";
// Collect all element types for imports (including nested elements) $element_types = collect_all_element_types( $elements ); foreach ( $element_types as $element_type ) { $element_class_name = convert_element_type_to_class_name( $element_type ); $php .= "use YayMail\\Elements\\{$element_class_name};\n"; }
$php .= "use YayMail\\TemplatePatterns\\SectionTemplates\\{$section_type};\n"; $php .= "use YayMail\\Utils\\SingletonTrait;\n\n";
// Class definition $php .= "/**\n * {$class_name} Class\n */\n"; $php .= "class {$class_name} extends BasePattern {\n"; $php .= " use SingletonTrait;\n\n"; $php .= " public const TYPE = '{$pattern_type}';\n\n";
// Constructor $php .= " public function __construct() {\n"; $php .= " \$this->id = uniqid();\n"; $php .= " \$this->section = {$section_type}::TYPE;\n"; $php .= " \$this->position = {$position};\n"; $php .= " \$this->name = __('{$pattern_name}', 'yaymail');\n"; $php .= " \$this->elements = [\n";
// Add each element foreach ( $elements as $element ) { $php .= process_element( $element, 3 ); }
// Close elements array and class $php .= " ];\n"; $php .= " }\n"; $php .= "}\n";
return $php; }
function collect_all_element_types( $elements ) { $types = [];
foreach ( $elements as $element ) { // Add this element's type if ( ! in_array( $element['type'], $types ) ) { $types[] = $element['type']; }
// Process children if they exist if ( isset( $element['children'] ) && is_array( $element['children'] ) ) { $child_types = collect_all_element_types( $element['children'] ); foreach ( $child_types as $type ) { if ( ! in_array( $type, $types ) ) { $types[] = $type; } } } }
return $types; }
function process_element( $element, $indent_level ) { $element_class_name = convert_element_type_to_class_name( $element['type'] ); $indentation = str_repeat( ' ', $indent_level ); $php = '';
// Special case for ColumnLayout if ( $element['type'] === 'column_layout' ) { $columns_count = $element['data']['amount_of_columns'] ?? count( $element['children'] ?? [] ); $php .= "{$indentation}{$element_class_name}::get_object_data(\n"; $php .= "{$indentation} {$columns_count},\n"; $php .= "{$indentation} [\n";
// Process normal attributes foreach ( $element['data'] as $key => $value ) { if ( $key !== 'amount_of_columns' && $key !== 'children' ) { $php .= convert_value_to_php( $key, $value, $indent_level + 1 ); } }
// Process children if they exist if ( isset( $element['children'] ) && ! empty( $element['children'] ) ) { $php .= "{$indentation} 'children' => [\n"; foreach ( $element['children'] as $child ) { $php .= process_element( $child, $indent_level + 3 ); } $php .= "{$indentation} ],\n"; }
$php .= "{$indentation} ]\n"; $php .= "{$indentation}),\n"; }//end if // Special case for Column elseif ( $element['type'] === 'column' ) { $width = $element['data']['width'] ?? 50; // Default width if not provided $php .= "{$indentation}{$element_class_name}::get_object_data(\n"; $php .= "{$indentation} {$width},\n"; $php .= "{$indentation} [\n";
// Process children if they exist if ( isset( $element['children'] ) && ! empty( $element['children'] ) ) { $php .= "{$indentation} 'children' => [\n"; foreach ( $element['children'] as $child ) { $php .= process_element( $child, $indent_level + 3 ); } $php .= "{$indentation} ],\n"; }
$php .= "{$indentation} ]\n"; $php .= "{$indentation}),\n"; } // Regular elements else { $php .= "{$indentation}{$element_class_name}::get_object_data(\n"; $php .= "{$indentation} [\n";
// Process data attributes if ( isset( $element['data'] ) ) { foreach ( $element['data'] as $key => $value ) { $php .= convert_value_to_php( $key, $value, $indent_level + 1 ); } }
$php .= "{$indentation} ]\n"; $php .= "{$indentation}),\n"; }
return $php; }
function convert_value_to_php( $key, $value, $indent ) { $indentation = str_repeat( ' ', $indent * 4 ); $php = '';
if ( is_array( $value ) ) { $php .= "{$indentation}'{$key}' => [\n";
foreach ( $value as $sub_key => $sub_value ) { if ( is_array( $sub_value ) ) { $php .= convert_value_to_php( $sub_key, $sub_value, $indent + 1 ); } else { if ( is_string( $sub_value ) ) { // Escape apostrophes and backslashes for PHP single-quoted strings $escaped_value = str_replace( [ '\\', "'" ], [ '\\\\', "\\'" ], $sub_value ); $formatted_value = "'{$escaped_value}'"; } else { $formatted_value = $sub_value; } $php .= str_repeat( ' ', ( $indent + 1 ) * 4 ) . "'{$sub_key}' => {$formatted_value},\n"; } }
$php .= "{$indentation}],\n"; } else { if ( is_string( $value ) ) { // Escape apostrophes and backslashes for PHP single-quoted strings $escaped_value = str_replace( [ '\\', "'" ], [ '\\\\', "\\'" ], $value ); $formatted_value = "'{$escaped_value}'"; } else { $formatted_value = $value; } $php .= "{$indentation}'{$key}' => {$formatted_value},\n"; }//end if
return $php; }
function create_pattern_file( $json_string, $section_type, $pattern_type, $pattern_name, $position = 10 ) { // Generate the PHP code $class_name = convert_element_type_to_class_name( $pattern_type ); $php_code = convert_json_elements_to_php( $json_string, $class_name, $section_type, $pattern_type, $pattern_name, $position );
// Define the file path $patterns_dir = './Patterns'; $file_path = $patterns_dir . '/' . $class_name . '.php';
// Write the PHP code to file if ( file_put_contents( $file_path, $php_code ) ) { return true; } else { return "Failed to write pattern file: {$file_path}"; } }
// Example usage (uncomment to use) create_pattern_file( file_get_contents( './tool-json.json' ), 'Banner', 'banner_23', 'Banner 23', $position = 20 );
|