Skip to content

Commit 412c864

Browse files
committed
changed property fromResponse to more dynamic mapping
1 parent 8874b20 commit 412c864

File tree

1 file changed

+49
-43
lines changed

1 file changed

+49
-43
lines changed

src/Entities/Properties/Property.php

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace FiveamCode\LaravelNotionApi\Entities\Properties;
44

5+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
56
use Illuminate\Support\Arr;
67
use FiveamCode\LaravelNotionApi\Entities\Entity;
78
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
@@ -94,7 +95,10 @@ public function getTitle(): string
9495
return $this->title;
9596
}
9697

97-
public function setTitle($title): void
98+
/**
99+
* @param string $title
100+
*/
101+
public function setTitle(string $title): void
98102
{
99103
$this->title = $title;
100104
}
@@ -133,55 +137,57 @@ public function getContent()
133137
}
134138

135139
/**
136-
* @param $propertyKey
140+
* @param string $propertyKey
137141
* @param $rawContent
138142
* @return Property
139143
* @throws HandlingException
140144
*/
141-
public static function fromResponse($propertyKey, $rawContent): Property
145+
public static function fromResponse(string $propertyKey, $rawContent): Property
142146
{
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);
183149

184150
$property->setResponseData($rawContent);
151+
185152
return $property;
186153
}
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+
}
187193
}

0 commit comments

Comments
 (0)