Skip to content

Commit

Permalink
Add formula database property
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Nov 1, 2021
1 parent 69239eb commit e6220de
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Databases/Properties/Factory.php
Expand Up @@ -20,6 +20,7 @@ public static function fromArray(array $array): PropertyInterface
Property::TYPE_SELECT => Select::fromArray($array),
Property::TYPE_MULTI_SELECT => MultiSelect::fromArray($array),
Property::TYPE_DATE => Date::fromArray($array),
Property::TYPE_FORMULA => Formula::fromArray($array),
Property::TYPE_PEOPLE => People::fromArray($array),
Property::TYPE_FILES => Files::fromArray($array),
Property::TYPE_CHECKBOX => Checkbox::fromArray($array),
Expand Down
66 changes: 66 additions & 0 deletions src/Databases/Properties/Formula.php
@@ -0,0 +1,66 @@
<?php

namespace Notion\Databases\Properties;

/**
* @psalm-type FormulaJson = array{
* id: string,
* name: string,
* type: "formula",
* formula: array{ expression: string },
* }
*/
class Formula implements PropertyInterface
{
private const TYPE = Property::TYPE_FORMULA;

private Property $property;
private string $expression;

private function __construct(Property $property, string $expression)
{
$this->property = $property;
$this->expression = $expression;
}

public static function create(string $propertyName = "Formula", string $expression = ""): self
{
$property = Property::create("", $propertyName, self::TYPE);

return new self($property, $expression);
}

public function property(): Property
{
return $this->property;
}

public function expression(): string
{
return $this->expression;
}

public function withExpression(string $expression): self
{
return new self($this->property, $expression);
}

public static function fromArray(array $array): self
{
/** @psalm-var FormulaJson $array */
$property = Property::fromArray($array);
$expression = $array[self::TYPE]["expression"];

return new self($property, $expression);
}

public function toArray(): array
{
$array = $this->property->toArray();
$array[self::TYPE] = [
"expression" => $this->expression,
];

return $array;
}
}
8 changes: 4 additions & 4 deletions src/Databases/Properties/Property.php
Expand Up @@ -105,10 +105,10 @@ public function isDate(): bool
return $this->type === self::TYPE_DATE;
}

// public function isFormula(): bool
// {
// return $this->type === self::TYPE_FORMULA;
// }
public function isFormula(): bool
{
return $this->type === self::TYPE_FORMULA;
}

// public function isRelation(): bool
// {
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/Databases/Properties/FormulaTest.php
@@ -0,0 +1,45 @@
<?php

namespace Notion\Test\Unit\Databases\Properties;

use Notion\Databases\Properties\Factory;
use Notion\Databases\Properties\Formula;
use PHPUnit\Framework\TestCase;

class FormulaTest extends TestCase
{
public function test_create(): void
{
$expression = "if(prop(\"In stock\"), 0, prop(\"Price\"))";
$formula = Formula::create("Dummy prop name", $expression);

$this->assertEquals("Dummy prop name", $formula->property()->name());
$this->assertTrue($formula->property()->isFormula());
$this->assertEquals($expression, $formula->expression());
}

public function test_change_expression(): void
{
$expression = "if(prop(\"In stock\"), 0, prop(\"Price\"))";
$formula = Formula::create()->withExpression($expression);

$this->assertEquals($expression, $formula->expression());
}

public function test_array_conversion(): void
{
$array = [
"id" => "abc",
"name" => "dummy",
"type" => "formula",
"formula" => [
"expression" => "if(prop(\"In stock\"), 0, prop(\"Price\"))",
],
];
$formula = Formula::fromArray($array);
$fromFactory = Factory::fromArray($array);

$this->assertEquals($array, $formula->toArray());
$this->assertEquals($array, $fromFactory->toArray());
}
}

0 comments on commit e6220de

Please sign in to comment.