| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Blocks\Conditional_Block; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Blocks\Conditional_Block\Render_States\Base_Render_State; |
| 7 |
use Jet_Form_Builder\Blocks\Conditional_Block\Render_States\Custom_Render_State; |
| 8 |
use Jet_Form_Builder\Classes\Arrayable\Collection; |
| 9 |
|
| 10 |
// If this file is called directly, abort. |
| 11 |
if ( ! defined( 'WPINC' ) ) { |
| 12 |
die; |
| 13 |
} |
| 14 |
|
| 15 |
class Render_States_Collection extends Collection { |
| 16 |
|
| 17 |
private $exclude_list = array(); |
| 18 |
|
| 19 |
/** |
| 20 |
* @param Base_Render_State|string $item |
| 21 |
* |
| 22 |
* @return Collection |
| 23 |
*/ |
| 24 |
public function push( $item ): Collection { |
| 25 |
if ( ! is_object( $item ) ) { |
| 26 |
$custom = new Custom_Render_State(); |
| 27 |
$custom->set_id( $item ); |
| 28 |
|
| 29 |
return $this->push( $custom ); |
| 30 |
} |
| 31 |
|
| 32 |
$states = $item->exclude_states(); |
| 33 |
|
| 34 |
if ( count( $states ) ) { |
| 35 |
array_push( $this->exclude_list, ...$states ); |
| 36 |
} |
| 37 |
|
| 38 |
return $this->add( $item ); |
| 39 |
} |
| 40 |
|
| 41 |
public function confirm(): Collection { |
| 42 |
$positions = array(); |
| 43 |
|
| 44 |
foreach ( $this as $position => $state ) { |
| 45 |
$current = get_class( $state ); |
| 46 |
|
| 47 |
if ( ! in_array( $current, $this->exclude_list, true ) ) { |
| 48 |
continue; |
| 49 |
} |
| 50 |
|
| 51 |
$positions[] = $position; |
| 52 |
} |
| 53 |
|
| 54 |
$this->remove( $positions ); |
| 55 |
$this->exclude_list = array(); |
| 56 |
|
| 57 |
return $this; |
| 58 |
} |
| 59 |
|
| 60 |
public function render(): string { |
| 61 |
$response = ''; |
| 62 |
|
| 63 |
/** @var Base_Render_State $item */ |
| 64 |
foreach ( $this as $item ) { |
| 65 |
$response .= "\r\n" . $item->render(); |
| 66 |
} |
| 67 |
|
| 68 |
return $response; |
| 69 |
} |
| 70 |
|
| 71 |
} |
| 72 |
|