| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Form; |
| 4 |
|
| 5 |
use FluentForm\App\Services\Parser\Form as FormParser; |
| 6 |
|
| 7 |
/** |
| 8 |
* @method array getShortCodeInputs(\stdClass $form, array $with = ['admin_label']) |
| 9 |
* @method array getValidations(\stdClass $form, array $inputs, array $fields = []) |
| 10 |
* @method array getElement(\stdClass $form, string $name, array $with = []) |
| 11 |
* @method boolean hasElement(\stdClass $form, string $name) |
| 12 |
* @method boolean hasPaymentFields(\stdClass $form) |
| 13 |
* @method array getPaymentFields(\stdClass $form, $with = []) |
| 14 |
* @method array getPaymentInputFields(\stdClass $form, $with = []) |
| 15 |
* @method array getAttachmentInputFields(\stdClass $form, $with = []) |
| 16 |
* @method boolean hasRequiredFields(\stdClass $form, array $fields) |
| 17 |
* @method array getInputsByElementTypes(\stdClass $form, array $elements) |
| 18 |
* @method array|null getField(\stdClass $form, string|array $element, string|array $attribute, array $with = []) |
| 19 |
*/ |
| 20 |
class FormFieldsParser |
| 21 |
{ |
| 22 |
protected static $forms = []; |
| 23 |
|
| 24 |
protected static $formsWith = []; |
| 25 |
|
| 26 |
public static function maybeResetForm($form, $with) |
| 27 |
{ |
| 28 |
if(!is_object($form) && is_numeric($form)) { |
| 29 |
$form = wpFluent()->table('fluentform_forms')->find($form); |
| 30 |
} |
| 31 |
|
| 32 |
if(isset(static::$formsWith[$form->id]) && array_diff(static::$formsWith[$form->id], $with)) { |
| 33 |
static::$forms[$form->id] = []; |
| 34 |
} |
| 35 |
static::$formsWith[$form->id] = $with; |
| 36 |
} |
| 37 |
|
| 38 |
public static function getFields($form, $asArray = false) |
| 39 |
{ |
| 40 |
return static::parse('fields', $form, $asArray); |
| 41 |
} |
| 42 |
|
| 43 |
public static function getInputs($form, $with = []) |
| 44 |
{ |
| 45 |
static::maybeResetForm($form, $with); |
| 46 |
return static::parse('inputs', $form, $with); |
| 47 |
} |
| 48 |
|
| 49 |
public static function getEntryInputs($form, $with = ['admin_label', 'raw']) |
| 50 |
{ |
| 51 |
static::maybeResetForm($form, $with); |
| 52 |
return static::parse('entry_inputs', $form, $with); |
| 53 |
} |
| 54 |
|
| 55 |
public static function parse($key, $form, $with) |
| 56 |
{ |
| 57 |
if(!is_object($form) && is_numeric($form)) { |
| 58 |
$form = wpFluent()->table('fluentform_forms')->find($form); |
| 59 |
} |
| 60 |
|
| 61 |
if (!isset(static::$forms[$form->id])) { |
| 62 |
static::$forms[$form->id] = []; |
| 63 |
} |
| 64 |
|
| 65 |
if (!isset(static::$forms[$form->id][$key])) { |
| 66 |
$parser = new FormParser($form); |
| 67 |
$method = str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); |
| 68 |
|
| 69 |
static::$forms[$form->id][$key] = $parser->{'get'.$method}($with); |
| 70 |
} |
| 71 |
|
| 72 |
return static::$forms[$form->id][$key]; |
| 73 |
} |
| 74 |
|
| 75 |
public static function getAdminLabels($form, $fields = []) |
| 76 |
{ |
| 77 |
if (!isset(static::$forms[$form->id])) { |
| 78 |
static::$forms[$form->id] = []; |
| 79 |
} |
| 80 |
|
| 81 |
if (!isset(static::$forms[$form->id]['admin_labels'])) { |
| 82 |
$parser = new FormParser($form); |
| 83 |
static::$forms[$form->id]['admin_labels'] = $parser->getAdminLabels($fields); |
| 84 |
} |
| 85 |
|
| 86 |
return static::$forms[$form->id]['admin_labels']; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Deligate dynamic static method calls to FormParser method. |
| 91 |
* And set the result to the store before returning to dev. |
| 92 |
* |
| 93 |
* @param string $method |
| 94 |
* @param array $parameters |
| 95 |
* @return mixed |
| 96 |
*/ |
| 97 |
public static function __callStatic($method, $parameters) |
| 98 |
{ |
| 99 |
// The first item of the parameters is expected to contain the form object. |
| 100 |
$form = array_shift($parameters); |
| 101 |
|
| 102 |
// If the store doesn't have the requested result we'll |
| 103 |
// deletegate the method call to the Parser method. |
| 104 |
// Set the store before returning it to the dev. |
| 105 |
if (!isset(static::$forms[$form->id][$method])) { |
| 106 |
$parser = new FormParser($form); |
| 107 |
|
| 108 |
static::$forms[$form->id][$method] = call_user_func_array([$parser, $method], $parameters); |
| 109 |
} |
| 110 |
|
| 111 |
return static::$forms[$form->id][$method]; |
| 112 |
} |
| 113 |
} |
| 114 |
|