| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\ValueObjects\GoogleSheets; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use IvyForms\Common\Exceptions\ValidationException; |
| 7 |
|
| 8 |
/** |
| 9 |
* Google Sheet integration value object. |
| 10 |
*/ |
| 11 |
final class GoogleSheetIntegration |
| 12 |
{ |
| 13 |
public ?int $id; |
| 14 |
public int $formId; |
| 15 |
public string $spreadsheetId; |
| 16 |
public string $spreadsheetName; |
| 17 |
public string $worksheetName; |
| 18 |
/** @var list<array{column: string, value: string}> */ |
| 19 |
public array $fieldMapping; |
| 20 |
/** @var array<string, mixed> */ |
| 21 |
public array $smartLogic; |
| 22 |
public bool $enabled; |
| 23 |
public ?string $lastStatus; |
| 24 |
public ?DateTime $lastRunAt; |
| 25 |
public ?DateTime $createdAt; |
| 26 |
public ?DateTime $updatedAt; |
| 27 |
|
| 28 |
/** |
| 29 |
* @param list<array{column: string, value: string}> $fieldMapping |
| 30 |
* @param array<string, mixed> $smartLogic |
| 31 |
* @throws ValidationException |
| 32 |
* |
| 33 |
* @SuppressWarnings("ExcessiveParameterList") |
| 34 |
*/ |
| 35 |
public function __construct( |
| 36 |
?int $id, |
| 37 |
int $formId, |
| 38 |
string $spreadsheetId, |
| 39 |
string $spreadsheetName, |
| 40 |
string $worksheetName, |
| 41 |
array $fieldMapping, |
| 42 |
array $smartLogic, |
| 43 |
bool $enabled, |
| 44 |
?string $lastStatus, |
| 45 |
?DateTime $lastRunAt, |
| 46 |
?DateTime $createdAt = null, |
| 47 |
?DateTime $updatedAt = null |
| 48 |
) { |
| 49 |
$this->id = $id; |
| 50 |
$this->formId = $this->validateFormId($formId); |
| 51 |
$this->spreadsheetId = $this->validateRequiredString($spreadsheetId, 'spreadsheet_id', 255); |
| 52 |
$this->spreadsheetName = $spreadsheetName; |
| 53 |
$this->worksheetName = $this->validateRequiredString($worksheetName, 'worksheet_name', 255); |
| 54 |
$this->fieldMapping = $fieldMapping; |
| 55 |
$this->smartLogic = $smartLogic; |
| 56 |
$this->enabled = $enabled; |
| 57 |
$this->lastStatus = $lastStatus; |
| 58 |
$this->lastRunAt = $lastRunAt; |
| 59 |
$this->createdAt = $createdAt ?? new DateTime(); |
| 60 |
$this->updatedAt = $updatedAt ?? new DateTime(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @throws ValidationException |
| 65 |
*/ |
| 66 |
private function validateFormId(int $formId): int |
| 67 |
{ |
| 68 |
if ($formId <= 0) { |
| 69 |
throw new ValidationException(__('Form ID must be a positive integer.', 'ivyforms')); |
| 70 |
} |
| 71 |
|
| 72 |
return $formId; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @throws ValidationException |
| 77 |
*/ |
| 78 |
private function validateRequiredString(string $value, string $fieldName, int $maxLength): string |
| 79 |
{ |
| 80 |
$value = trim($value); |
| 81 |
|
| 82 |
if ($value === '') { |
| 83 |
throw new ValidationException( |
| 84 |
sprintf( |
| 85 |
/* translators: %s: field name */ |
| 86 |
__('%s cannot be empty.', 'ivyforms'), |
| 87 |
$fieldName |
| 88 |
) |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
if (strlen($value) > $maxLength) { |
| 93 |
throw new ValidationException( |
| 94 |
sprintf( |
| 95 |
/* translators: 1: field name, 2: max length */ |
| 96 |
__('%1$s must be at most %2$d characters.', 'ivyforms'), |
| 97 |
$fieldName, |
| 98 |
$maxLength |
| 99 |
) |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
return $value; |
| 104 |
} |
| 105 |
|
| 106 |
public function getId(): ?int |
| 107 |
{ |
| 108 |
return $this->id; |
| 109 |
} |
| 110 |
|
| 111 |
public function getFormId(): int |
| 112 |
{ |
| 113 |
return $this->formId; |
| 114 |
} |
| 115 |
|
| 116 |
public function getSpreadsheetId(): string |
| 117 |
{ |
| 118 |
return $this->spreadsheetId; |
| 119 |
} |
| 120 |
|
| 121 |
public function getSpreadsheetName(): string |
| 122 |
{ |
| 123 |
return $this->spreadsheetName; |
| 124 |
} |
| 125 |
|
| 126 |
public function getWorksheetName(): string |
| 127 |
{ |
| 128 |
return $this->worksheetName; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @return list<array{column: string, value: string}> |
| 133 |
*/ |
| 134 |
public function getFieldMapping(): array |
| 135 |
{ |
| 136 |
return $this->fieldMapping; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* @return array<string, mixed> |
| 141 |
*/ |
| 142 |
public function getSmartLogic(): array |
| 143 |
{ |
| 144 |
return $this->smartLogic; |
| 145 |
} |
| 146 |
|
| 147 |
public function isEnabled(): bool |
| 148 |
{ |
| 149 |
return $this->enabled; |
| 150 |
} |
| 151 |
|
| 152 |
public function getLastStatus(): ?string |
| 153 |
{ |
| 154 |
return $this->lastStatus; |
| 155 |
} |
| 156 |
|
| 157 |
public function getLastRunAt(): ?DateTime |
| 158 |
{ |
| 159 |
return $this->lastRunAt; |
| 160 |
} |
| 161 |
|
| 162 |
public function getCreatedAt(): ?DateTime |
| 163 |
{ |
| 164 |
return $this->createdAt; |
| 165 |
} |
| 166 |
|
| 167 |
public function getUpdatedAt(): ?DateTime |
| 168 |
{ |
| 169 |
return $this->updatedAt; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* @return array<string, mixed> |
| 174 |
*/ |
| 175 |
public function toArray(): array |
| 176 |
{ |
| 177 |
return [ |
| 178 |
'id' => $this->getId(), |
| 179 |
'form_id' => $this->getFormId(), |
| 180 |
'spreadsheet_id' => $this->getSpreadsheetId(), |
| 181 |
'spreadsheet_name' => $this->getSpreadsheetName(), |
| 182 |
'worksheet_name' => $this->getWorksheetName(), |
| 183 |
'field_mapping' => $this->getFieldMapping(), |
| 184 |
'smart_logic' => $this->getSmartLogic(), |
| 185 |
'enabled' => $this->isEnabled(), |
| 186 |
'last_status' => $this->getLastStatus(), |
| 187 |
'last_run_at' => $this->getLastRunAt() ? $this->getLastRunAt()->format('Y-m-d H:i:s') : null, |
| 188 |
'created_at' => $this->getCreatedAt() ? $this->getCreatedAt()->format('Y-m-d H:i:s') : null, |
| 189 |
'updated_at' => $this->getUpdatedAt() ? $this->getUpdatedAt()->format('Y-m-d H:i:s') : null, |
| 190 |
]; |
| 191 |
} |
| 192 |
} |
| 193 |
|