|
2 | 2 |
|
3 | 3 | namespace FiveamCode\LaravelNotionApi\Entities\Properties; |
4 | 4 |
|
| 5 | +use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; |
5 | 6 | use Illuminate\Support\Arr; |
6 | 7 | use FiveamCode\LaravelNotionApi\Entities\Entity; |
7 | 8 | use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; |
@@ -94,7 +95,10 @@ public function getTitle(): string |
94 | 95 | return $this->title; |
95 | 96 | } |
96 | 97 |
|
97 | | - public function setTitle($title): void |
| 98 | + /** |
| 99 | + * @param string $title |
| 100 | + */ |
| 101 | + public function setTitle(string $title): void |
98 | 102 | { |
99 | 103 | $this->title = $title; |
100 | 104 | } |
@@ -133,55 +137,57 @@ public function getContent() |
133 | 137 | } |
134 | 138 |
|
135 | 139 | /** |
136 | | - * @param $propertyKey |
| 140 | + * @param string $propertyKey |
137 | 141 | * @param $rawContent |
138 | 142 | * @return Property |
139 | 143 | * @throws HandlingException |
140 | 144 | */ |
141 | | - public static function fromResponse($propertyKey, $rawContent): Property |
| 145 | + public static function fromResponse(string $propertyKey, $rawContent): Property |
142 | 146 | { |
143 | | - $property = null; |
144 | | - if ($rawContent['type'] == 'multi_select') { |
145 | | - $property = new MultiSelect($propertyKey); |
146 | | - } else if ($rawContent['type'] == 'select') { |
147 | | - $property = new Select($propertyKey); |
148 | | - } else if ($rawContent['type'] == 'text') { |
149 | | - $property = new Text($propertyKey); |
150 | | - } else if ($rawContent['type'] == 'created_by') { |
151 | | - $property = new CreatedBy($propertyKey); |
152 | | - } else if ($rawContent['type'] == 'title') { |
153 | | - $property = new Title($propertyKey); |
154 | | - } else if ($rawContent['type'] == 'number') { |
155 | | - $property = new Number($propertyKey); |
156 | | - } else if ($rawContent['type'] == 'people') { |
157 | | - $property = new People($propertyKey); |
158 | | - } else if ($rawContent['type'] == 'checkbox') { |
159 | | - $property = new Checkbox($propertyKey); |
160 | | - } else if ($rawContent['type'] == 'date') { |
161 | | - $property = new Date($propertyKey); |
162 | | - } else if ($rawContent['type'] == 'email') { |
163 | | - $property = new Email($propertyKey); |
164 | | - } else if ($rawContent['type'] == 'phone_number') { |
165 | | - $property = new PhoneNumber($propertyKey); |
166 | | - } else if ($rawContent['type'] == 'url') { |
167 | | - $property = new Url($propertyKey); |
168 | | - } else if ($rawContent['type'] == 'last_edited_by') { |
169 | | - $property = new LastEditedBy($propertyKey); |
170 | | - } else if ($rawContent['type'] == 'created_time') { |
171 | | - $property = new CreatedTime($propertyKey); |
172 | | - } else if ($rawContent['type'] == 'last_edited_time') { |
173 | | - $property = new LastEditedTime($propertyKey); |
174 | | - } else if ($rawContent['type'] == 'files') { |
175 | | - $property = new Files($propertyKey); |
176 | | - } else if ($rawContent['type'] == 'formula') { |
177 | | - $property = new Formula($propertyKey); |
178 | | - } else if ($rawContent['type'] == 'rollup') { |
179 | | - $property = new Rollup($propertyKey); |
180 | | - } else { |
181 | | - $property = new Property($propertyKey); |
182 | | - } |
| 147 | + $propertyClass = self::mapTypeToClass($rawContent['type']); |
| 148 | + $property = new $propertyClass($propertyKey); |
183 | 149 |
|
184 | 150 | $property->setResponseData($rawContent); |
| 151 | + |
185 | 152 | return $property; |
186 | 153 | } |
| 154 | + |
| 155 | + |
| 156 | + /** |
| 157 | + * Maps the type of a property to the corresponding package class by converting the type name. |
| 158 | + * |
| 159 | + * @param string $type |
| 160 | + * @return string |
| 161 | + */ |
| 162 | + private static function mapTypeToClass(string $type): string |
| 163 | + { |
| 164 | + |
| 165 | + switch ($type) { |
| 166 | + case 'multi_select': |
| 167 | + case 'select': |
| 168 | + case 'created_by': |
| 169 | + case 'title': |
| 170 | + case 'number': |
| 171 | + case 'people': |
| 172 | + case 'checkbox': |
| 173 | + case 'date': |
| 174 | + case 'email': |
| 175 | + case 'phone_number': |
| 176 | + case 'url': |
| 177 | + case 'last_edited_by': |
| 178 | + case 'created_time': |
| 179 | + case 'last_edited_time': |
| 180 | + case 'files': |
| 181 | + case 'formula': |
| 182 | + case 'rollup': |
| 183 | + $class = str_replace('_', '', ucwords($type, '_')); |
| 184 | + return "FiveamCode\\LaravelNotionApi\\Entities\\Properties\\" . $class; |
| 185 | + case 'text': |
| 186 | + # TODO: Depending on the Notion API version. |
| 187 | + return RichText::class; |
| 188 | + default: |
| 189 | + return Property::class; |
| 190 | + } |
| 191 | + |
| 192 | + } |
187 | 193 | } |
0 commit comments