Skip to content

Commit

Permalink
Allow modification of property type
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuapease committed May 20, 2024
1 parent 1d90e78 commit 2fa37ab
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions src/endpoints/NotionEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Notion\Pages\PageParent;
use Notion\Pages\Properties\Date;
use Notion\Pages\Properties\MultiSelect;
use Notion\Pages\Properties\RichTextProperty;
use Notion\Pages\Properties\Select;
use Notion\Pages\Properties\Title;
use Notion\Pages\Properties\Url;
Expand Down Expand Up @@ -84,36 +83,6 @@ public function __construct(
{
}

/**
* @param string $propertyName
* @param class-string<PropertyInterface> $propertyClass
* @param Database $database Pass by reference because there's some immutable stuff going on in the Notion lib
* @return bool True if property was created
* @throws Exception
*/
private function createProperty(string $propertyName, string $propertyClass, Database &$database): bool
{
$existingProperties = $database->properties()->getAll();

// Don't create a property if it already exists
if (!empty($existingProperties[$propertyName])) {
return false;
}

// If you're using a class that isn't in this list, most likely the ::create
// method is compatible. But it's worth double-checking.
$database = match ($propertyClass) {
UrlDb::class,
SelectDb::class,
MultiSelectDb::class,
RichTextDb::class,
DateDb::class => $database->addProperty($propertyClass::create($propertyName)),
default => throw new Exception("createProperty doesnt support the class $propertyClass. Double check that its ::create method is compatible and add to this method")
};

return true;
}

/**
* @throws Exception
*/
Expand All @@ -125,7 +94,7 @@ public function send(SitePayload $payload): void
// Loop through property config and create properties that don't exist on the DB
$updated = false;
foreach (self::PROPERTY_CONFIG as $propertyName => $config) {
$didUpdate = $this->createProperty(
$didUpdate = $this->configureProperty(
$propertyName,
$config['class'],
$database
Expand Down Expand Up @@ -178,4 +147,35 @@ public function send(SitePayload $payload): void
$notion->pages()->update($page);
}
}

/**
* @param string $propertyName
* @param class-string<PropertyInterface> $propertyClass
* @param Database $database Pass by reference because there's some immutable stuff going on in the Notion lib
* @return bool True if property was created
* @throws Exception
*/
private function configureProperty(string $propertyName, string $propertyClass, Database &$database): bool
{
$existingProperties = $database->properties()->getAll();
$existingProperty = $existingProperties[$propertyName] ?? null;

// Don't configure a property if it already exists and has same type
if ($existingProperty && $existingProperty::class === $propertyClass) {
return false;
}

// If you're using a class that isn't in this list, most likely the ::create
// method is compatible. But it's worth double-checking.
$database = match ($propertyClass) {
UrlDb::class,
SelectDb::class,
MultiSelectDb::class,
RichTextDb::class,
DateDb::class => $database->addProperty($propertyClass::create($propertyName)),
default => throw new Exception("createProperty doesnt support the class $propertyClass. Double check that its ::create method is compatible and add to this method")
};

return true;
}
}

0 comments on commit 2fa37ab

Please sign in to comment.