Use self where possible

This commit is contained in:
Daniel J. Summers 2023-08-23 22:15:43 -04:00
parent 9491359b52
commit fa281124bb
5 changed files with 50 additions and 50 deletions

View File

@ -17,8 +17,8 @@ class Data
public static function startUp() public static function startUp()
{ {
Configuration::$connectionString = "pgsql:host=localhost;port=5432;dbname=leafjson;user=leaf;password=leaf"; Configuration::$connectionString = "pgsql:host=localhost;port=5432;dbname=leafjson;user=leaf;password=leaf";
Definition::ensureTable(Data::REQ_TABLE); Definition::ensureTable(self::REQ_TABLE);
Definition::ensureIndex(Data::REQ_TABLE, DocumentIndex::Optimized); Definition::ensureIndex(self::REQ_TABLE, DocumentIndex::Optimized);
} }
/** /**
@ -30,7 +30,7 @@ class Data
*/ */
public static function findFullRequestById(string $reqId, string $userId): ?Request public static function findFullRequestById(string $reqId, string $userId): ?Request
{ {
$req = Document::findById(Data::REQ_TABLE, $reqId, Request::class); $req = Document::findById(self::REQ_TABLE, $reqId, Request::class);
return is_null($req) || $req->userId != $userId ? null : $req; return is_null($req) || $req->userId != $userId ? null : $req;
} }
@ -43,10 +43,10 @@ class Data
*/ */
public static function addHistory(string $reqId, string $userId, History $history) public static function addHistory(string $reqId, string $userId, History $history)
{ {
$req = Data::findFullRequestById($reqId, $userId); $req = self::findFullRequestById($reqId, $userId);
if (is_null($req)) throw new \InvalidArgumentException("$reqId not found"); if (is_null($req)) throw new \InvalidArgumentException("$reqId not found");
array_unshift($req->history, $history); array_unshift($req->history, $history);
Document::updateFull(Data::REQ_TABLE, $reqId, $req); Document::updateFull(self::REQ_TABLE, $reqId, $req);
} }
/** /**
@ -58,10 +58,10 @@ class Data
*/ */
public static function addNote(string $reqId, string $userId, Note $note) public static function addNote(string $reqId, string $userId, Note $note)
{ {
$req = Data::findFullRequestById($reqId, $userId); $req = self::findFullRequestById($reqId, $userId);
if (is_null($req)) throw new \InvalidArgumentException("$reqId not found"); if (is_null($req)) throw new \InvalidArgumentException("$reqId not found");
array_unshift($req->notes, $note); array_unshift($req->notes, $note);
Document::updateFull(Data::REQ_TABLE, $reqId, $req); Document::updateFull(self::REQ_TABLE, $reqId, $req);
} }
/** /**
@ -71,7 +71,7 @@ class Data
*/ */
public static function addRequest(Request $req) public static function addRequest(Request $req)
{ {
Document::insert(Data::REQ_TABLE, $req->id, $req); Document::insert(self::REQ_TABLE, $req->id, $req);
} }
/** /**
@ -95,13 +95,13 @@ class Data
*/ */
private static function getJournalByAnswered(string $userId, string $op): array private static function getJournalByAnswered(string $userId, string $op): array
{ {
$sql = Query::selectFromTable(Data::REQ_TABLE) $sql = Query::selectFromTable(self::REQ_TABLE)
. ' WHERE ' . Query::whereDataContains(':criteria') . ' AND ' . Query::whereJsonPathMatches(':path'); . ' WHERE ' . Query::whereDataContains(':criteria') . ' AND ' . Query::whereJsonPathMatches(':path');
$params = [ $params = [
':criteria' => Query::jsonbDocParam([ 'userId' => $userId ]), ':criteria' => Query::jsonbDocParam([ 'userId' => $userId ]),
':path' => '$.history[*].action (@ ' . $op . ' "' . RequestAction::Answered->name . '")' ':path' => '$.history[*].action (@ ' . $op . ' "' . RequestAction::Answered->name . '")'
]; ];
return Data::mapToJournalRequest( return self::mapToJournalRequest(
Document::customList($sql, $params, Request::class, Document::mapFromJson(...)), true); Document::customList($sql, $params, Request::class, Document::mapFromJson(...)), true);
} }
@ -113,7 +113,7 @@ class Data
*/ */
public static function getAnsweredRequests(string $userId): array public static function getAnsweredRequests(string $userId): array
{ {
$answered = Data::getJournalByAnswered($userId, '=='); $answered = self::getJournalByAnswered($userId, '==');
usort($answered, AsOf::newestToOldest(...)); usort($answered, AsOf::newestToOldest(...));
return $answered; return $answered;
} }
@ -126,7 +126,7 @@ class Data
*/ */
public static function getJournal(string $userId): array public static function getJournal(string $userId): array
{ {
$reqs = data::getJournalByAnswered($userId, '<>'); $reqs = self::getJournalByAnswered($userId, '<>');
usort($reqs, AsOf::oldestToNewest(...)); usort($reqs, AsOf::oldestToNewest(...));
return $reqs; return $reqs;
} }

