| @@ -1,32 +1,80 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace Jet_Form_Builder\Generators; | |
| 4 | - | |
| 5 | -abstract class Base { | |
| 6 | - | |
| 7 | - /** | |
| 8 | - * Returns generator ID | |
| 9 | - * | |
| 10 | - * @return [type] [description] | |
| 11 | - */ | |
| 12 | - abstract public function get_id(); | |
| 13 | - | |
| 14 | - /** | |
| 15 | - * Returns generator name | |
| 16 | - * | |
| 17 | - * @return [type] [description] | |
| 18 | - */ | |
| 19 | - abstract public function get_name(); | |
| 20 | - | |
| 21 | - /** | |
| 22 | - * Returns generated options list | |
| 23 | - * | |
| 24 | - * @return array | |
| 25 | - */ | |
| 26 | - abstract public function generate( $field ); | |
| 27 | - | |
| 28 | - public function can_generate() { | |
| 29 | - return true; | |
| 30 | - } | |
| 31 | - | |
| 32 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace Jet_Form_Builder\Generators; | |
| 4 | + | |
| 5 | +// If this file is called directly, abort. | |
| 6 | +if ( ! defined( 'WPINC' ) ) { | |
| 7 | + die; | |
| 8 | +} | |
| 9 | + | |
| 10 | +abstract class Base { | |
| 11 | + | |
| 12 | + /** | |
| 13 | + * @var \Jet_Form_Builder\Blocks\Types\Base | |
| 14 | + */ | |
| 15 | + protected $block; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Returns generator ID | |
| 19 | + * | |
| 20 | + * @return [type] [description] | |
| 21 | + */ | |
| 22 | + abstract public function get_id(); | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * Returns generator name | |
| 26 | + * | |
| 27 | + * @return [type] [description] | |
| 28 | + */ | |
| 29 | + abstract public function get_name(); | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Returns generated options list | |
| 33 | + * | |
| 34 | + * @param $args | |
| 35 | + * | |
| 36 | + * @return array | |
| 37 | + */ | |
| 38 | + abstract public function generate( $args ); | |
| 39 | + | |
| 40 | + public function can_generate() { | |
| 41 | + return true; | |
| 42 | + } | |
| 43 | + | |
| 44 | + public function incoming_args() { | |
| 45 | + return array( | |
| 46 | + 'generator_field' => function ( $value ) { | |
| 47 | + return $value; | |
| 48 | + }, | |
| 49 | + ); | |
| 50 | + } | |
| 51 | + | |
| 52 | + public function get_values( $args ) { | |
| 53 | + $fields = array(); | |
| 54 | + | |
| 55 | + foreach ( $this->incoming_args() as $name => $parse_callable ) { | |
| 56 | + $fields[ $name ] = isset( $args[ $name ] ) ? call_user_func( $parse_callable, $args[ $name ] ) : false; | |
| 57 | + } | |
| 58 | + | |
| 59 | + return $this->generate( $fields ); | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * @param \Jet_Form_Builder\Blocks\Types\Base $block | |
| 64 | + */ | |
| 65 | + public function set_block( \Jet_Form_Builder\Blocks\Types\Base $block ) { | |
| 66 | + $this->block = $block; | |
| 67 | + } | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * @return \Jet_Form_Builder\Blocks\Types\Base | |
| 71 | + */ | |
| 72 | + public function get_block(): \Jet_Form_Builder\Blocks\Types\Base { | |
| 73 | + return $this->block; | |
| 74 | + } | |
| 75 | + | |
| 76 | + public function clear_block() { | |
| 77 | + $this->block = null; | |
| 78 | + } | |
| 79 | + | |
| 80 | +} | |