base-meta-box.php
84 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Admin\Single_Pages\Meta_Boxes; |
| 5 | |
| 6 | use Jet_Form_Builder\Admin\Exceptions\Empty_Box_Exception; |
| 7 | use Jet_Form_Builder\Admin\Exceptions\Not_Found_Page_Exception; |
| 8 | use Jet_Form_Builder\Admin\Pages\Pages_Manager; |
| 9 | use Jet_Form_Builder\Admin\Single_Pages\Base_Single_Page; |
| 10 | use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 11 | use Jet_Form_Builder\Classes\Repository\Repository_Item_With_Class; |
| 12 | use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 13 | use Jet_Form_Builder\Db_Queries\Traits\Model_Dependencies; |
| 14 | use Jet_Form_Builder\Db_Queries\Traits\Model_Dependencies_Interface; |
| 15 | |
| 16 | abstract class Base_Meta_Box implements Model_Dependencies_Interface, Arrayable { |
| 17 | |
| 18 | const TYPE_LIST = 'list'; |
| 19 | const TYPE_TABLE = 'table'; |
| 20 | |
| 21 | use Repository_Item_With_Class; |
| 22 | use Model_Dependencies; |
| 23 | |
| 24 | private $id = 0; |
| 25 | |
| 26 | abstract public function get_title(): string; |
| 27 | |
| 28 | abstract public function get_values(): array; |
| 29 | |
| 30 | public function get_slug(): string { |
| 31 | return str_replace( ' ', '-', strtolower( $this->get_title() ) ); |
| 32 | } |
| 33 | |
| 34 | public function is_active(): bool { |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return int |
| 40 | */ |
| 41 | public function get_id(): int { |
| 42 | return $this->id; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param int $id |
| 47 | * |
| 48 | * @return Base_Meta_Box |
| 49 | */ |
| 50 | public function set_id( int $id ): Base_Meta_Box { |
| 51 | $this->id = $id; |
| 52 | |
| 53 | return $this; |
| 54 | } |
| 55 | |
| 56 | public function set_single_id(): Base_Meta_Box { |
| 57 | /** @var Base_Single_Page $single */ |
| 58 | try { |
| 59 | $single = Pages_Manager::instance()->get_current(); |
| 60 | } catch ( Not_Found_Page_Exception $e ) { |
| 61 | return $this; |
| 62 | } |
| 63 | |
| 64 | return $this->set_id( $single->get_id() ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @return int[] |
| 69 | * @throws Empty_Box_Exception |
| 70 | */ |
| 71 | public function to_array(): array { |
| 72 | try { |
| 73 | $this->prepare_dependencies(); |
| 74 | } catch ( Sql_Exception $exception ) { |
| 75 | throw new Empty_Box_Exception( $exception->getMessage(), ...$exception->get_additional() ); |
| 76 | } |
| 77 | return array( |
| 78 | 'slug' => $this->get_slug(), |
| 79 | 'title' => $this->get_title(), |
| 80 | 'list' => $this->get_values(), |
| 81 | ); |
| 82 | } |
| 83 | } |
| 84 |