| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class Prop_Canonicalizer { |
| 12 |
|
| 13 |
/** |
| 14 |
* Resolves a property name to its canonical key in the schema. |
| 15 |
* Mirrors the frontend's resolveCanonicalPropName function. |
| 16 |
* |
| 17 |
* @param array<string, Prop_Type> $schema The widget props schema. |
| 18 |
* @param string $name The property name to resolve (may be canonical or alias). |
| 19 |
* @param array<string, string>|null $alias_map Optional precomputed alias map from build_alias_map(). |
| 20 |
* Pass it when resolving many props against the same schema. |
| 21 |
* |
| 22 |
* @return string|null The canonical key if found, or null if unknown. |
| 23 |
*/ |
| 24 |
public static function resolve_canonical_key( array $schema, string $name, ?array $alias_map = null ): ?string { |
| 25 |
if ( isset( $schema[ $name ] ) ) { |
| 26 |
return $name; |
| 27 |
} |
| 28 |
|
| 29 |
$alias_map = $alias_map ?? self::build_alias_map( $schema ); |
| 30 |
|
| 31 |
return $alias_map[ $name ] ?? null; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Builds an alias-to-canonical map for the given schema. |
| 36 |
* O(n) over schema props — precompute once and pass to resolve_canonical_key() |
| 37 |
* or resolve_canonical_prop_keys() when resolving many props against the same schema. |
| 38 |
* |
| 39 |
* @param array<string, Prop_Type> $schema The widget props schema. |
| 40 |
* @return array<string, string> Map of alias => canonical. |
| 41 |
*/ |
| 42 |
public static function build_alias_map( array $schema ): array { |
| 43 |
return self::build_alias_to_canonical_map( $schema ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Resolves all property keys in an array to their canonical names. |
| 48 |
* Mirrors the frontend's resolveCanonicalPropKeys function. |
| 49 |
* |
| 50 |
* @param array<string, Prop_Type> $schema The widget props schema. |
| 51 |
* @param array<string, mixed> $props The props to resolve. |
| 52 |
* @param array<string, string>|null $alias_map Optional precomputed alias map from build_alias_map(). |
| 53 |
* |
| 54 |
* @return array<string, mixed> Props with canonical keys. |
| 55 |
*/ |
| 56 |
public static function resolve_canonical_prop_keys( array $schema, array $props, ?array $alias_map = null ): array { |
| 57 |
$alias_map = $alias_map ?? self::build_alias_to_canonical_map( $schema ); |
| 58 |
$resolved = []; |
| 59 |
|
| 60 |
foreach ( $props as $key => $value ) { |
| 61 |
if ( isset( $schema[ $key ] ) ) { |
| 62 |
$resolved[ $key ] = $value; |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
$canonical = $alias_map[ $key ] ?? null; |
| 67 |
|
| 68 |
if ( ! $canonical ) { |
| 69 |
$resolved[ $key ] = $value; |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! array_key_exists( $canonical, $resolved ) ) { |
| 74 |
$resolved[ $canonical ] = $value; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return $resolved; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns the list of available property names in the schema. |
| 83 |
* |
| 84 |
* @param array<string, Prop_Type> $schema The widget props schema. |
| 85 |
* |
| 86 |
* @return string[] List of canonical property names. |
| 87 |
*/ |
| 88 |
public static function available_prop_names( array $schema ): array { |
| 89 |
return array_keys( $schema ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Builds a map from alias names to canonical property names. |
| 94 |
* |
| 95 |
* @param array<string, Prop_Type> $schema The widget props schema. |
| 96 |
* |
| 97 |
* @return array<string, string> Map of alias => canonical. |
| 98 |
*/ |
| 99 |
private static function build_alias_to_canonical_map( array $schema ): array { |
| 100 |
$alias_to_canonical = []; |
| 101 |
|
| 102 |
foreach ( $schema as $canonical => $prop_type ) { |
| 103 |
if ( ! ( $prop_type instanceof Prop_Type ) ) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
$aliases = $prop_type->get_aliases(); |
| 108 |
|
| 109 |
foreach ( $aliases as $alias ) { |
| 110 |
if ( is_string( $alias ) && '' !== $alias ) { |
| 111 |
$alias_to_canonical[ $alias ] = $canonical; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return $alias_to_canonical; |
| 117 |
} |
| 118 |
} |
| 119 |
|