| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\ChildrenDependencies; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\PropDependencies\Manager as Dependency_Manager; |
| 6 |
use Elementor\Modules\AtomicWidgets\Utils\Element_Position; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Child_Dependency { |
| 13 |
|
| 14 |
private string $child_type; |
| 15 |
private ?Dependency_Manager $when = null; |
| 16 |
private ?Element_Position $position = null; |
| 17 |
private bool $stash = true; |
| 18 |
private ?array $default_model = null; |
| 19 |
|
| 20 |
private function __construct( string $child_type ) { |
| 21 |
$this->child_type = $child_type; |
| 22 |
} |
| 23 |
|
| 24 |
public static function for( string $child_type ): self { |
| 25 |
return new self( $child_type ); |
| 26 |
} |
| 27 |
|
| 28 |
public function when( Dependency_Manager $when ): self { |
| 29 |
$this->when = $when; |
| 30 |
|
| 31 |
return $this; |
| 32 |
} |
| 33 |
|
| 34 |
public function position( Element_Position $position ): self { |
| 35 |
$this->position = $position; |
| 36 |
|
| 37 |
return $this; |
| 38 |
} |
| 39 |
|
| 40 |
public function stash( bool $stash = true ): self { |
| 41 |
$this->stash = $stash; |
| 42 |
|
| 43 |
return $this; |
| 44 |
} |
| 45 |
|
| 46 |
public function default_model( array $default_model ): self { |
| 47 |
$this->default_model = $default_model; |
| 48 |
|
| 49 |
return $this; |
| 50 |
} |
| 51 |
|
| 52 |
public function build(): array { |
| 53 |
if ( null === $this->when ) { |
| 54 |
throw new \InvalidArgumentException( 'Child_Dependency: `when` clause is required.' ); |
| 55 |
} |
| 56 |
|
| 57 |
$when_config = $this->when->get(); |
| 58 |
|
| 59 |
if ( null === $when_config ) { |
| 60 |
throw new \InvalidArgumentException( 'Child_Dependency: `when` clause must contain at least one term.' ); |
| 61 |
} |
| 62 |
|
| 63 |
return [ |
| 64 |
'child_type' => $this->child_type, |
| 65 |
'when' => $when_config, |
| 66 |
'position' => ( $this->position ?? Element_Position::last() )->to_array(), |
| 67 |
'stash' => $this->stash, |
| 68 |
'default_model' => $this->default_model, |
| 69 |
]; |
| 70 |
} |
| 71 |
} |
| 72 |
|