| @@ -1,0 +1,81 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * @copyright © Melograno Venture Studio. All rights reserved. | |
| 5 | + * @licence See LICENCE.md for license details. | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace IvyForms\Factory\Entry; | |
| 9 | + | |
| 10 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 11 | +use IvyForms\Entity\Entry\Entry as EntryEntity; | |
| 12 | +use IvyForms\Entity\EntryField\EntryField; | |
| 13 | +use IvyForms\Factory\EntryField\EntryFieldFactory; | |
| 14 | +use IvyForms\ValueObjects\Entry\Entry; | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * Class EntryFactory | |
| 18 | + * | |
| 19 | + * @package IvyForms\Factory\Entry | |
| 20 | + */ | |
| 21 | +class EntryFactory | |
| 22 | +{ | |
| 23 | + /** | |
| 24 | + * Create an EntryEntity from the provided data. | |
| 25 | + * | |
| 26 | + * @param array<string, mixed> $data The entry data. | |
| 27 | + * | |
| 28 | + * @return EntryEntity | |
| 29 | + * | |
| 30 | + * @throws ValidationException | |
| 31 | + */ | |
| 32 | + public static function create(array $data): EntryEntity | |
| 33 | + { | |
| 34 | + | |
| 35 | + $entryObject = new Entry( | |
| 36 | + $data['id'] ?? 0, | |
| 37 | + $data['formId'] ?? 0, | |
| 38 | + $data['userId'] ?? null, | |
| 39 | + $data['status'] ?? EntryEntity::DEFAULT_STATUS, | |
| 40 | + $data['ipAddress'] ?? null, | |
| 41 | + $data['userAgent'] ?? null, | |
| 42 | + $data['sourceURL'] ?? null, | |
| 43 | + ); | |
| 44 | + | |
| 45 | + $entry = new EntryEntity($entryObject); | |
| 46 | + | |
| 47 | + if (isset($data['id'])) { | |
| 48 | + $entry->setId($data['id']); | |
| 49 | + } | |
| 50 | + | |
| 51 | + if (isset($data['formId'])) { | |
| 52 | + $entry->setFormId($data['formId']); | |
| 53 | + } | |
| 54 | + if (isset($data['userId'])) { | |
| 55 | + $entry->setUserId($data['userId']); | |
| 56 | + } | |
| 57 | + if (isset($data['dateCreated'])) { | |
| 58 | + $entry->setDateCreated((string) $data['dateCreated']); | |
| 59 | + } | |
| 60 | + if (isset($data['dateEdited'])) { | |
| 61 | + $entry->setDateEdited((string) $data['dateEdited']); | |
| 62 | + } | |
| 63 | + if (isset($data['status'])) { | |
| 64 | + $entry->setStatus($data['status']); | |
| 65 | + } | |
| 66 | + if (isset($data['ipAddress'])) { | |
| 67 | + $entry->setIpAddress($data['ipAddress']); | |
| 68 | + } | |
| 69 | + if (isset($data['userAgent'])) { | |
| 70 | + $entry->setUserAgent($data['userAgent']); | |
| 71 | + } | |
| 72 | + if (isset($data['sourceURL'])) { | |
| 73 | + $entry->setSourceURL($data['sourceURL']); | |
| 74 | + } | |
| 75 | + if (isset($data['starred'])) { | |
| 76 | + $entry->setStarred($data['starred']); | |
| 77 | + } | |
| 78 | + | |
| 79 | + return $entry; | |
| 80 | + } | |
| 81 | +} | |