jetformbuilder
/
includes
/
compatibility
/
jet-engine
/
generators
/
je-query-object-handlers
/
base-object-handler.php
jetformbuilder
/
includes
/
compatibility
/
jet-engine
/
generators
/
je-query-object-handlers
Last commit date
base-object-handler.php
3 years ago
user-object-handler.php
3 years ago
base-object-handler.php
68 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Compatibility\Jet_Engine\Generators\Je_Query_Object_Handlers; |
| 5 | |
| 6 | |
| 7 | class Base_Object_Handler { |
| 8 | |
| 9 | protected $value_field; |
| 10 | protected $label_field; |
| 11 | protected $calc_field; |
| 12 | protected $additional; |
| 13 | |
| 14 | public function is_supported( $object ): bool { |
| 15 | return true; |
| 16 | } |
| 17 | |
| 18 | public function set_fields( array $args ) { |
| 19 | $this->value_field = $args[1] ?? 'ID'; |
| 20 | $this->label_field = $args[2] ?? 'post_title'; |
| 21 | $this->calc_field = $args[3] ?? false; |
| 22 | $this->additional = $args[4] ?? false; |
| 23 | } |
| 24 | |
| 25 | final public function to_array( $object ): array { |
| 26 | // convert to array because in 8.2 dynamic properties will be deprecated |
| 27 | $converted = $this->get_converted( $object ); |
| 28 | |
| 29 | $value = $this->get_value( $converted, $object ); |
| 30 | $label = $this->get_label( $converted, $object ); |
| 31 | $calculated = $this->get_calc( $converted, $object ); |
| 32 | |
| 33 | $value = apply_filters( 'jet-forms-generate-from-query/value', $value, $object, $this->additional ); |
| 34 | $label = apply_filters( 'jet-forms-generate-from-query/label', $label, $object, $this->additional ); |
| 35 | $calculated = apply_filters( 'jet-forms-generate-from-query/calculated', $calculated, $object, $this->additional ); |
| 36 | |
| 37 | $item = array(); |
| 38 | |
| 39 | if ( $value ) { |
| 40 | $item['value'] = $value; |
| 41 | $item['label'] = $label; |
| 42 | } |
| 43 | |
| 44 | if ( $this->calc_field ) { |
| 45 | $item['calculate'] = $calculated; |
| 46 | } |
| 47 | |
| 48 | return $item; |
| 49 | } |
| 50 | |
| 51 | protected function get_converted( $object ) { |
| 52 | // convert to array because in 8.2 dynamic properties will be deprecated |
| 53 | return get_object_vars( $object ); |
| 54 | } |
| 55 | |
| 56 | protected function get_value( $converted, $object ) { |
| 57 | return $converted[ $this->value_field ] ?? false; |
| 58 | } |
| 59 | |
| 60 | protected function get_label( $converted, $object ) { |
| 61 | return $converted[ $this->label_field ] ?? $this->get_value( $converted, $object ); |
| 62 | } |
| 63 | |
| 64 | protected function get_calc( $converted, $object ) { |
| 65 | return $converted[ $this->calc_field ] ?? false; |
| 66 | } |
| 67 | |
| 68 | } |