PluginProbe
The Innovative Form Builder – IvyForms / 1.1
The Innovative Form Builder – IvyForms v1.1
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.1, at backend/src/Factory/Notification/NotificationFactory.php

133 lines 3.8 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 );
34
35 $notificationValueObject = new Notification(
36 $data['id'] ?? 0,
37 $data['name'] ?? '',
38 $contact,
39 $data['enabled'] ?? true,
40 $data['subject'] ?? '',
41 $data['message'] ?? '',
42 $data['showEmptyFields'] ?? false,
43 $smartLogic,
44 $data['formId'] ?? 0
45 );
46
47 $notificationEntity = new NotificationEntity($notificationValueObject);
48
49 if (isset($data['id'])) {
50 $notificationEntity->setId($data['id']);
51 }
52
53 if (isset($data['name'])) {
54 $notificationEntity->setName($data['name']);
55 }
56
57 if (isset($data['sender'])) {
58 $notificationEntity->getNotificationContact()->setSender($data['sender']);
59 }
60
61 if (isset($data['replyTo'])) {
62 $notificationEntity->getNotificationContact()->setReplyTo($data['replyTo']);
63 }
64
65 if (isset($data['receiver'])) {
66 $notificationEntity->getNotificationContact()->setReceiver($data['receiver']);
67 }
68
69 if (isset($data['enabled'])) {
70 $notificationEntity->setEnabled($data['enabled']);
71 }
72
73 if (isset($data['subject'])) {
74 $notificationEntity->setSubject($data['subject']);
75 }
76
77 if (isset($data['message'])) {
78 $notificationEntity->setMessage(wp_unslash($data['message']));
79 }
80
81 if (isset($data['showEmptyFields'])) {
82 $notificationEntity->setShowEmptyFields($data['showEmptyFields']);
83 }
84
85 if (isset($data['smartLogic'])) {
86 $notificationEntity->setSmartLogic($smartLogic);
87 }
88
89 if (isset($data['formId'])) {
90 $notificationEntity->setFormId($data['formId']);
91 }
92
93 return $notificationEntity;
94 }
95
96 /**
97 * Normalize smartLogic from DB/API payload.
98 *
99 * @param mixed $smartLogic
100 * @return array<string, mixed>
101 */
102 private static function normalizeSmartLogic($smartLogic): array
103 {
104 if (is_array($smartLogic)) {
105 $match = $smartLogic['match'] ?? 'any';
106
107 return [
108 'enabled' => (bool)($smartLogic['enabled'] ?? false),
109 'match' => in_array($match, ['any', 'all'], true) ? $match : 'any',
110 'rules' => is_array($smartLogic['rules'] ?? null) ? $smartLogic['rules'] : [],
111 ];
112 }
113
114 if (is_string($smartLogic)) {
115 $decoded = json_decode($smartLogic, true);
116 if (is_array($decoded)) {
117 return self::normalizeSmartLogic($decoded);
118 }
119 return [
120 'enabled' => in_array(strtolower($smartLogic), ['1', 'true'], true),
121 'match' => 'any',
122 'rules' => [],
123 ];
124 }
125
126 return [
127 'enabled' => (bool)$smartLogic,
128 'match' => 'any',
129 'rules' => [],
130 ];
131 }
132 }
133