-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69239eb
commit e6220de
Showing
4 changed files
with
116 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |