| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\PropsResolver; |
| 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\Contracts\Prop_Type; |
| 8 |
use Elementor\Modules\AtomicWidgets\PropTypes\Prop_Type_Migrator; |
| 9 |
use Elementor\Modules\AtomicWidgets\PropTypes\Union_Prop_Type; |
| 10 |
use Exception; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
abstract class Props_Resolver { |
| 17 |
protected static array $instances = []; |
| 18 |
|
| 19 |
protected Transformers_Registry $transformers_registry; |
| 20 |
|
| 21 |
protected function __construct( Transformers_Registry $transformers_registry ) { |
| 22 |
$this->transformers_registry = $transformers_registry; |
| 23 |
} |
| 24 |
|
| 25 |
protected static function instance( string $context ) { |
| 26 |
if ( ! isset( static::$instances[ $context ] ) ) { |
| 27 |
$instance = new static( new Transformers_Registry() ); |
| 28 |
|
| 29 |
static::$instances[ $context ] = $instance; |
| 30 |
|
| 31 |
do_action( |
| 32 |
"elementor/atomic-widgets/$context/transformers/register", |
| 33 |
$instance->get_transformers_registry(), |
| 34 |
$instance |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
return static::$instances[ $context ]; |
| 39 |
} |
| 40 |
|
| 41 |
public static function reset(): void { |
| 42 |
static::$instances = []; |
| 43 |
} |
| 44 |
|
| 45 |
public function get_transformers_registry(): Transformers_Registry { |
| 46 |
return $this->transformers_registry; |
| 47 |
} |
| 48 |
|
| 49 |
protected function transform( $value, $key, Prop_Type $prop_type ) { |
| 50 |
$value = Prop_Type_Migrator::migrate( $value, $prop_type ); |
| 51 |
|
| 52 |
if ( $prop_type instanceof Union_Prop_Type ) { |
| 53 |
$prop_type = $prop_type->get_prop_type( $value['$$type'] ); |
| 54 |
|
| 55 |
if ( ! $prop_type ) { |
| 56 |
return null; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if ( $value['$$type'] !== $prop_type::get_key() ) { |
| 61 |
return null; |
| 62 |
} |
| 63 |
|
| 64 |
if ( $prop_type instanceof Object_Prop_Type ) { |
| 65 |
if ( ! is_array( $value['value'] ) ) { |
| 66 |
return null; |
| 67 |
} |
| 68 |
|
| 69 |
$value['value'] = $this->resolve( |
| 70 |
$prop_type->get_shape(), |
| 71 |
$value['value'] |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
if ( $prop_type instanceof Array_Prop_Type ) { |
| 76 |
if ( ! is_array( $value['value'] ) ) { |
| 77 |
return null; |
| 78 |
} |
| 79 |
|
| 80 |
$value['value'] = array_map( |
| 81 |
fn( $item ) => $this->resolve_item( $item, null, $prop_type->get_item_type() ), |
| 82 |
$value['value'] |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
$transformer = $this->transformers_registry->get( $value['$$type'] ); |
| 87 |
|
| 88 |
if ( ! ( $transformer instanceof Transformer_Base ) ) { |
| 89 |
return null; |
| 90 |
} |
| 91 |
|
| 92 |
try { |
| 93 |
$context = Props_Resolver_Context::make() |
| 94 |
->set_key( $key ) |
| 95 |
->set_disabled( (bool) ( $value['disabled'] ?? false ) ) |
| 96 |
->set_prop_type( $prop_type ) |
| 97 |
->set_transformers_registry( $this->transformers_registry ); |
| 98 |
|
| 99 |
return $transformer->transform( $value['value'], $context ); |
| 100 |
} catch ( Exception $e ) { |
| 101 |
return null; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
protected function is_transformable( $value ): bool { |
| 106 |
return ( |
| 107 |
! empty( $value['$$type'] ) && |
| 108 |
array_key_exists( 'value', $value ) |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
abstract public function resolve( array $schema, array $props ): array; |
| 113 |
|
| 114 |
abstract protected function resolve_item( $value, $key, Prop_Type $prop_type ); |
| 115 |
} |
| 116 |
|