| 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 |
|