1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace app\Types\DatabaseObjects;
- use app\Hajeebtok;
- use app\Interfaces\IDatabaseObject;
- use app\Logger;
- use app\Types\LinkEnum;
- use app\Exceptions\LinkNotFoundException;
- use app\Exceptions\SecurityFaultException;
- class Link implements IDatabaseObject
- {
- public private(set) ?int $id;
- public private(set) ?int $account_id;
- public private(set) ?LinkEnum $type;
- public private(set) ?string $url;
- public function __construct(?int $id = null, ?int $account_id = null, ?LinkEnum $type = null, ?string $url = null) {
- $this->id = $id;
- $this->account_id = $account_id;
- $this->type = $type;
- $this->url = $url;
- }
- /**
- * Creates the table for the object type in the database.
- */
- public static function CreateTable(): void {
- throw new SecurityFaultException("Attempt to create table on link object.");
- }
- /**
- * Drops the table for the object type from the database.
- */
- public static function DropTable(): void {
- throw new SecurityFaultException("Attempt to drop table on link object.");
- }
- /**
- * Saves the object to the database.
- */
- public function Save() {
- Hajeebtok::$Database->Query("INSERT INTO links (account_id, type, url) VALUES (:account_id, :type, :url)", [
- "account_id" => $this->account_id,
- "type" => $this->type->value,
- "url" => $this->url
- ]);
- $this->id = Hajeebtok::$Database->LastInsertId();
- Logger::Debug("Saved link id ($this->id).");
- }
- /**
- * Deletes the object from the database.
- */
- public function Delete() {
- Hajeebtok::$Database->Query("DELETE FROM link WHERE id = :id", ["id" => $this->id]);
- }
- /**
- * Loads the object from the database.
- */
- public function Load() {
- if($this->id === null) throw new LinkNotFoundException(0, 404);
- $data = Hajeebtok::$Database->Row("SELECT * FROM link WHERE id = :id", ["id" => $this->id]);
- if(empty($data)) throw new LinkNotFoundException($this->id, 404);
- $this->account_id = $data["account_id"];
- $this->type = LinkEnum::tryFrom($data["type"]);
- $this->url = $data["url"];
- }
- public function LoadMany(): array {
- if(empty($this->account_id)) throw new LinkNotFoundException(0, 404);
- $data = Hajeebtok::$Database->Query("SELECT * FROM links WHERE account_id = :account_id", ["account_id" => $this->account_id]);
- if(empty($data)) throw new LinkNotFoundException(0, 404);
- return $data;
- }
- }
|