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