Skip to content

Commit 2fa37ab

Browse files
committed
Allow modification of property type
1 parent 1d90e78 commit 2fa37ab

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

src/endpoints/NotionEndpoint.php

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Notion\Pages\PageParent;
1919
use Notion\Pages\Properties\Date;
2020
use Notion\Pages\Properties\MultiSelect;
21-
use Notion\Pages\Properties\RichTextProperty;
2221
use Notion\Pages\Properties\Select;
2322
use Notion\Pages\Properties\Title;
2423
use Notion\Pages\Properties\Url;
@@ -84,36 +83,6 @@ public function __construct(
8483
{
8584
}
8685

87-
/**
88-
* @param string $propertyName
89-
* @param class-string<PropertyInterface> $propertyClass
90-
* @param Database $database Pass by reference because there's some immutable stuff going on in the Notion lib
91-
* @return bool True if property was created
92-
* @throws Exception
93-
*/
94-
private function createProperty(string $propertyName, string $propertyClass, Database &$database): bool
95-
{
96-
$existingProperties = $database->properties()->getAll();
97-
98-
// Don't create a property if it already exists
99-
if (!empty($existingProperties[$propertyName])) {
100-
return false;
101-
}
102-
103-
// If you're using a class that isn't in this list, most likely the ::create
104-
// method is compatible. But it's worth double-checking.
105-
$database = match ($propertyClass) {
106-
UrlDb::class,
107-
SelectDb::class,
108-
MultiSelectDb::class,
109-
RichTextDb::class,
110-
DateDb::class => $database->addProperty($propertyClass::create($propertyName)),
111-
default => throw new Exception("createProperty doesnt support the class $propertyClass. Double check that its ::create method is compatible and add to this method")
112-
};
113-
114-
return true;
115-
}
116-
11786
/**
11887
* @throws Exception
11988
*/
@@ -125,7 +94,7 @@ public function send(SitePayload $payload): void
12594
// Loop through property config and create properties that don't exist on the DB
12695
$updated = false;
12796
foreach (self::PROPERTY_CONFIG as $propertyName => $config) {
128-
$didUpdate = $this->createProperty(
97+
$didUpdate = $this->configureProperty(
12998
$propertyName,
13099
$config['class'],
131100
$database
@@ -178,4 +147,35 @@ public function send(SitePayload $payload): void
178147
$notion->pages()->update($page);
179148
}
180149
}
150+
151+
/**
152+
* @param string $propertyName
153+
* @param class-string<PropertyInterface> $propertyClass
154+
* @param Database $database Pass by reference because there's some immutable stuff going on in the Notion lib
155+
* @return bool True if property was created
156+
* @throws Exception
157+
*/
158+
private function configureProperty(string $propertyName, string $propertyClass, Database &$database): bool
159+
{
160+
$existingProperties = $database->properties()->getAll();
161+
$existingProperty = $existingProperties[$propertyName] ?? null;
162+
163+
// Don't configure a property if it already exists and has same type
164+
if ($existingProperty && $existingProperty::class === $propertyClass) {
165+
return false;
166+
}
167+
168+
// If you're using a class that isn't in this list, most likely the ::create
169+
// method is compatible. But it's worth double-checking.
170+
$database = match ($propertyClass) {
171+
UrlDb::class,
172+
SelectDb::class,
173+
MultiSelectDb::class,
174+
RichTextDb::class,
175+
DateDb::class => $database->addProperty($propertyClass::create($propertyName)),
176+
default => throw new Exception("createProperty doesnt support the class $propertyClass. Double check that its ::create method is compatible and add to this method")
177+
};
178+
179+
return true;
180+
}
181181
}

0 commit comments

Comments
 (0)