| 1 |
<?php |
| 2 |
namespace YayMail\Builder; |
| 3 |
|
| 4 |
/** |
| 5 |
* Builder class for constructing email elements |
| 6 |
*/ |
| 7 |
class ElementBuilder { |
| 8 |
/** |
| 9 |
* Array of elements to build |
| 10 |
* |
| 11 |
* @var array |
| 12 |
*/ |
| 13 |
protected $elements = []; |
| 14 |
|
| 15 |
/** |
| 16 |
* Constructor |
| 17 |
*/ |
| 18 |
public function __construct() {} |
| 19 |
|
| 20 |
public function add_element( $type, $attributes ) { |
| 21 |
$this->elements[] = [ |
| 22 |
'type' => $type, |
| 23 |
'attributes' => $attributes, |
| 24 |
]; |
| 25 |
|
| 26 |
return $this; |
| 27 |
} |
| 28 |
|
| 29 |
public function build() { |
| 30 |
$built_elements = []; |
| 31 |
|
| 32 |
foreach ( $this->elements as $element ) { |
| 33 |
$element_type = $element['type']; |
| 34 |
$attributes = $element['attributes']; |
| 35 |
|
| 36 |
if ( isset( $element['integration'] ) && '3rd' === $element['integration'] ) { |
| 37 |
$class = 'YayMail\Integrations\\' . $element_type; |
| 38 |
} elseif ( ! empty( $element['addon_namespace'] ) ) { |
| 39 |
$class = $element['addon_namespace'] . '\\Elements\\' . $element_type; |
| 40 |
} else { |
| 41 |
$class = 'YayMail\Elements\\' . $element_type; |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! class_exists( $class ) ) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
if ( 'ColumnLayout' === $element_type ) { |
| 49 |
$amount_of_columns = $attributes['amount_of_columns'] ?? 1; |
| 50 |
unset( $attributes['amount_of_columns'] ); |
| 51 |
$element_data = $class::get_data( $amount_of_columns, $attributes ); |
| 52 |
} elseif ( 'Column' === $element_type ) { |
| 53 |
$width = $attributes['column_width'] ?? 5; |
| 54 |
unset( $attributes['column_width'] ); |
| 55 |
$element_data = $class::get_data( $width, $attributes ); |
| 56 |
} else { |
| 57 |
$element_data = $class::get_data( $attributes ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( isset( $attributes['children'] ) && is_array( $attributes['children'] ) ) { |
| 61 |
$element_data['children'] = array_merge( ...array_values( $attributes['children'] ) ); |
| 62 |
} |
| 63 |
|
| 64 |
$built_elements[] = $element_data; |
| 65 |
}//end foreach |
| 66 |
|
| 67 |
return $built_elements; |
| 68 |
} |
| 69 |
} |
| 70 |
|