PluginProbe
The Innovative Form Builder – IvyForms / 1.4
The Innovative Form Builder – IvyForms v1.4
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
ivyforms / backend / src / Factory / Notification / NotificationFactory.php

NotificationFactory.php in The Innovative Form Builder – IvyForms 1.4, at backend/src/Factory/Notification/NotificationFactory.php

143 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Factory\Notification;
4
5 use IvyForms\Common\Exceptions\InvalidArgumentException;
6 use IvyForms\Entity\Notification\Notification as NotificationEntity;
7 use IvyForms\ValueObjects\Notification\Notification;
8 use IvyForms\ValueObjects\Notification\NotificationContact;
9
10 /**
11 * Class NotificationFactory
12 *
13 * @package IvyForms\Factory\Notification
14 */
15 class NotificationFactory
16 {
17 /**
18 * Create a NotificationEntity from an array of data.
19 *
20 * @param array<string, mixed> $data
21 *
22 * @return NotificationEntity
23 * @throws InvalidArgumentException
24 */
25 public static function create(array $data): NotificationEntity
26 {
27 $smartLogic = self::normalizeSmartLogic($data['smartLogic'] ?? false);
28
29 $contact = new NotificationContact(
30 $data['sender'] ?? '',
31 $data['receiver'] ?? '',
32 $data['replyTo'] ?? '',
33 $data['cc'] ?? '',
34 $data['bcc'] ?? ''
35 );
36
37 $notificationValueObject = new Notification(
38 $data['id'] ?? 0,
39 $data['name'] ?? '',
40 $contact,
41 $data['enabled'] ?? true,
42 $data['subject'] ?? '',
43 $data['message'] ?? '',
44 $data['showEmptyFields'] ?? false,
45 $smartLogic,
46 $data['formId'] ?? 0
47 );
48
49 $notificationEntity = new NotificationEntity($notificationValueObject);
50
51 if (isset($data['id'])) {
52 $notificationEntity->setId($data['id']);
53 }
54
55 if (isset($data['name'])) {
56 $notificationEntity->setName($data['name']);
57 }
58
59 if (isset($data['sender'])) {
60 $notificationEntity->getNotificationContact()->setSender($data['sender']);
61 }
62
63 if (isset($data['replyTo'])) {
64 $notificationEntity->getNotificationContact()->setReplyTo($data['replyTo']);
65 }
66
67 if (isset($data['receiver'])) {
68 $notificationEntity->getNotificationContact()->setReceiver($data['receiver']);
69 }
70
71 if (isset($data['cc'])) {
72 $notificationEntity->getNotificationContact()->setCc($data['cc']);
73 }
74
75 if (isset($data['bcc'])) {
76 $notificationEntity->getNotificationContact()->setBcc($data['bcc']);
77 }
78
79 if (isset($data['enabled'])) {
80 $notificationEntity->setEnabled($data['enabled']);
81 }
82
83 if (isset($data['subject'])) {
84 $notificationEntity->setSubject($data['subject']);
85 }
86
87 if (isset($data['message'])) {
88 $notificationEntity->setMessage(wp_unslash($data['message']));
89 }
90
91 if (isset($data['showEmptyFields'])) {
92 $notificationEntity->setShowEmptyFields($data['showEmptyFields']);
93 }
94
95 if (isset($data['smartLogic'])) {
96 $notificationEntity->setSmartLogic($smartLogic);
97 }
98
99 if (isset($data['formId'])) {
100 $notificationEntity->setFormId($data['formId']);
101 }
102
103 return $notificationEntity;
104 }
105
106 /**
107 * Normalize smartLogic from DB/API payload.
108 *
109 * @param mixed $smartLogic
110 * @return array<string, mixed>
111 */
112 private static function normalizeSmartLogic($smartLogic): array
113 {
114 if (is_array($smartLogic)) {
115 $match = $smartLogic['match'] ?? 'any';
116
117 return [
118 'enabled' => (bool)($smartLogic['enabled'] ?? false),
119 'match' => in_array($match, ['any', 'all'], true) ? $match : 'any',
120 'rules' => is_array($smartLogic['rules'] ?? null) ? $smartLogic['rules'] : [],
121 ];
122 }
123
124 if (is_string($smartLogic)) {
125 $decoded = json_decode($smartLogic, true);
126 if (is_array($decoded)) {
127 return self::normalizeSmartLogic($decoded);
128 }
129 return [
130 'enabled' => in_array(strtolower($smartLogic), ['1', 'true'], true),
131 'match' => 'any',
132 'rules' => [],
133 ];
134 }
135
136 return [
137 'enabled' => (bool)$smartLogic,
138 'match' => 'any',
139 'rules' => [],
140 ];
141 }
142 }
143