-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Showing
2 changed files
with
148 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Notion\Databases\Query; | ||
|
||
/** @psalm-immutable */ | ||
class RelationFilter implements Filter, Condition | ||
{ | ||
private static array $validOperators = [ | ||
Operator::Contains, | ||
Operator::DoesNotContain, | ||
Operator::IsEmpty, | ||
Operator::IsNotEmpty, | ||
]; | ||
|
||
|
||
private function __construct( | ||
private readonly string $propertyName, | ||
private readonly Operator $operator, | ||
private readonly string|bool $value, | ||
) { | ||
if (!in_array($operator, self::$validOperators)) { | ||
throw new \Exception("Invalid operator"); | ||
} | ||
} | ||
|
||
public static function property(string $propertyName): self | ||
{ | ||
return new self( | ||
$propertyName, | ||
Operator::IsNotEmpty, | ||
true | ||
); | ||
} | ||
|
||
/** @return "property" */ | ||
public function propertyType(): string | ||
{ | ||
return 'property'; | ||
} | ||
|
||
public function propertyName(): string | ||
{ | ||
return $this->propertyName; | ||
} | ||
|
||
public function operator(): Operator | ||
{ | ||
return $this->operator; | ||
} | ||
|
||
public function value(): string|bool | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
$this->propertyType() => $this->propertyName, | ||
"relation" => [ | ||
$this->operator->value => $this->value | ||
], | ||
]; | ||
} | ||
|
||
public function contains(string $value): self | ||
{ | ||
return new self($this->propertyName, Operator::Contains, $value); | ||
} | ||
|
||
public function doesNotContain(string $value): self | ||
{ | ||
return new self($this->propertyName, Operator::DoesNotContain, $value); | ||
} | ||
|
||
public function isEmpty(): self | ||
{ | ||
return new self($this->propertyName, Operator::IsEmpty, true); | ||
} | ||
|
||
public function isNotEmpty(): self | ||
{ | ||
return new self($this->propertyName, Operator::IsNotEmpty, true); | ||
} | ||
} |
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,63 @@ | ||
<?php | ||
|
||
namespace Notion\Test\Unit\Databases\Query; | ||
|
||
use Notion\Databases\Query\Operator; | ||
use Notion\Databases\Query\RelationFilter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RelationFilterTest extends TestCase | ||
{ | ||
public function test_empty_filter(): void | ||
{ | ||
$filter = RelationFilter::property("Category"); | ||
|
||
$this->assertSame("Category", $filter->propertyName()); | ||
$this->assertSame(Operator::IsNotEmpty, $filter->operator()); | ||
$this->assertTrue($filter->value()); | ||
} | ||
|
||
public function test_contains(): void | ||
{ | ||
$filter = RelationFilter::property("Category")->contains("Blog"); | ||
|
||
$expected = [ | ||
"property" => "Category", | ||
"relation" => [ "contains" => "Blog" ], | ||
]; | ||
$this->assertSame($expected, $filter->toArray()); | ||
} | ||
|
||
public function test_does_not_contain(): void | ||
{ | ||
$filter = RelationFilter::property("Category")->doesNotContain("Blog"); | ||
|
||
$expected = [ | ||
"property" => "Category", | ||
"relation" => [ "does_not_contain" => "Blog" ], | ||
]; | ||
$this->assertSame($expected, $filter->toArray()); | ||
} | ||
|
||
public function test_is_empty(): void | ||
{ | ||
$filter = RelationFilter::property("Category")->isEmpty(); | ||
|
||
$expected = [ | ||
"property" => "Category", | ||
"relation" => [ "is_empty" => true ], | ||
]; | ||
$this->assertSame($expected, $filter->toArray()); | ||
} | ||
|
||
public function test_is_not_empty(): void | ||
{ | ||
$filter = RelationFilter::property("Category")->isNotEmpty(); | ||
|
||
$expected = [ | ||
"property" => "Category", | ||
"relation" => [ "is_not_empty" => true ], | ||
]; | ||
$this->assertSame($expected, $filter->toArray()); | ||
} | ||
} |