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