View File

@ -21,10 +21,10 @@ class Configuration
*/ */
public static function getConn(): \PDO public static function getConn(): \PDO
{ {
if (is_null(Configuration::$conn)) { if (is_null(self::$conn)) {
Configuration::$conn = new \PDO(Configuration::$connectionString); self::$conn = new \PDO(self::$connectionString);
} }
return Configuration::$conn; return self::$conn;
} }
} }

View File

@ -41,7 +41,7 @@ class Definition
*/ */
public static function ensureTable(string $name) public static function ensureTable(string $name)
{ {
pdo()->query(Definition::createTable($name))->execute(); pdo()->query(self::createTable($name))->execute();
} }
/** /**
@ -52,6 +52,6 @@ class Definition
*/ */
public static function ensureIndex(string $name, DocumentIndex $type) public static function ensureIndex(string $name, DocumentIndex $type)
{ {
pdo()->query(Definition::createIndex($name, $type))->execute(); pdo()->query(self::createIndex($name, $type))->execute();
} }
} }

View File

@ -21,12 +21,12 @@ class Document
*/ */
public static function mapDocFromJson(string $columnName, array $result, string $className): mixed public static function mapDocFromJson(string $columnName, array $result, string $className): mixed
{ {
if (is_null(Document::$mapper)) { if (is_null(self::$mapper)) {
Document::$mapper = new \JsonMapper(); self::$mapper = new \JsonMapper();
} }
$mapped = new $className(); $mapped = new $className();
Document::$mapper->map(json_decode($result[$columnName]), $mapped); self::$mapper->map(json_decode($result[$columnName]), $mapped);
return $mapped; return $mapped;
} }
@ -39,7 +39,7 @@ class Document
*/ */
public static function mapFromJson(array $result, string $className): mixed public static function mapFromJson(array $result, string $className): mixed
{ {
return Document::mapDocFromJson('data', $result, $className); return self::mapDocFromJson('data', $result, $className);
} }
/** /**
@ -66,7 +66,7 @@ class Document
*/ */
public static function insert(string $tableName, string $docId, array|object $document) public static function insert(string $tableName, string $docId, array|object $document)
{ {
Document::executeNonQuery(Query::insert($tableName), $docId, $document); self::executeNonQuery(Query::insert($tableName), $docId, $document);
} }
/** /**
@ -78,7 +78,7 @@ class Document
*/ */
public static function save(string $tableName, string $docId, array|object $document) public static function save(string $tableName, string $docId, array|object $document)
{ {
Document::executeNonQuery(Query::save($tableName), $docId, $document); self::executeNonQuery(Query::save($tableName), $docId, $document);
} }
/** /**
@ -182,7 +182,7 @@ class Document
*/ */
private static function mapResults(\PDOStatement $stmt, string $className): array private static function mapResults(\PDOStatement $stmt, string $className): array
{ {
return array_map(fn ($it) => Document::mapFromJson($it, $className), $stmt->fetchAll(\PDO::FETCH_ASSOC)); return array_map(fn ($it) => self::mapFromJson($it, $className), $stmt->fetchAll(\PDO::FETCH_ASSOC));
} }
/** /**
@ -194,7 +194,7 @@ class Document
*/ */
public static function findAll(string $tableName, string $className): array public static function findAll(string $tableName, string $className): array
{ {
return Document::mapResults(pdo()->query(Query::selectFromTable($tableName)), $className); return self::mapResults(pdo()->query(Query::selectFromTable($tableName)), $className);
} }
/** /**
@ -211,7 +211,7 @@ class Document
$query->bindParam(':id', $docId); $query->bindParam(':id', $docId);
$query->execute(); $query->execute();
$result = $query->fetch(\PDO::FETCH_ASSOC); $result = $query->fetch(\PDO::FETCH_ASSOC);
return $result ? Document::mapFromJson($result, $className) : null; return $result ? self::mapFromJson($result, $className) : null;
} }
/** /**
@ -239,7 +239,7 @@ class Document
*/ */
public static function findByContains(string $tableName, array|object $criteria, string $className): array public static function findByContains(string $tableName, array|object $criteria, string $className): array
{ {
return Document::mapResults(Document::queryByContains($tableName, $criteria), $className); return self::mapResults(self::queryByContains($tableName, $criteria), $className);
} }
/** /**
@ -252,9 +252,9 @@ class Document
*/ */
public static function findFirstByContains(string $tableName, array|object $criteria, string $className): mixed public static function findFirstByContains(string $tableName, array|object $criteria, string $className): mixed
{ {
$query = Document::queryByContains($tableName, $criteria); $query = self::queryByContains($tableName, $criteria);
$result = $query->fetch(\PDO::FETCH_ASSOC); $result = $query->fetch(\PDO::FETCH_ASSOC);
return $result ? Document::mapFromJson($result, $className) : null; return $result ? self::mapFromJson($result, $className) : null;
} }
/** /**
@ -282,7 +282,7 @@ class Document
*/ */
public static function findByJsonPath(string $tableName, string $jsonPath, string $className): array public static function findByJsonPath(string $tableName, string $jsonPath, string $className): array
{ {
return Document::mapResults(Document::queryByJsonPath($tableName, $jsonPath), $className); return self::mapResults(self::queryByJsonPath($tableName, $jsonPath), $className);
} }
/** /**
@ -295,9 +295,9 @@ class Document
*/ */
public static function findFirstByJsonPath(string $tableName, string $jsonPath, string $className): mixed public static function findFirstByJsonPath(string $tableName, string $jsonPath, string $className): mixed
{ {
$query = Document::queryByJsonPath($tableName, $jsonPath); $query = self::queryByJsonPath($tableName, $jsonPath);
$result = $query->fetch(\PDO::FETCH_ASSOC); $result = $query->fetch(\PDO::FETCH_ASSOC);
return $result ? Document::mapFromJson($result, $className) : null; return $result ? self::mapFromJson($result, $className) : null;
} }
/** /**
@ -309,7 +309,7 @@ class Document
*/ */
public static function updateFull(string $tableName, string $docId, array|object $document) public static function updateFull(string $tableName, string $docId, array|object $document)
{ {
Document::executeNonQuery(Query::updateFull($tableName), $docId, $document); self::executeNonQuery(Query::updateFull($tableName), $docId, $document);
} }
/** /**
@ -321,7 +321,7 @@ class Document
*/ */
public static function updatePartialById(string $tableName, string $docId, array|object $document) public static function updatePartialById(string $tableName, string $docId, array|object $document)
{ {
Document::executeNonQuery(Query::updatePartialById($tableName), $docId, $document); self::executeNonQuery(Query::updatePartialById($tableName), $docId, $document);
} }
/** /**
@ -362,7 +362,7 @@ class Document
*/ */
public static function deleteById(string $tableName, string $docId) public static function deleteById(string $tableName, string $docId)
{ {
Document::executeNonQuery(Query::deleteById($tableName), $docId, []); self::executeNonQuery(Query::deleteById($tableName), $docId, []);
} }
/** /**
@ -423,7 +423,7 @@ class Document
{ {
return array_map( return array_map(
fn ($it) => $mapFunc($it, $className), fn ($it) => $mapFunc($it, $className),
Document::createCustomQuery($sql, $params)->fetchAll(\PDO::FETCH_ASSOC)); self::createCustomQuery($sql, $params)->fetchAll(\PDO::FETCH_ASSOC));
} }
/** /**
@ -437,7 +437,7 @@ class Document
*/ */
public static function customSingle(string $sql, array $params, string $className, callable $mapFunc): mixed public static function customSingle(string $sql, array $params, string $className, callable $mapFunc): mixed
{ {
$result = Document::createCustomQuery($sql, $params)->fetch(\PDO::FETCH_ASSOC); $result = self::createCustomQuery($sql, $params)->fetch(\PDO::FETCH_ASSOC);
return $result ? $mapFunc($result, $className) : null; return $result ? $mapFunc($result, $className) : null;
} }
@ -449,6 +449,6 @@ class Document
*/ */
public static function customNonQuery(string $sql, array $params) public static function customNonQuery(string $sql, array $params)
{ {
Document::createCustomQuery($sql, $params); self::createCustomQuery($sql, $params);
} }
} }

