| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Factory\EntryField; |
| 4 |
|
| 5 |
use IvyForms\Common\Exceptions\ValidationException; |
| 6 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 7 |
use IvyForms\Entity\EntryField\EntryField as EntryFieldEntity; |
| 8 |
use IvyForms\ValueObjects\Entry\EntryField as EntryFieldValueObject; |
| 9 |
|
| 10 |
class EntryFieldFactory |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Create an EntryField entity from array data. |
| 14 |
* |
| 15 |
* @param array<mixed> $data |
| 16 |
* @return EntryFieldEntity |
| 17 |
* @throws ValidationException |
| 18 |
*/ |
| 19 |
public static function create(array $data): EntryFieldEntity |
| 20 |
{ |
| 21 |
$normalizedFieldValue = Sanitizer::normalizeFieldValue($data['fieldValue'] ?? ''); |
| 22 |
$entryFieldObject = new EntryFieldValueObject( |
| 23 |
$data['id'] ?? null, |
| 24 |
$data['entryId'] ?? null, |
| 25 |
$data['fieldId'] ?? '', |
| 26 |
$normalizedFieldValue |
| 27 |
); |
| 28 |
|
| 29 |
$entryField = new EntryFieldEntity($entryFieldObject); |
| 30 |
|
| 31 |
if (isset($data['id'])) { |
| 32 |
$entryField->setId($data['id']); |
| 33 |
} |
| 34 |
if (isset($data['entryId'])) { |
| 35 |
$entryField->setEntryId($data['entryId']); |
| 36 |
} |
| 37 |
if (isset($data['fieldId'])) { |
| 38 |
$entryField->setFieldId($data['fieldId']); |
| 39 |
} |
| 40 |
if (isset($data['fieldValue'])) { |
| 41 |
$entryField->setFieldValue($normalizedFieldValue); |
| 42 |
} |
| 43 |
return $entryField; |
| 44 |
} |
| 45 |
} |
| 46 |
|