Something went wrong. Try again.
A barebones implementation of an atproto PDS in PHP and Slim Framework 4.
pds php atproto
Something went wrong. Try again.
3.1 kB · 117 lines
PHP
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118<?php
declare(strict_types=1);
namespace App\Infrastructure\Persistence\Preference;
use App\Domain\Preference\AccountPref;use App\Domain\Preference\AccountPrefNotFoundException;use App\Domain\Preference\AccountPrefRepository;use App\Infrastructure\Database\Database;use App\Infrastructure\Database\Row;
class SqliteAccountPrefRepository implements AccountPrefRepository{ public function __construct(private readonly Database $db) { }
/** * @return AccountPref[] */ public function findAll(): array { $rows = $this->db->fetchAll('SELECT * FROM account_pref ORDER BY id');
$result = []; foreach ($rows as $row) { $result[] = $this->hydrate($row); } return $result; }
/** * @return AccountPref[] */ public function findByName(string $name): array { $rows = $this->db->fetchAll( 'SELECT * FROM account_pref WHERE name = ? ORDER BY id', [$name] );
$result = []; foreach ($rows as $row) { $result[] = $this->hydrate($row); } return $result; }
public function findById(int $id): AccountPref { $row = $this->db->fetchOne('SELECT * FROM account_pref WHERE id = ?', [$id]);
if ($row === null) { throw new AccountPrefNotFoundException(); }
return $this->hydrate($row); }
public function save(AccountPref $pref): AccountPref { if ($pref->getId() === 0) { $this->db->execute( 'INSERT INTO account_pref (name, value_json) VALUES (?, ?)', [$pref->getName(), $pref->getValueJson()] );
return new AccountPref( id: (int) $this->db->pdo()->lastInsertId(), name: $pref->getName(), valueJson: $pref->getValueJson(), ); }
$this->db->execute( 'INSERT INTO account_pref (id, name, value_json) VALUES (:id, :name, :value_json) ON CONFLICT(id) DO UPDATE SET name = excluded.name, value_json = excluded.value_json', [ 'id' => $pref->getId(), 'name' => $pref->getName(), 'value_json' => $pref->getValueJson(), ] );
return $pref; }
public function deleteById(int $id): void { $this->db->execute('DELETE FROM account_pref WHERE id = ?', [$id]); }
public function deleteByName(string $name): void { $this->db->execute('DELETE FROM account_pref WHERE name = ?', [$name]); }
public function deleteAll(): void { $this->db->pdo()->exec('DELETE FROM account_pref'); }
/** * @param array<string, mixed> $row */ private function hydrate(array $row): AccountPref { return new AccountPref( id: Row::int($row, 'id'), name: Row::str($row, 'name'), valueJson: Row::str($row, 'value_json'), ); }}