1<?php 2 3namespace MatrixPhp; 4 5/** 6 * The User class can be used to call user specific functions. 7 * 8 * @package MatrixPhp 9 */ 10class User { 11 12 protected $userId; 13 protected $displayName; 14 protected $api; 15 16 /** 17 * User constructor. 18 * 19 * @param MatrixHttpApi $api 20 * @param string $userId 21 * @param string|null $displayName 22 * @throws Exceptions\ValidationException 23 */ 24 public function __construct(MatrixHttpApi $api, string $userId, ?string $displayName = null) { 25 Util::checkUserId($userId); 26 $this->userId = $userId; 27 $this->displayName = $displayName; 28 $this->api = $api; 29 } 30 31 /** 32 * Get this user's display name. 33 * 34 * @param Room|null $room Optional. When specified, return the display name of the user in this room. 35 * @return string The display name. Defaults to the user ID if not set. 36 * @throws Exceptions\MatrixException 37 * @throws Exceptions\MatrixHttpLibException 38 * @throws Exceptions\MatrixRequestException 39 */ 40 public function getDisplayName(?Room $room = null): string { 41 if ($room) { 42 return array_get($room->getMembersDisplayNames(), $this->userId, $this->userId); 43 } 44 45 if (!$this->displayName) { 46 $this->displayName = $this->api->getDisplayName($this->userId); 47 } 48 49 return $this->displayName ?: $this->userId; 50 } 51 52 /** 53 * Set this users display name. 54 * 55 * @param string $displayName Display Name 56 * @return mixed //FIXME: add proper type 57 * @throws Exceptions\MatrixException 58 * @throws Exceptions\MatrixHttpLibException 59 * @throws Exceptions\MatrixRequestException 60 */ 61 public function setDisplayName(string $displayName) { 62 $this->displayName = $displayName; 63 64 return $this->api->setDisplayName($this->userId, $displayName); 65 } 66 67 /** 68 * @return string|null 69 * @throws Exceptions\MatrixException 70 * @throws Exceptions\MatrixHttpLibException 71 * @throws Exceptions\MatrixRequestException 72 * @throws Exceptions\ValidationException 73 */ 74 public function getAvatarUrl(): ?string { 75 $mxurl = $this->api->getAvatarUrl($this->userId); 76 $url = null; 77 if ($mxurl) { 78 $url = $this->api->getDownloadUrl($mxurl); 79 } 80 81 return $url; 82 } 83 84 /** 85 * Set this users avatar. 86 * 87 * @param string $avatarUrl mxc url from previously uploaded 88 * @return mixed //FIXME: add proper type 89 * @throws Exceptions\MatrixException 90 * @throws Exceptions\MatrixHttpLibException 91 * @throws Exceptions\MatrixRequestException 92 */ 93 public function setAvatarUrl(string $avatarUrl) { 94 return $this->api->setAvatarUrl($this->userId, $avatarUrl); 95 } 96 97 public function userId(): string { 98 return $this->userId; 99 } 100 101 102} 103