| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Classes\Arrayable; |
| 5 |
|
| 6 |
use JFB_Components\Repository\Repository_Item_Instance_Trait; |
| 7 |
use JFB_Components\Repository\Repository_Static_Item_It; |
| 8 |
|
| 9 |
// If this file is called directly, abort. |
| 10 |
if ( ! defined( 'WPINC' ) ) { |
| 11 |
die; |
| 12 |
} |
| 13 |
|
| 14 |
class Array_Convert_Once { |
| 15 |
|
| 16 |
/** |
| 17 |
* @var array[]|Arrayable[] |
| 18 |
*/ |
| 19 |
private $stack = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* @param Arrayable $item |
| 23 |
* |
| 24 |
* @return mixed |
| 25 |
*/ |
| 26 |
public function add( Arrayable $item ): Arrayable { |
| 27 |
if ( ! ( $item instanceof Arrayable_Once ) ) { |
| 28 |
return $item; |
| 29 |
} |
| 30 |
$slug = $this->get_slug( $item ); |
| 31 |
|
| 32 |
if ( isset( $this->stack[ $slug ] ) ) { |
| 33 |
return $this->stack[ $slug ]; |
| 34 |
} |
| 35 |
|
| 36 |
$this->stack[ $slug ] = $item; |
| 37 |
|
| 38 |
return $this->stack[ $slug ]; |
| 39 |
} |
| 40 |
|
| 41 |
public function to_array( Arrayable $item ): array { |
| 42 |
if ( ! ( $item instanceof Arrayable_Once ) ) { |
| 43 |
return $item->to_array(); |
| 44 |
} |
| 45 |
$slug = $this->get_slug( $item ); |
| 46 |
|
| 47 |
if ( isset( $this->stack[ $slug ] ) && is_array( $this->stack[ $slug ] ) ) { |
| 48 |
return $this->stack[ $slug ]; |
| 49 |
} |
| 50 |
|
| 51 |
if ( isset( $this->stack[ $slug ] ) ) { |
| 52 |
$this->stack[ $slug ] = $this->stack[ $slug ]->to_array(); |
| 53 |
|
| 54 |
return $this->stack[ $slug ]; |
| 55 |
} |
| 56 |
|
| 57 |
$this->add( $item ); |
| 58 |
|
| 59 |
return $this->to_array( $item ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @param string $slug |
| 64 |
* |
| 65 |
* @return array|Arrayable |
| 66 |
* @throws Array_Continue_Exception |
| 67 |
*/ |
| 68 |
public function get_item( string $slug ) { |
| 69 |
$this->exist( $slug ); |
| 70 |
|
| 71 |
return $this->stack[ $slug ]; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @param string $slug |
| 76 |
* |
| 77 |
* @return array|Arrayable |
| 78 |
* @throws Array_Continue_Exception |
| 79 |
*/ |
| 80 |
public function get_array_item( string $slug ) { |
| 81 |
$item = $this->get_item( $slug ); |
| 82 |
|
| 83 |
if ( is_array( $item ) ) { |
| 84 |
return $item; |
| 85 |
} |
| 86 |
|
| 87 |
return $this->to_array( $item ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* @param string $slug |
| 92 |
* |
| 93 |
* @return $this |
| 94 |
* @throws Array_Continue_Exception |
| 95 |
*/ |
| 96 |
public function exist( string $slug ): Array_Convert_Once { |
| 97 |
if ( isset( $this->stack[ $slug ] ) ) { |
| 98 |
return $this; |
| 99 |
} |
| 100 |
|
| 101 |
throw new Array_Continue_Exception( esc_html( 'Undefined item: ' . $slug ) ); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
/** |
| 106 |
* @param Arrayable $item |
| 107 |
* |
| 108 |
* @return mixed |
| 109 |
*/ |
| 110 |
private function get_slug( Arrayable $item ) { |
| 111 |
if ( $item instanceof Repository_Item_Instance_Trait ) { |
| 112 |
return $item->rep_item_id(); |
| 113 |
} |
| 114 |
if ( $item instanceof Repository_Static_Item_It ) { |
| 115 |
return $item::rep_item_id(); |
| 116 |
} |
| 117 |
|
| 118 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 119 |
wp_die( 'Invalid element. Item must an instance of ' . Repository_Item_Instance_Trait::class ); |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|