| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\CssConverter\Converters; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\CssConverter\Conversion_Context; |
| 6 |
use Elementor\Modules\AtomicWidgets\CssConverter\Property_Converter_Base; |
| 7 |
use Elementor\Modules\AtomicWidgets\PropTypes\Flex_Prop_Type; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Converter for individual flex longhands (flex-grow, flex-basis) that contribute one field to |
| 15 |
* the aggregate `flex` prop. Reads the existing flex prop from context (if any), updates the |
| 16 |
* target field, then writes the merged result back. |
| 17 |
* |
| 18 |
* Value parsing is delegated to an injected callable so each instance can apply the correct |
| 19 |
* parser (numeric for flex-grow, size for flex-basis) without duplicating merge logic. |
| 20 |
*/ |
| 21 |
class Flex_Longhand_Converter extends Property_Converter_Base { |
| 22 |
private string $property; |
| 23 |
private string $field_key; |
| 24 |
|
| 25 |
/** @var callable(string): ?array */ |
| 26 |
private $value_parser; |
| 27 |
|
| 28 |
public function __construct( string $property, string $field_key, callable $value_parser ) { |
| 29 |
$this->property = $property; |
| 30 |
$this->field_key = $field_key; |
| 31 |
$this->value_parser = $value_parser; |
| 32 |
} |
| 33 |
|
| 34 |
protected function get_supported_properties(): array { |
| 35 |
return [ $this->property ]; |
| 36 |
} |
| 37 |
|
| 38 |
protected function convert_null( Conversion_Context $context, array $rule ): bool { |
| 39 |
$fields = $this->current_flex_fields( $context->get_prop( 'flex' ) ); |
| 40 |
$fields[ $this->field_key ] = null; |
| 41 |
|
| 42 |
$context->set_prop( 'flex', Flex_Prop_Type::generate( $fields ) ); |
| 43 |
|
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
protected function do_convert( Conversion_Context $context, array $rule ): bool { |
| 48 |
$parsed = ( $this->value_parser )( trim( $rule['value'] ) ); |
| 49 |
|
| 50 |
if ( null === $parsed ) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
$fields = $this->current_flex_fields( $context->get_prop( 'flex' ) ); |
| 55 |
$fields[ $this->field_key ] = $parsed; |
| 56 |
|
| 57 |
$context->set_prop( 'flex', Flex_Prop_Type::generate( $fields ) ); |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
private function current_flex_fields( $existing ): array { |
| 63 |
if ( |
| 64 |
is_array( $existing ) && |
| 65 |
Flex_Prop_Type::get_key() === ( $existing['$$type'] ?? null ) && |
| 66 |
is_array( $existing['value'] ?? null ) |
| 67 |
) { |
| 68 |
return $existing['value']; |
| 69 |
} |
| 70 |
|
| 71 |
return []; |
| 72 |
} |
| 73 |
} |
| 74 |
|