Skip to content

Commit

Permalink
feat(database): relation filter
Browse files Browse the repository at this point in the history
  • Loading branch information
aknEvrnky authored Mar 24, 2023
1 parent 4ee122e commit 3d580fa
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/Databases/Query/RelationFilter.php
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);
}
}
63 changes: 63 additions & 0 deletions tests/Unit/Databases/Query/RelationFilterTest.php
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());
}
}

0 comments on commit 3d580fa

Please sign in to comment.