diff options
Diffstat (limited to 'Digitigrade/Model/Instance.php')
| -rw-r--r-- | Digitigrade/Model/Instance.php | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/Digitigrade/Model/Instance.php b/Digitigrade/Model/Instance.php new file mode 100644 index 0000000..2924f71 --- /dev/null +++ b/Digitigrade/Model/Instance.php @@ -0,0 +1,48 @@ +<?php +namespace Digitigrade\Model; + +use Digitigrade\Model; + +class Instance extends FetchableModel { + public ?int $id; + public string $domain; + public string $name; + public ?string $description; + public ?string $icon; // uri + public string $softwareName; + public ?string $softwareDescription; + public ?string $softwareVersion; + public ?string $softwareHomepage; + + public InstanceEndpoints $endpoints; + + protected function getUpdateWhereClause(\PDO $db): ?string { + if (self::findWhere('domain = ?', [$this->domain]) != null) + return 'domain = ' . $db->quote($this->domain); + if (self::findWhere('id = ?', [$this->id]) != null) + return "id = $this->id"; + return null; + } + + public static function findByDomain(string $domain): ?self { + return self::findByUri("https://$domain/.well-known/pawpub-instance"); + } + + public function hydrate() { + $this->endpoints = InstanceEndpoints::findWhere('instance_id = ?', [$this->id]); + } + + protected function beforeSave(string $uri) { + $this->domain = explode('/', $uri)[2]; + } + + public static function findByUri(string $uri, bool $autoSave = true, bool $forceRefetch = false): ?static { + if (!$forceRefetch) { + $domain = explode('/', $uri)[2]; + $obj = self::findWhere('domain = ?', [$domain]); + if ($obj != null) + return $obj; + } + return self::fetchFromRemote($uri, $autoSave); + } +}
\ No newline at end of file |
