| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\ValueObjects\Notification; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 11 |
use IvyForms\Common\Exceptions\ValidationException; |
| 12 |
use IvyForms\Services\Translations\BackendStrings; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Notification |
| 16 |
* |
| 17 |
* @package IvyForms\ValueObjects\Notification |
| 18 |
*/ |
| 19 |
final class Notification |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var int |
| 23 |
*/ |
| 24 |
public int $id; |
| 25 |
/** |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
public string $name; |
| 29 |
/** |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
public string $sender; |
| 33 |
/** |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
public string $replyTo; |
| 37 |
/** |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
public string $receiver; |
| 41 |
/** |
| 42 |
* @var bool |
| 43 |
*/ |
| 44 |
public bool $enabled; |
| 45 |
/** |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
public string $subject; |
| 49 |
/** |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
public string $message; |
| 53 |
/** |
| 54 |
* @var bool |
| 55 |
*/ |
| 56 |
public bool $smartLogic; |
| 57 |
/** |
| 58 |
* @var int |
| 59 |
*/ |
| 60 |
public int $formId; |
| 61 |
|
| 62 |
/** |
| 63 |
* Form constructor. |
| 64 |
* |
| 65 |
* @param int $id |
| 66 |
* @param string $name |
| 67 |
* @param string $sender |
| 68 |
* @param string $replyTo |
| 69 |
* @param string $receiver |
| 70 |
* @param bool $enabled |
| 71 |
* @param string $subject |
| 72 |
* @param string $message |
| 73 |
* @param bool $smartLogic |
| 74 |
* @param int $formId |
| 75 |
* @throws InvalidArgumentException|ValidationException |
| 76 |
*/ |
| 77 |
public function __construct( |
| 78 |
int $id, |
| 79 |
string $name, |
| 80 |
string $sender, |
| 81 |
string $replyTo, |
| 82 |
string $receiver, |
| 83 |
bool $enabled, |
| 84 |
string $subject, |
| 85 |
string $message, |
| 86 |
bool $smartLogic, |
| 87 |
int $formId |
| 88 |
) { |
| 89 |
$this->id = $this->validateId($id); |
| 90 |
$this->name = $this->validateString($name, 255, 'name'); |
| 91 |
$this->sender = $this->validateString($sender, 255, 'sender'); |
| 92 |
$this->replyTo = $this->validateString($replyTo, 255, 'replyTo'); |
| 93 |
$this->receiver = $this->validateString($receiver, 255, 'receiver'); |
| 94 |
$this->enabled = $this->validateBool($enabled); |
| 95 |
$this->subject = $this->validateString($subject, 255, 'subject'); |
| 96 |
$this->message = $this->validateString($message, 1000, 'message'); |
| 97 |
$this->smartLogic = $this->validateBool($smartLogic); |
| 98 |
$this->formId = $this->validateId($formId); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Validates the ID. |
| 103 |
* |
| 104 |
* @param int $id |
| 105 |
* |
| 106 |
* @return int |
| 107 |
* @throws InvalidArgumentException |
| 108 |
*/ |
| 109 |
private function validateId(int $id): int |
| 110 |
{ |
| 111 |
if ($id < 0) { |
| 112 |
throw new InvalidArgumentException(BackendStrings::getExceptionStrings()['id_positive_integer']); |
| 113 |
} |
| 114 |
return $id; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Validates a string value. |
| 119 |
* |
| 120 |
* @param string $value |
| 121 |
* @param int $maxLength |
| 122 |
* @param string $fieldName |
| 123 |
* |
| 124 |
* @return string |
| 125 |
* |
| 126 |
* @throws ValidationException |
| 127 |
*/ |
| 128 |
private function validateString(string $value, int $maxLength, string $fieldName): string |
| 129 |
{ |
| 130 |
if (strlen($value) > $maxLength) { |
| 131 |
throw new ValidationException( |
| 132 |
sprintf( |
| 133 |
/* translators: 1: String value, 2: String max length. */ |
| 134 |
esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'), |
| 135 |
esc_html($fieldName), |
| 136 |
$maxLength |
| 137 |
) |
| 138 |
); |
| 139 |
} |
| 140 |
return $value; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Validates a boolean field. |
| 145 |
* |
| 146 |
* @param bool $value |
| 147 |
* |
| 148 |
* @return bool |
| 149 |
*/ |
| 150 |
private function validateBool(bool $value): bool |
| 151 |
{ |
| 152 |
return $value; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Get the notification ID. |
| 157 |
* |
| 158 |
* @return int |
| 159 |
*/ |
| 160 |
public function getId(): int |
| 161 |
{ |
| 162 |
return $this->id; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get the notification name. |
| 167 |
* |
| 168 |
* @return string |
| 169 |
*/ |
| 170 |
public function getName(): string |
| 171 |
{ |
| 172 |
return $this->name; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Get the notification sender. |
| 177 |
* |
| 178 |
* @return string |
| 179 |
*/ |
| 180 |
public function getSender(): string |
| 181 |
{ |
| 182 |
return $this->sender; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get the notification reply-to address. |
| 187 |
* |
| 188 |
* @return string |
| 189 |
*/ |
| 190 |
public function getReplyTo(): string |
| 191 |
{ |
| 192 |
return $this->replyTo; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Get the notification receiver. |
| 197 |
* |
| 198 |
* @return string |
| 199 |
*/ |
| 200 |
public function getReceiver(): string |
| 201 |
{ |
| 202 |
return $this->receiver; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get the notification status. |
| 207 |
* |
| 208 |
* @return bool |
| 209 |
*/ |
| 210 |
public function isEnabled(): bool |
| 211 |
{ |
| 212 |
return $this->enabled; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Get the notification subject. |
| 217 |
* |
| 218 |
* @return string |
| 219 |
*/ |
| 220 |
public function getSubject(): string |
| 221 |
{ |
| 222 |
return $this->subject; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Get the notification message. |
| 227 |
* |
| 228 |
* @return string |
| 229 |
*/ |
| 230 |
public function getMessage(): string |
| 231 |
{ |
| 232 |
return $this->message; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Get the smart logic status. |
| 237 |
* |
| 238 |
* @return bool |
| 239 |
*/ |
| 240 |
public function isSmartLogic(): bool |
| 241 |
{ |
| 242 |
return $this->smartLogic; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Get the form ID associated with the notification. |
| 247 |
* |
| 248 |
* @return int |
| 249 |
*/ |
| 250 |
public function getFormId(): int |
| 251 |
{ |
| 252 |
return $this->formId; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Convert the notification to an array. |
| 257 |
* |
| 258 |
* @return array<string, mixed> |
| 259 |
*/ |
| 260 |
public function toArray(): array |
| 261 |
{ |
| 262 |
return [ |
| 263 |
'id' => $this->getId(), |
| 264 |
'name' => $this->getName(), |
| 265 |
'sender' => $this->getSender(), |
| 266 |
'replyTo' => $this->getReplyTo(), |
| 267 |
'receiver' => $this->getReceiver(), |
| 268 |
'enabled' => $this->isEnabled(), |
| 269 |
'subject' => $this->getSubject(), |
| 270 |
'message' => $this->getMessage(), |
| 271 |
'smartLogic' => $this->isSmartLogic(), |
| 272 |
'formId' => $this->getFormId(), |
| 273 |
]; |
| 274 |
} |
| 275 |
} |
| 276 |
|