| @@ -4,19 +4,22 @@ | ||
| 4 | 4 | |
| 5 | 5 | use FluentForm\App\Services\Parser\Form as FormParser; |
| 6 | 6 | |
| 7 | 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 = []) | |
| 8 | + * Available methods | |
| 9 | + * | |
| 10 | + * @method static array getShortCodeInputs(\stdClass $form, array $with = ['admin_label']) | |
| 11 | + * @method static array getValidations(\stdClass $form, array $inputs, array $fields = []) | |
| 12 | + * @method static array getElement(\stdClass $form, string|array $name, array $with = []) | |
| 13 | + * @method static boolean hasElement(\stdClass $form, string $name) | |
| 14 | + * @method static boolean hasPaymentFields(\stdClass $form) | |
| 15 | + * @method static array getPaymentFields(\stdClass $form, $with = []) | |
| 16 | + * @method static array getPaymentInputFields(\stdClass $form, $with = []) | |
| 17 | + * @method static array getAttachmentInputFields(\stdClass $form, $with = []) | |
| 18 | + * @method static boolean hasRequiredFields(\stdClass $form, array $fields) | |
| 19 | + * @method static array getInputsByElementTypes(\stdClass $form, array $elements, array $with = []) | |
| 20 | + * @method static array|null getField(\stdClass $form, string|array $element, string|array $attribute, array $with = []) | |
| 21 | + * @method static array getEssentialInputs(\stdClass $form, array $inputs, array $with) | |
| 19 | 22 | */ |
| 20 | 23 | class FormFieldsParser |
| 21 | 24 | { |
| 22 | 25 | protected static $forms = []; |
| @@ -24,13 +27,13 @@ | ||
| 24 | 27 | protected static $formsWith = []; |
| 25 | 28 | |
| 26 | 29 | public static function maybeResetForm($form, $with) |
| 27 | 30 | { |
| 28 | - if(!is_object($form) && is_numeric($form)) { | |
| 29 | - $form = wpFluent()->table('fluentform_forms')->find($form); | |
| 31 | + if (!is_object($form) && is_numeric($form)) { | |
| 32 | + $form = \FluentForm\App\Models\Form::find($form); | |
| 30 | 33 | } |
| 31 | 34 | |
| 32 | - if(isset(static::$formsWith[$form->id]) && array_diff(static::$formsWith[$form->id], $with)) { | |
| 35 | + if (isset(static::$formsWith[$form->id]) && array_diff(static::$formsWith[$form->id], $with)) { | |
| 33 | 36 | static::$forms[$form->id] = []; |
| 34 | 37 | } |
| 35 | 38 | static::$formsWith[$form->id] = $with; |
| 36 | 39 | } |
| @@ -53,12 +56,17 @@ | ||
| 53 | 56 | } |
| 54 | 57 | |
| 55 | 58 | public static function parse($key, $form, $with) |
| 56 | 59 | { |
| 57 | - if(!is_object($form) && is_numeric($form)) { | |
| 60 | + if (!is_object($form) && is_numeric($form)) { | |
| 58 | 61 | $form = wpFluent()->table('fluentform_forms')->find($form); |
| 59 | 62 | } |
| 60 | 63 | |
| 64 | + // Return empty array if form is still not found or is null | |
| 65 | + if (!$form || !is_object($form)) { | |
| 66 | + return []; | |
| 67 | + } | |
| 68 | + | |
| 61 | 69 | if (!isset(static::$forms[$form->id])) { |
| 62 | 70 | static::$forms[$form->id] = []; |
| 63 | 71 | } |
| 64 | 72 | |
| @@ -65,9 +73,9 @@ | ||
| 65 | 73 | if (!isset(static::$forms[$form->id][$key])) { |
| 66 | 74 | $parser = new FormParser($form); |
| 67 | 75 | $method = str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); |
| 68 | 76 | |
| 69 | - static::$forms[$form->id][$key] = $parser->{'get'.$method}($with); | |
| 77 | + static::$forms[$form->id][$key] = $parser->{'get' . $method}($with); | |
| 70 | 78 | } |
| 71 | 79 | |
| 72 | 80 | return static::$forms[$form->id][$key]; |
| 73 | 81 | } |
| @@ -81,9 +89,9 @@ | ||
| 81 | 89 | if (!isset(static::$forms[$form->id]['admin_labels'])) { |
| 82 | 90 | $parser = new FormParser($form); |
| 83 | 91 | static::$forms[$form->id]['admin_labels'] = $parser->getAdminLabels($fields); |
| 84 | 92 | } |
| 85 | - | |
| 93 | + | |
| 86 | 94 | return static::$forms[$form->id]['admin_labels']; |
| 87 | 95 | } |
| 88 | 96 | |
| 89 | 97 | /** |
| @@ -89,10 +97,11 @@ | ||
| 89 | 97 | /** |
| 90 | 98 | * Deligate dynamic static method calls to FormParser method. |
| 91 | 99 | * And set the result to the store before returning to dev. |
| 92 | 100 | * |
| 93 | - * @param string $method | |
| 94 | - * @param array $parameters | |
| 101 | + * @param string $method | |
| 102 | + * @param array $parameters | |
| 103 | + * | |
| 95 | 104 | * @return mixed |
| 96 | 105 | */ |
| 97 | 106 | public static function __callStatic($method, $parameters) |
| 98 | 107 | { |
| @@ -98,12 +107,19 @@ | ||
| 98 | 107 | { |
| 99 | 108 | // The first item of the parameters is expected to contain the form object. |
| 100 | 109 | $form = array_shift($parameters); |
| 101 | 110 | |
| 111 | + $forceFreshValue = [ | |
| 112 | + 'getField', | |
| 113 | + 'getElement', | |
| 114 | + 'hasElement', | |
| 115 | + 'getInputsByElementTypes', | |
| 116 | + ]; | |
| 117 | + | |
| 102 | 118 | // If the store doesn't have the requested result we'll |
| 103 | 119 | // deletegate the method call to the Parser method. |
| 104 | 120 | // Set the store before returning it to the dev. |
| 105 | - if (!isset(static::$forms[$form->id][$method])) { | |
| 121 | + if (in_array($method, $forceFreshValue) || !isset(static::$forms[$form->id][$method])) { | |
| 106 | 122 | $parser = new FormParser($form); |
| 107 | 123 | |
| 108 | 124 | static::$forms[$form->id][$method] = call_user_func_array([$parser, $method], $parameters); |
| 109 | 125 | } |
| @@ -108,6 +124,12 @@ | ||
| 108 | 124 | static::$forms[$form->id][$method] = call_user_func_array([$parser, $method], $parameters); |
| 109 | 125 | } |
| 110 | 126 | |
| 111 | 127 | return static::$forms[$form->id][$method]; |
| 128 | + } | |
| 129 | + | |
| 130 | + public static function resetData() | |
| 131 | + { | |
| 132 | + static::$forms = []; | |
| 133 | + static::$formsWith = []; | |
| 112 | 134 | } |
| 113 | 135 | } |