| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Factory\Form; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use IvyForms\Common\Exceptions\ValidationException; |
| 16 |
use IvyForms\Common\Helpers\Helper; |
| 17 |
use IvyForms\Entity\Form\Form as FormEntity; |
| 18 |
use IvyForms\ValueObjects\Form\Form; |
| 19 |
use IvyForms\ValueObjects\Form\IntegrationSettings; |
| 20 |
use IvyForms\Services\Translations\BackendStrings; |
| 21 |
|
| 22 |
/** |
| 23 |
* Class FormFactory |
| 24 |
* |
| 25 |
* @package IvyForms\Factory\Form |
| 26 |
*/ |
| 27 |
class FormFactory |
| 28 |
{ |
| 29 |
/** |
| 30 |
* @param array<string, mixed> $data |
| 31 |
* |
| 32 |
* @return FormEntity |
| 33 |
* |
| 34 |
* @throws ValidationException |
| 35 |
*/ |
| 36 |
public static function create(array $data): FormEntity |
| 37 |
{ |
| 38 |
$settings = isset($data['settings']) ? json_decode($data['settings'], true) : []; |
| 39 |
$integrationSettings = Helper::parseToArray($data['integrationSettings']); |
| 40 |
|
| 41 |
if (!empty($settings)) { |
| 42 |
$data['showTitle'] = $settings['showTitle'] ?? true; |
| 43 |
$data['showDescription'] = $settings['showDescription'] ?? false; |
| 44 |
$data['storeEntries'] = !isset($settings['storeEntries']) || (bool)$settings['storeEntries']; |
| 45 |
$data['formActionButtons'] = $settings['formActionButtons'] ?? [ |
| 46 |
'submitButtonSettings' => [ |
| 47 |
'label' => BackendStrings::getCommonStrings()['submit'], |
| 48 |
'position' => 'default' |
| 49 |
] |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
$formObject = new Form( |
| 54 |
$data['id'] ?? 0, |
| 55 |
$data['name'] ?? '', |
| 56 |
$data['author'] ?? '', |
| 57 |
$data['starred'] ?? false, |
| 58 |
$data['published'] ?? true, |
| 59 |
$data['description'] ?? '', |
| 60 |
$data['showTitle'] ?? true, |
| 61 |
$data['showDescription'] ?? false, |
| 62 |
isset($data['storeEntries']) ? (bool)$data['storeEntries'] : true, |
| 63 |
$data['fields'] ?? [], |
| 64 |
new IntegrationSettings($integrationSettings), |
| 65 |
$data['formActionButtons'] |
| 66 |
); |
| 67 |
|
| 68 |
$form = new FormEntity($formObject); |
| 69 |
|
| 70 |
if (isset($data['id'])) { |
| 71 |
$form->setId($data['id']); |
| 72 |
} |
| 73 |
|
| 74 |
if (isset($data['name'])) { |
| 75 |
$form->setName($data['name']); |
| 76 |
} |
| 77 |
|
| 78 |
if (isset($data['author'])) { |
| 79 |
$form->setAuthor($data['author']); |
| 80 |
} |
| 81 |
|
| 82 |
if (isset($data['starred'])) { |
| 83 |
$form->setStarred($data['starred']); |
| 84 |
} |
| 85 |
|
| 86 |
if (isset($data['published'])) { |
| 87 |
$form->setPublished($data['published']); |
| 88 |
} |
| 89 |
|
| 90 |
if (isset($data['description'])) { |
| 91 |
$form->setDescription($data['description']); |
| 92 |
} |
| 93 |
|
| 94 |
if (isset($data['fields'])) { |
| 95 |
$form->setFields($data['fields']); |
| 96 |
} |
| 97 |
|
| 98 |
if (isset($data['showTitle'])) { |
| 99 |
$form->setTitleVisible($data['showTitle']); |
| 100 |
} |
| 101 |
|
| 102 |
if (isset($data['showDescription'])) { |
| 103 |
$form->setDescriptionVisible($data['showDescription']); |
| 104 |
} |
| 105 |
|
| 106 |
if (isset($data['storeEntries'])) { |
| 107 |
$form->setStoreEntries($data['storeEntries']); |
| 108 |
} |
| 109 |
|
| 110 |
return $form; |
| 111 |
} |
| 112 |
} |
| 113 |
|