PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
← All changes | backend/src/Factory/Confirmation/ConfirmationFactory.php +136 -0 0.6.1 → 1.5 View file →
@@ -1,0 +1,136 @@
1 +<?php
2 +
3 +namespace IvyForms\Factory\Confirmation;
4 +
5 +use IvyForms\Common\Exceptions\InvalidArgumentException;
6 +use IvyForms\Common\Exceptions\ValidationException;
7 +use IvyForms\Entity\Confirmation\Confirmation as ConfirmationEntity;
8 +use IvyForms\Services\Translations\BackendStrings;
9 +use IvyForms\ValueObjects\Confirmation\Confirmation;
10 +
11 +/**
12 + * Class ConfirmationFactory
13 + *
14 + * @package IvyForms\Factory\Confirmation
15 + */
16 +class ConfirmationFactory
17 +{
18 + /**
19 + * Create a ConfirmationEntity from an array of data.
20 + *
21 + * @param array<string, mixed> $data
22 + *
23 + * @return ConfirmationEntity
24 + * @throws InvalidArgumentException
25 + * @throws ValidationException
26 + */
27 + public static function create(array $data): ConfirmationEntity
28 + {
29 + $smartLogic = self::normalizeSmartLogic($data['smartLogic'] ?? false);
30 +
31 + $confirmationValueObject = new Confirmation(
32 + $data['id'] ?? 0,
33 + $data['formId'] ?? 0,
34 + $data['name'] ?? BackendStrings::getSettingsFormBuilderStrings()['default_confirmation_name'],
35 + $data['type'] ?? '',
36 + $data['enabled'] ?? true,
37 + $data['showForm'] ?? false,
38 + $data['message'] ?? '',
39 + $data['url'] ?? '',
40 + $data['page'] ?? '',
41 + (int) ($data['position'] ?? 0),
42 + (bool) ($data['isDefault'] ?? false),
43 + $smartLogic
44 + );
45 +
46 + $confirmationEntity = new ConfirmationEntity($confirmationValueObject);
47 +
48 + if (isset($data['id'])) {
49 + $confirmationEntity->setId($data['id']);
50 + }
51 +
52 + if (isset($data['formId'])) {
53 + $confirmationEntity->setFormId($data['formId']);
54 + }
55 +
56 + if (isset($data['type'])) {
57 + $confirmationEntity->setType($data['type']);
58 + }
59 +
60 + if (isset($data['enabled'])) {
61 + $confirmationEntity->setEnabled($data['enabled']);
62 + }
63 +
64 + if (isset($data['showForm'])) {
65 + $confirmationEntity->setShowForm($data['showForm']);
66 + }
67 +
68 + if (isset($data['message'])) {
69 + $confirmationEntity->setMessage(wp_unslash($data['message']));
70 + }
71 +
72 + if (isset($data['url'])) {
73 + $confirmationEntity->setUrl($data['url']);
74 + }
75 +
76 + if (isset($data['page'])) {
77 + $confirmationEntity->setPage($data['page']);
78 + }
79 +
80 + if (isset($data['name'])) {
81 + $confirmationEntity->setName($data['name']);
82 + }
83 +
84 + if (isset($data['position'])) {
85 + $confirmationEntity->setPosition((int) $data['position']);
86 + }
87 +
88 + if (isset($data['isDefault'])) {
89 + $confirmationEntity->setIsDefault((bool) $data['isDefault']);
90 + }
91 +
92 + if (isset($data['smartLogic'])) {
93 + $confirmationEntity->setSmartLogic($smartLogic);
94 + }
95 +
96 + return $confirmationEntity;
97 + }
98 +
99 + /**
100 + * Normalize smartLogic from DB/API payload.
101 + *
102 + * @param mixed $smartLogic
103 + * @return array<string, mixed>
104 + */
105 + private static function normalizeSmartLogic($smartLogic): array
106 + {
107 + if (is_array($smartLogic)) {
108 + $match = $smartLogic['match'] ?? 'any';
109 +
110 + return [
111 + 'enabled' => (bool)($smartLogic['enabled'] ?? false),
112 + 'match' => in_array($match, ['any', 'all'], true) ? $match : 'any',
113 + 'rules' => is_array($smartLogic['rules'] ?? null) ? $smartLogic['rules'] : [],
114 + ];
115 + }
116 +
117 + if (is_string($smartLogic)) {
118 + $decoded = json_decode($smartLogic, true);
119 + if (is_array($decoded)) {
120 + return self::normalizeSmartLogic($decoded);
121 + }
122 +
123 + return [
124 + 'enabled' => in_array(strtolower($smartLogic), ['1', 'true'], true),
125 + 'match' => 'any',
126 + 'rules' => [],
127 + ];
128 + }
129 +
130 + return [
131 + 'enabled' => (bool)$smartLogic,
132 + 'match' => 'any',
133 + 'rules' => [],
134 + ];
135 + }
136 +}