Skip to content

Commit

Permalink
Added Render fields classes
Browse files Browse the repository at this point in the history
  • Loading branch information
girafffee committed Apr 1, 2021
1 parent a8f49f6 commit 6f68d5a
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/BaseRenderField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php


namespace JFBCore;


trait BaseRenderField {

public $args;
public $args_map;

abstract public function set_up( ...$args );

abstract public function render_field( $attrs_string );

/**
* It used in JetFormBuilder Render\Base for naming field template
* And in JetEngine & jetFormBuilder it used for naming wp.hook
*
* @return string
*/
abstract public function get_name();

public function attributes_values() {
return array();
}

public function attributes_map() {
return array();
}

public function args_map() {
return array();
}

private function default_args_map() {
$this->args_map = array_merge_recursive( array(
'required' => '',
'name' => 'field_name',
'default' => ''
), $this->args_map() );
}

final public function get_arg( $key ) {
return ( isset( $this->args[ $key ] ) ? $this->args[ $key ] : $this->args_map[ $key ] );
}

private function save_attributes() {
$attributes = apply_filters( "jet-fb/attributes/{$this->get_name()}", $this->get_attributes_map() );

foreach ( $attributes as $name => $value ) {
$this->add_attribute( $name, $value );
}
}

final public function get_attributes_map() {
return array_merge_recursive( $this->attributes_map(), $this->attributes_values() );
}

final public function get_rendered() {
$this->default_args_map();
$this->save_attributes();

return $this->render_field( $this->get_attributes_string_save() );
}

public function get_args( $args_names ) {
$response = array();

foreach ( $args_names as $args_name ) {
$response[ $args_name ] = $this->get_arg( $args_name );
}

return $response;
}
}
34 changes: 34 additions & 0 deletions src/JetEngine/RenderField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php


namespace JFBCore\JetEngine;


use JFBCore\AttributesTrait;
use JFBCore\BaseRenderField;

trait RenderField {

use AttributesTrait;
use BaseRenderField;

private $builder;

public function set_up( ...$args ) {
$this->args = $args[0];
$this->builder = $args[1];

return $this;
}

public function attributes_map() {
return array(
'class' => array( 'jet-form__field' ),
'required' => $this->builder->get_required_val( $this->args ),
'name' => $this->builder->get_field_name( $this->get_arg( 'name' ) ),
'id' => $this->builder->get_field_id( $this->args ),
'data-field-name' => $this->get_arg( 'name' ),
'value' => $this->get_arg( 'default' ),
);
}
}
33 changes: 33 additions & 0 deletions src/JetFormBuilder/RenderBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php


namespace JFBCore\JetFormBuilder;




use JFBCore\BaseRenderField;

trait RenderBlock {

use BaseRenderField;

public function set_up( ...$args ) {
$this->args = $this->block_type->block_attrs;

return $this;
}

public function attributes_map() {
return array(
'class' => array( 'jet-form-builder__field' ),
'required' => $this->block_type->get_required_val( $this->args ),
'name' => $this->block_type->get_field_name( $this->get_arg( 'name' ) ),
'id' => $this->block_type->get_field_id( $this->args ),
'data-field-name' => $this->get_arg( 'name' ),
'value' => $this->get_arg( 'default' ),
);
}


}

0 comments on commit 6f68d5a

Please sign in to comment.