Skip to content

Commit

Permalink
Merge pull request #7 from formal-php/simplify-spec-usage
Browse files Browse the repository at this point in the history
Simplify specification usage
  • Loading branch information
Baptouuuu authored Jul 14, 2024
2 parents 9cab7ad + 27cad5e commit e74c574
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 292 deletions.
34 changes: 9 additions & 25 deletions documentation/queries/delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,24 @@ use Formal\AccesLayer\{
Table\Name,
};
use Innmind\Specification\{
Comparator,
Composable,
Comparator\Property,
Sign,
};

final class Username implements Comparator
final class Username
{
use Composable;

private string $value;

public function __construct(string $value)
{
$this->value = $value;
}

public function property(): string
{
return 'username'; // this is the name of the column
}

public function sign(): Sign
{
return Sign::equality;
}

public function value(): mixed
public static function of(string $value): Property
{
return $this->value;
return Property::of(
'username', // this is the name of the column
Sign::equality,
$value,
);
}
}

$delete = Delete::from(Name::of('users'))->where(
(new Username('some username'))->or(new Username('other username')),
Username::of('some username')->or(Username::of('other username')),
);
$connection($delete);
```
Expand Down
34 changes: 9 additions & 25 deletions documentation/queries/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,19 @@ use Formal\AccesLayer\{
Row,
};
use Innmind\Specification\{
Comparator,
Composable,
Comparator\Property,
Sign,
};

final class Username implements Comparator
final class Username
{
use Composable;

private string $value;

public function __construct(string $value)
{
$this->value = $value;
}

public function property(): string
{
return 'username'; // this is the name of the column
}

public function sign(): Sign
{
return Sign::equality;
}

public function value(): mixed
public static function of(string $value): Property
{
return $this->value;
return Property::of(
'username', // this is the name of the column
Sign::equality,
$value,
);
}
}

Expand All @@ -68,7 +52,7 @@ $update = Update::set(
]),
);
$update = $update->where(
(new Username('some username'))->or(new Username('other username')),
Username::of('some username')->or(Username::of('other username')),
);
$connection($update);
```
Expand Down
31 changes: 5 additions & 26 deletions properties/Connection/DeleteSpecificRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
};
use Innmind\Specification\{
Comparator,
Composable,
Sign,
};
use Innmind\Immutable\Sequence;
Expand Down Expand Up @@ -73,31 +72,11 @@ public function ensureHeldBy(Assert $assert, object $connection): object
)));

$delete = Query\Delete::from(Table\Name::of('test'))->where(
new class($this->uuid1) implements Comparator {
use Composable;

private string $uuid;

public function __construct(string $uuid)
{
$this->uuid = $uuid;
}

public function property(): string
{
return 'id';
}

public function sign(): Sign
{
return Sign::equality;
}

public function value(): string
{
return $this->uuid;
}
},
Comparator\Property::of(
'id',
Sign::equality,
$this->uuid1,
),
);
$sequence = $connection($delete);

Expand Down
29 changes: 5 additions & 24 deletions properties/Connection/SelectOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
};
use Innmind\Specification\{
Comparator,
Composable,
Sign,
};
use Innmind\Immutable\Sequence;
Expand Down Expand Up @@ -89,29 +88,11 @@ public function ensureHeldBy(Assert $assert, object $connection): object
Column\Name::of('username')->in($table),
Direction::asc,
)
->where(new class([$this->uuid1, $this->uuid2]) implements Comparator {
use Composable;

public function __construct(
private array $values,
) {
}

public function property(): string
{
return 'id';
}

public function sign(): Sign
{
return Sign::in;
}

public function value(): array
{
return $this->values;
}
});
->where(Comparator\Property::of(
'id',
Sign::in,
[$this->uuid1, $this->uuid2],
));
$rows = $connection($select);

$assert
Expand Down
31 changes: 5 additions & 26 deletions properties/Connection/SelectWhere.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
};
use Innmind\Specification\{
Comparator,
Composable,
Sign,
};
use Innmind\BlackBox\{
Expand Down Expand Up @@ -64,31 +63,11 @@ public function ensureHeldBy(Assert $assert, object $connection): object
));

$select = Select::from(Name::of('test'));
$select = $select->where(new class($this->uuid) implements Comparator {
use Composable;

private string $uuid;

public function __construct(string $uuid)
{
$this->uuid = $uuid;
}

public function property(): string
{
return 'id';
}

public function sign(): Sign
{
return Sign::equality;
}

public function value(): string
{
return $this->uuid;
}
});
$select = $select->where(Comparator\Property::of(
'id',
Sign::equality,
$this->uuid,
));
$rows = $connection($select);

$assert->count(1, $rows);
Expand Down
28 changes: 5 additions & 23 deletions properties/Connection/SelectWhereContains.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
};
use Innmind\Specification\{
Comparator,
Composable,
Sign,
};
use Innmind\BlackBox\{
Expand Down Expand Up @@ -75,28 +74,11 @@ public function ensureHeldBy(Assert $assert, object $connection): object
));

$select = Select::from(Name::of('test'));
$select = $select->where(new class($this->username) implements Comparator {
use Composable;

public function __construct(private string $username)
{
}

public function property(): string
{
return 'username';
}

public function sign(): Sign
{
return Sign::contains;
}

public function value(): string
{
return $this->username;
}
});
$select = $select->where(Comparator\Property::of(
'username',
Sign::contains,
$this->username,
));
$rows = $connection($select);

$assert->count(1, $rows);
Expand Down
28 changes: 5 additions & 23 deletions properties/Connection/SelectWhereEndsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
};
use Innmind\Specification\{
Comparator,
Composable,
Sign,
};
use Innmind\BlackBox\{
Expand Down Expand Up @@ -71,28 +70,11 @@ public function ensureHeldBy(Assert $assert, object $connection): object
));

$select = Select::from(Name::of('test'));
$select = $select->where(new class($this->suffix) implements Comparator {
use Composable;

public function __construct(private string $suffix)
{
}

public function property(): string
{
return 'username';
}

public function sign(): Sign
{
return Sign::endsWith;
}

public function value(): string
{
return $this->suffix;
}
});
$select = $select->where(Comparator\Property::of(
'username',
Sign::endsWith,
$this->suffix,
));
$rows = $connection($select);

$assert->count(1, $rows);
Expand Down
28 changes: 5 additions & 23 deletions properties/Connection/SelectWhereIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
};
use Innmind\Specification\{
Comparator,
Composable,
Sign,
};
use Innmind\BlackBox\{
Expand Down Expand Up @@ -64,28 +63,11 @@ public function ensureHeldBy(Assert $assert, object $connection): object
));

$select = Select::from(Name::of('test'));
$select = $select->where(new class($this->uuid) implements Comparator {
use Composable;

public function __construct(private string $uuid)
{
}

public function property(): string
{
return 'test.id';
}

public function sign(): Sign
{
return Sign::in;
}

public function value(): array
{
return [$this->uuid];
}
});
$select = $select->where(Comparator\Property::of(
'test.id',
Sign::in,
[$this->uuid],
));
$rows = $connection($select);

$assert->count(1, $rows);
Expand Down
Loading

0 comments on commit e74c574

Please sign in to comment.