85 lines
2.0 KiB
PHP
85 lines
2.0 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace Lewisdale\Webmentions\Gateways;
|
|
|
|
use Exception;
|
|
use Lewisdale\Webmentions\Models\Webmention;
|
|
use PDO;
|
|
|
|
class SqliteGateway extends WebmentionGatewayInterface {
|
|
private readonly PDO $connection;
|
|
|
|
function __construct(string $name)
|
|
{
|
|
$this->connection = new PDO("sqlite:$name");
|
|
$this->up();
|
|
}
|
|
|
|
protected function up() : void
|
|
{
|
|
// Create Webmention table
|
|
$sql = <<<SQL
|
|
CREATE TABLE IF NOT EXISTS webmentions (
|
|
id INTEGER PRIMARY KEY,
|
|
target TEXT NOT NULL,
|
|
source TEXT NOT NULL,
|
|
content TEXT,
|
|
author TEXT
|
|
);
|
|
SQL;
|
|
|
|
$this->connection->exec($sql);
|
|
}
|
|
|
|
public function get(int $id): ?Webmention
|
|
{
|
|
$sql = "SELECT * FROM webmentions;";
|
|
$statement = $this->connection->query($sql);
|
|
|
|
if ($statement == false) {
|
|
return null;
|
|
}
|
|
|
|
$row = $statement->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($row) {
|
|
return new Webmention(
|
|
$row["id"],
|
|
$row["target"],
|
|
$row["source"],
|
|
$row["content"],
|
|
$row["author"]
|
|
);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function getByPost(string $post): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function save(Webmention $webmention): ?int
|
|
{
|
|
$sql = <<< SQL
|
|
INSERT INTO webmentions
|
|
(id, target, source, content, author)
|
|
VALUES
|
|
(:id, :target, :source, :content, :author);
|
|
SQL;
|
|
|
|
$statement = $this->connection->prepare($sql);
|
|
$success = $statement->execute((array) $webmention);
|
|
$statement->closeCursor();
|
|
|
|
return $success ? (int) $this->connection->lastInsertId() : null;
|
|
}
|
|
|
|
public function delete(Webmention $webmention): void
|
|
{
|
|
throw new Exception("Method " . SqliteGateway::class . "::delete not implemented");
|
|
|
|
}
|
|
}
|
|
?>
|