PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.27
Booking for Appointments and Events Calendar – Amelia v1.2.27
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Domain / Factory / CustomField / CustomFieldFactory.php
ameliabooking / src / Domain / Factory / CustomField Last commit date
CustomFieldFactory.php 1 year ago CustomFieldOptionFactory.php 5 years ago
CustomFieldFactory.php
182 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['saveFirstChoice'])) {
100 $customField->setSaveFirstChoice(new BooleanValueObject($data['saveFirstChoice']));
101 }
102
103 return $customField;
104 }
105
106 /**
107 * @param array $rows
108 *
109 * @return Collection
110 * @throws InvalidArgumentException
111 * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException
112 */
113 public static function createCollection($rows)
114 {
115 $customFields = [];
116
117 foreach ($rows as $row) {
118 $customFieldId = $row['cf_id'];
119 $optionId = $row['cfo_id'];
120 $serviceId = $row['s_id'];
121 $eventId = $row['e_id'];
122
123 $customFields[$customFieldId]['id'] = $row['cf_id'];
124 $customFields[$customFieldId]['label'] = $row['cf_label'];
125 $customFields[$customFieldId]['type'] = $row['cf_type'];
126 $customFields[$customFieldId]['saveType'] = $row['cf_saveType'];
127 $customFields[$customFieldId]['required'] = $row['cf_required'];
128 $customFields[$customFieldId]['position'] = $row['cf_position'];
129 $customFields[$customFieldId]['translations'] = $row['cf_translations'];
130 $customFields[$customFieldId]['allServices'] = $row['cf_allServices'];
131 $customFields[$customFieldId]['allEvents'] = $row['cf_allEvents'];
132 $customFields[$customFieldId]['useAsLocation'] = $row['cf_useAsLocation'];
133 $customFields[$customFieldId]['width'] = $row['cf_width'];
134 $customFields[$customFieldId]['saveFirstChoice'] = $row['cf_saveFirstChoice'];
135
136
137 if ($optionId) {
138 $customFields[$customFieldId]['options'][$optionId]['id'] = $row['cfo_id'];
139 $customFields[$customFieldId]['options'][$optionId]['customFieldId'] = $row['cfo_custom_field_id'];
140 $customFields[$customFieldId]['options'][$optionId]['label'] = $row['cfo_label'];
141 $customFields[$customFieldId]['options'][$optionId]['position'] = $row['cfo_position'];
142 $customFields[$customFieldId]['options'][$optionId]['translations'] = $row['cfo_translations'];
143 }
144
145 if ($serviceId) {
146 $customFields[$customFieldId]['services'][$serviceId]['id'] = $row['s_id'];
147 $customFields[$customFieldId]['services'][$serviceId]['name'] = $row['s_name'];
148 $customFields[$customFieldId]['services'][$serviceId]['description'] = $row['s_description'];
149 $customFields[$customFieldId]['services'][$serviceId]['color'] = $row['s_color'];
150 $customFields[$customFieldId]['services'][$serviceId]['price'] = $row['s_price'];
151 $customFields[$customFieldId]['services'][$serviceId]['status'] = $row['s_status'];
152 $customFields[$customFieldId]['services'][$serviceId]['categoryId'] = $row['s_categoryId'];
153 $customFields[$customFieldId]['services'][$serviceId]['minCapacity'] = $row['s_minCapacity'];
154 $customFields[$customFieldId]['services'][$serviceId]['maxCapacity'] = $row['s_maxCapacity'];
155 $customFields[$customFieldId]['services'][$serviceId]['duration'] = $row['s_duration'];
156 }
157
158 if ($eventId) {
159 $customFields[$customFieldId]['events'][$eventId]['id'] = $row['e_id'];
160 $customFields[$customFieldId]['events'][$eventId]['name'] = $row['e_name'];
161 $customFields[$customFieldId]['events'][$eventId]['price'] = $row['e_price'];
162 $customFields[$customFieldId]['events'][$eventId]['parentId'] = $row['e_parentId'];
163 }
164 }
165
166 $customFieldsCollection = new Collection();
167
168 foreach ($customFields as $customFieldKey => $customFieldArray) {
169 if (!array_key_exists('options', $customFieldArray)) {
170 $customFieldArray['options'] = [];
171 }
172
173 $customFieldsCollection->addItem(
174 self::create($customFieldArray),
175 $customFieldKey
176 );
177 }
178
179 return $customFieldsCollection;
180 }
181 }
182