Beautiful Open-Source Form Builder
Optimize FormController's removed properties calculation for improved performance and refactor PaymentBlockConfigurationRule to implement lazy-loading and caching of Stripe currency codes. These changes enhance efficiency and reduce complexity in property management and currency validation.
Julien Nahum8b67d2ed3262
diff --git a/api/app/Http/Controllers/Forms/FormController.php b/api/app/Http/Controllers/Forms/FormController.phpindex 63e280a94..8c649f03b 100644--- a/api/app/Http/Controllers/Forms/FormController.php+++ b/api/app/Http/Controllers/Forms/FormController.php@@ -142,10 +142,14 @@ class FormController extends Controller->simulateCleaning($form->workspace)->getData();- // Set Removed Properties- $formData['removed_properties'] = array_merge($form->removed_properties, collect($form->properties)->filter(function ($field) use ($formData) {- return !Str::of($field['type'])->startsWith('nf-') && !in_array($field['id'], collect($formData['properties'])->pluck('id')->toArray());- })->toArray());+ // Set Removed Properties (pre-compute lookup set to avoid O(n²) complexity)+ $newPropertyIds = collect($formData['properties'])->pluck('id')->flip()->all();+ $formData['removed_properties'] = array_merge(+ $form->removed_properties,+ collect($form->properties)->filter(function ($field) use ($newPropertyIds) {+ return !Str::of($field['type'])->startsWith('nf-') && !isset($newPropertyIds[$field['id']]);+ })->toArray()+ );$form->slug = (config('app.self_hosted') && !empty($formData['slug'])) ? $formData['slug'] : $form->slug;diff --git a/api/app/Rules/PaymentBlockConfigurationRule.php b/api/app/Rules/PaymentBlockConfigurationRule.phpindex 3ef5439d4..f485649b3 100644--- a/api/app/Rules/PaymentBlockConfigurationRule.php+++ b/api/app/Rules/PaymentBlockConfigurationRule.php@@ -14,6 +14,11 @@ class PaymentBlockConfigurationRule implements ValidationRuleprotected array $field;protected ?Workspace $workspace;+ /**+ * Cached stripe currency codes (static to persist across instances)+ */+ protected static ?array $stripeCurrencyCodes = null;+public function __construct(array $properties, ?Workspace $workspace = null){$this->properties = $properties;@@ -22,14 +27,15 @@ class PaymentBlockConfigurationRule implements ValidationRulepublic function validate(string $attribute, mixed $value, Closure $fail): void{- // Set the field+ // Early bail - $value is the type, skip all processing for non-payment blocks+ if ($value !== 'payment') {+ return;+ }++ // Set the field only for payment blocks$fieldIndex = explode('.', $attribute)[1];$this->field = $this->properties[$fieldIndex];- if ($this->field['type'] !== 'payment') {- return; // If not a payment block, validation passes- }-// Payment block not allowed if self hostedif (config('app.self_hosted')) {$fail('Payment block is not allowed on self hosted. Please use our hosted version.');@@ -46,16 +52,19 @@ class PaymentBlockConfigurationRule implements ValidationRulereturn;}-// Amount validationif (!isset($this->field['amount']) || !is_numeric($this->field['amount']) || $this->field['amount'] < 1) {$fail('Amount must be a number greater than 1');return;}- // Currency validation- $stripeCurrencies = json_decode(file_get_contents(resource_path('data/stripe_currencies.json')), true);- if (!isset($this->field['currency']) || !in_array(strtoupper($this->field['currency']), array_column($stripeCurrencies, 'code'))) {+ // Currency validation (lazy-load and cache currency codes)+ if (self::$stripeCurrencyCodes === null) {+ $stripeCurrencies = json_decode(file_get_contents(resource_path('data/stripe_currencies.json')), true);+ self::$stripeCurrencyCodes = array_column($stripeCurrencies, 'code');+ }++ if (!isset($this->field['currency']) || !in_array(strtoupper($this->field['currency']), self::$stripeCurrencyCodes)) {$fail('Currency must be a valid currency');return;}