View File

@ -85,7 +85,7 @@ class Query
*/ */
public static function countAll(string $tableName): string public static function countAll(string $tableName): string
{ {
return "SELECT COUNT(id) AS it FROM $tableName"; return "SELECT COUNT(*) AS it FROM $tableName";
} }
/** /**
@ -96,7 +96,7 @@ class Query
*/ */
public static function countByContains(string $tableName): string public static function countByContains(string $tableName): string
{ {
return "SELECT COUNT(id) AS it FROM $tableName WHERE " . Query::whereDataContains('@criteria'); return "SELECT COUNT(*) AS it FROM $tableName WHERE " . self::whereDataContains('@criteria');
} }
/** /**
@ -107,7 +107,7 @@ class Query
*/ */
public static function countByJsonPath(string $tableName): string public static function countByJsonPath(string $tableName): string
{ {
return "SELECT COUNT(id) AS it FROM $tableName WHERE " . Query::whereJsonPathMatches('@path'); return "SELECT COUNT(*) AS it FROM $tableName WHERE " . self::whereJsonPathMatches('@path');
} }
/** /**
@ -129,7 +129,7 @@ class Query
*/ */
public static function existsByContains(string $tableName): string public static function existsByContains(string $tableName): string
{ {
return "SELECT EXISTS (SELECT 1 FROM $tableName WHERE " . Query::whereDataContains('@criteria') . ' AS it'; return "SELECT EXISTS (SELECT 1 FROM $tableName WHERE " . self::whereDataContains('@criteria') . ' AS it';
} }
/** /**
@ -140,7 +140,7 @@ class Query
*/ */
public static function existsByJsonPath(string $tableName): string public static function existsByJsonPath(string $tableName): string
{ {
return "SELECT EXISTS (SELECT 1 FROM $tableName WHERE " . Query::whereJsonPathMatches('@path') . ' AS it'; return "SELECT EXISTS (SELECT 1 FROM $tableName WHERE " . self::whereJsonPathMatches('@path') . ' AS it';
} }
/** /**
@ -151,7 +151,7 @@ class Query
*/ */
public static function findById(string $tableName): string public static function findById(string $tableName): string
{ {
return Query::selectFromTable($tableName) . ' WHERE id = :id'; return self::selectFromTable($tableName) . ' WHERE id = :id';
} }
/** /**
@ -162,7 +162,7 @@ class Query
*/ */
public static function findByContains(string $tableName): string public static function findByContains(string $tableName): string
{ {
return Query::selectFromTable($tableName) . ' WHERE ' . Query::whereDataContains('@criteria'); return self::selectFromTable($tableName) . ' WHERE ' . self::whereDataContains('@criteria');
} }
/** /**
@ -173,7 +173,7 @@ class Query
*/ */
public static function findByJsonPath(string $tableName): string public static function findByJsonPath(string $tableName): string
{ {
return Query::selectFromTable($tableName) . ' WHERE ' . Query::whereJsonPathMatches('@path'); return self::selectFromTable($tableName) . ' WHERE ' . self::whereJsonPathMatches('@path');
} }
/** /**
@ -206,7 +206,7 @@ class Query
*/ */
public static function updatePartialByContains(string $tableName): string public static function updatePartialByContains(string $tableName): string
{ {
return "UPDATE $tableName SET data = data || @data WHERE " . Query::whereDataContains('@criteria'); return "UPDATE $tableName SET data = data || @data WHERE " . self::whereDataContains('@criteria');
} }
/** /**
@ -217,7 +217,7 @@ class Query
*/ */
public static function updatePartialByJsonPath(string $tableName): string public static function updatePartialByJsonPath(string $tableName): string
{ {
return "UPDATE $tableName SET data = data || @data WHERE " . Query::whereJsonPathMatches('@path'); return "UPDATE $tableName SET data = data || @data WHERE " . self::whereJsonPathMatches('@path');
} }
/** /**
@ -239,7 +239,7 @@ class Query
*/ */
public static function deleteByContains(string $tableName): string public static function deleteByContains(string $tableName): string
{ {
return "DELETE FROM $tableName WHERE " . Query::whereDataContains('@criteria'); return "DELETE FROM $tableName WHERE " . self::whereDataContains('@criteria');
} }
/** /**
@ -250,6 +250,6 @@ class Query
*/ */
public static function deleteByJsonPath(string $tableName): string public static function deleteByJsonPath(string $tableName): string
{ {
return "DELETE FROM $tableName WHERE " . Query::whereJsonPathMatches('@path'); return "DELETE FROM $tableName WHERE " . self::whereJsonPathMatches('@path');
} }
} }