CustomFieldFactory.php
187 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Domain\Factory\CustomField; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Collection\Collection; |
| 6 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 7 | use AmeliaBooking\Domain\Entity\CustomField\CustomField; |
| 8 | use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory; |
| 9 | use AmeliaBooking\Domain\Factory\Booking\Event\EventFactory; |
| 10 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 11 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 12 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 13 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\IntegerValue; |
| 14 | use AmeliaBooking\Domain\ValueObjects\String\CustomFieldType; |
| 15 | use AmeliaBooking\Domain\ValueObjects\String\CustomFieldSaveType; |
| 16 | use AmeliaBooking\Domain\ValueObjects\String\Label; |
| 17 | |
| 18 | /** |
| 19 | * Class CustomFieldFactory |
| 20 | * |
| 21 | * @package AmeliaBooking\Domain\Factory\CustomField |
| 22 | */ |
| 23 | class CustomFieldFactory |
| 24 | { |
| 25 | /** |
| 26 | * @param $data |
| 27 | * |
| 28 | * @return CustomField |
| 29 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 30 | */ |
| 31 | public static function create($data) |
| 32 | { |
| 33 | $customField = new CustomField( |
| 34 | new Label($data['label']), |
| 35 | new CustomFieldType($data['type']), |
| 36 | new BooleanValueObject($data['required']), |
| 37 | new IntegerValue($data['position']), |
| 38 | new IntegerValue($data['width']), |
| 39 | new CustomFieldSaveType($data['saveType']) |
| 40 | ); |
| 41 | |
| 42 | if (isset($data['id'])) { |
| 43 | $customField->setId(new Id($data['id'])); |
| 44 | } |
| 45 | |
| 46 | if (isset($data['options'])) { |
| 47 | $optionList = []; |
| 48 | /** @var array $options */ |
| 49 | $options = $data['options']; |
| 50 | foreach ($options as $option) { |
| 51 | $optionList[] = CustomFieldOptionFactory::create($option); |
| 52 | } |
| 53 | |
| 54 | $customField->setOptions(new Collection($optionList)); |
| 55 | } |
| 56 | |
| 57 | if (isset($data['translations'])) { |
| 58 | $customField->setTranslations(new Json($data['translations'])); |
| 59 | } |
| 60 | |
| 61 | $serviceList = []; |
| 62 | |
| 63 | if (isset($data['allServices']) && $data['allServices']) { |
| 64 | $customField->setAllServices(new BooleanValueObject(true)); |
| 65 | } else { |
| 66 | $customField->setAllServices(new BooleanValueObject(false)); |
| 67 | if (isset($data['services'])) { |
| 68 | /** @var array $options */ |
| 69 | $services = $data['services']; |
| 70 | foreach ($services as $service) { |
| 71 | $serviceList[] = ServiceFactory::create($service); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | $customField->setServices(new Collection($serviceList)); |
| 77 | |
| 78 | $eventList = []; |
| 79 | |
| 80 | if (isset($data['allEvents']) && $data['allEvents']) { |
| 81 | $customField->setAllEvents(new BooleanValueObject(true)); |
| 82 | } else { |
| 83 | $customField->setAllEvents(new BooleanValueObject(false)); |
| 84 | if (isset($data['events'])) { |
| 85 | /** @var array $options */ |
| 86 | $events = $data['events']; |
| 87 | foreach ($events as $event) { |
| 88 | $eventList[] = EventFactory::create($event); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | $customField->setEvents(new Collection($eventList)); |
| 94 | |
| 95 | if (isset($data['useAsLocation'])) { |
| 96 | $customField->setUseAsLocation(new BooleanValueObject($data['useAsLocation'])); |
| 97 | } |
| 98 | |
| 99 | if (isset($data['includeInInvoice'])) { |
| 100 | $customField->setIncludeInInvoice(new BooleanValueObject($data['includeInInvoice'])); |
| 101 | } |
| 102 | |
| 103 | if (isset($data['saveFirstChoice'])) { |
| 104 | $customField->setSaveFirstChoice(new BooleanValueObject($data['saveFirstChoice'])); |
| 105 | } |
| 106 | |
| 107 | return $customField; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param array $rows |
| 112 | * |
| 113 | * @return Collection |
| 114 | * @throws InvalidArgumentException |
| 115 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 116 | */ |
| 117 | public static function createCollection($rows) |
| 118 | { |
| 119 | $customFields = []; |
| 120 | |
| 121 | foreach ($rows as $row) { |
| 122 | $customFieldId = $row['cf_id']; |
| 123 | $optionId = $row['cfo_id']; |
| 124 | $serviceId = !empty($row['s_id']) ? $row['s_id'] : null; |
| 125 | $eventId = !empty($row['e_id']) ? $row['e_id'] : null; |
| 126 | |
| 127 | $customFields[$customFieldId]['id'] = $row['cf_id']; |
| 128 | $customFields[$customFieldId]['label'] = $row['cf_label']; |
| 129 | $customFields[$customFieldId]['type'] = $row['cf_type']; |
| 130 | $customFields[$customFieldId]['saveType'] = $row['cf_saveType']; |
| 131 | $customFields[$customFieldId]['required'] = $row['cf_required']; |
| 132 | $customFields[$customFieldId]['position'] = $row['cf_position']; |
| 133 | $customFields[$customFieldId]['translations'] = $row['cf_translations']; |
| 134 | $customFields[$customFieldId]['allServices'] = $row['cf_allServices']; |
| 135 | $customFields[$customFieldId]['allEvents'] = $row['cf_allEvents']; |
| 136 | $customFields[$customFieldId]['useAsLocation'] = $row['cf_useAsLocation']; |
| 137 | $customFields[$customFieldId]['width'] = $row['cf_width']; |
| 138 | $customFields[$customFieldId]['saveFirstChoice'] = $row['cf_saveFirstChoice']; |
| 139 | $customFields[$customFieldId]['includeInInvoice'] = $row['cf_includeInInvoice']; |
| 140 | |
| 141 | |
| 142 | if ($optionId) { |
| 143 | $customFields[$customFieldId]['options'][$optionId]['id'] = $row['cfo_id']; |
| 144 | $customFields[$customFieldId]['options'][$optionId]['customFieldId'] = $row['cfo_custom_field_id']; |
| 145 | $customFields[$customFieldId]['options'][$optionId]['label'] = $row['cfo_label']; |
| 146 | $customFields[$customFieldId]['options'][$optionId]['position'] = $row['cfo_position']; |
| 147 | $customFields[$customFieldId]['options'][$optionId]['translations'] = $row['cfo_translations']; |
| 148 | } |
| 149 | |
| 150 | if ($serviceId) { |
| 151 | $customFields[$customFieldId]['services'][$serviceId]['id'] = $row['s_id']; |
| 152 | $customFields[$customFieldId]['services'][$serviceId]['name'] = $row['s_name']; |
| 153 | $customFields[$customFieldId]['services'][$serviceId]['description'] = $row['s_description']; |
| 154 | $customFields[$customFieldId]['services'][$serviceId]['color'] = $row['s_color']; |
| 155 | $customFields[$customFieldId]['services'][$serviceId]['price'] = $row['s_price']; |
| 156 | $customFields[$customFieldId]['services'][$serviceId]['status'] = $row['s_status']; |
| 157 | $customFields[$customFieldId]['services'][$serviceId]['categoryId'] = $row['s_categoryId']; |
| 158 | $customFields[$customFieldId]['services'][$serviceId]['minCapacity'] = $row['s_minCapacity']; |
| 159 | $customFields[$customFieldId]['services'][$serviceId]['maxCapacity'] = $row['s_maxCapacity']; |
| 160 | $customFields[$customFieldId]['services'][$serviceId]['duration'] = $row['s_duration']; |
| 161 | } |
| 162 | |
| 163 | if ($eventId) { |
| 164 | $customFields[$customFieldId]['events'][$eventId]['id'] = $row['e_id']; |
| 165 | $customFields[$customFieldId]['events'][$eventId]['name'] = $row['e_name']; |
| 166 | $customFields[$customFieldId]['events'][$eventId]['price'] = $row['e_price']; |
| 167 | $customFields[$customFieldId]['events'][$eventId]['parentId'] = $row['e_parentId']; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | $customFieldsCollection = new Collection(); |
| 172 | |
| 173 | foreach ($customFields as $customFieldKey => $customFieldArray) { |
| 174 | if (!array_key_exists('options', $customFieldArray)) { |
| 175 | $customFieldArray['options'] = []; |
| 176 | } |
| 177 | |
| 178 | $customFieldsCollection->addItem( |
| 179 | self::create($customFieldArray), |
| 180 | $customFieldKey |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | return $customFieldsCollection; |
| 185 | } |
| 186 | } |
| 187 |