]> git.mxchange.org Git - friendica.git/blob - src/Protocol/WebFingerUri.php
Merge remote-tracking branch 'upstream/develop' into user-defined-channels
[friendica.git] / src / Protocol / WebFingerUri.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Protocol;
23
24 use GuzzleHttp\Psr7\Uri;
25
26 class WebFingerUri
27 {
28         /**
29          * @var string
30          */
31         private $user;
32         /**
33          * @var string
34          */
35         private $host;
36         /**
37          * @var int|null
38          */
39         private $port;
40         /**
41          * @var string|null
42          */
43         private $path;
44
45         private function __construct(string $user, string $host, int $port = null, string $path = null)
46         {
47                 $this->user = $user;
48                 $this->host = $host;
49                 $this->port = $port;
50                 $this->path = $path;
51
52                 $this->validate();
53         }
54
55         /**
56          * @param string $addr
57          * @return WebFingerUri
58          */
59         public static function fromString(string $addr): WebFingerUri
60         {
61                 $uri = new Uri('acct://' . preg_replace('/^acct:/', '', $addr));
62
63                 return new self($uri->getUserInfo(), $uri->getHost(), $uri->getPort(), $uri->getPath());
64         }
65
66         private function validate()
67         {
68                 if (!$this->user) {
69                         throw new \InvalidArgumentException('WebFinger URI User part is required');
70                 }
71
72                 if (!$this->host) {
73                         throw new \InvalidArgumentException('WebFinger URI Host part is required');
74                 }
75         }
76
77         public function getUser(): string
78         {
79                 return $this->user;
80         }
81
82         public function getHost(): string
83         {
84                 return $this->host;
85         }
86
87         public function getFullHost(): string
88         {
89                 return $this->host
90                         . ($this->port ? ':' . $this->port : '') .
91                         ($this->path ?: '');
92         }
93
94         public function getLongForm(): string
95         {
96                 return 'acct:' . $this->getShortForm();
97         }
98
99         public function getShortForm(): string
100         {
101                 return $this->user . '@' . $this->getFullHost();
102         }
103
104         public function getAddr(): string
105         {
106                 return $this->getShortForm();
107         }
108
109         public function __toString(): string
110         {
111                 return $this->getShortForm();
112         }
113 }