summaryrefslogtreecommitdiff
path: root/src/Support/Parser.php
blob: c4322405dc81198c646cef512e957740b9cf7e73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

namespace App\Support;

class Parser
{
  /**
   * @return array<string, string>
   */
  public static function parseUser(string $user): array
  {
    $username = $user;
    $server = "";

    if (str_starts_with($user, "@")) {
      $username = substr($user, 1);
      $usernameParts = explode(":", $username);
      $username = $usernameParts[0];
      $server = $usernameParts[1];
    }

    return [
      "username" => $username,
      "server" => $server,
    ];
  }

  /**
   * @return array<string, string>
   */
  public static function parseRoomAlias(string $alias): array
  {
    $name = "";
    $server = "";

    if (str_starts_with($alias, "#")) {
      $alias = substr($alias, 1);
    }

    $parts = explode(":", $alias);
    $name = $parts[0];
    $server = $parts[1];

    if ($server === "localhost" && str_starts_with($_ENV["DOMAIN"], "localhost")) {
      $server = $_ENV["DOMAIN"];
    }

    return [
      "name" => $name,
      "server" => $server,
    ];
  }
}