matrix-php-sdk/src/User.php

102 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2018-11-27 13:59:27 +01:00
<?php
namespace Aryess\PhpMatrixSdk;
/**
* The User class can be used to call user specific functions.
*
* @package Aryess\PhpMatrixSdk
*/
class User {
protected $userId;
protected $displayName;
protected $api;
/**
* User constructor.
*
* @param MatrixHttpApi $api
* @param string $userId
* @param string|null $displayName
* @throws Exceptions\ValidationException
*/
public function __construct(MatrixHttpApi $api, string $userId, ?string $displayName = null) {
Util::checkUserId($userId);
$this->userId = $userId;
$this->displayName = $displayName;
$this->api = $api;
}
/**
* Get this user's display name.
*
* @param Room|null $room Optional. When specified, return the display name of the user in this room.
* @return string The display name. Defaults to the user ID if not set.
2018-12-01 01:47:23 +01:00
* @throws Exceptions\MatrixException
* @throws Exceptions\MatrixHttpLibException
* @throws Exceptions\MatrixRequestException
2018-11-27 13:59:27 +01:00
*/
public function getDisplayName(?Room $room = null): string {
2018-12-02 11:57:02 +01:00
if ($room) {
2018-11-27 13:59:27 +01:00
return array_get($room->getMembersDisplayNames(), $this->userId, $this->userId);
}
2018-12-02 11:57:02 +01:00
if (!$this->displayName) {
2018-11-27 13:59:27 +01:00
$this->displayName = $this->api->getDisplayName($this->userId);
}
2018-12-02 11:57:02 +01:00
return $this->displayName ?: $this->userId;
2018-11-27 13:59:27 +01:00
}
/**
* Set this users display name.
*
* @param string $displayName Display Name
* @return mixed //FIXME: add proper type
2018-12-01 01:47:23 +01:00
* @throws Exceptions\MatrixException
* @throws Exceptions\MatrixHttpLibException
* @throws Exceptions\MatrixRequestException
2018-11-27 13:59:27 +01:00
*/
public function setDisplayName(string $displayName) {
$this->displayName = $displayName;
return $this->api->setDisplayName($this->userId, $displayName);
}
2018-12-01 01:47:23 +01:00
/**
* @return string|null
* @throws Exceptions\MatrixException
* @throws Exceptions\MatrixHttpLibException
* @throws Exceptions\MatrixRequestException
* @throws Exceptions\ValidationException
*/
2018-11-27 13:59:27 +01:00
public function getAvatarUrl(): ?string {
$mxurl = $this->api->getAvatarUrl($this->userId);
$url = null;
2018-12-02 11:57:02 +01:00
if ($mxurl) {
2018-11-27 13:59:27 +01:00
$url = $this->api->getDownloadUrl($mxurl);
}
return $url;
}
/**
* Set this users avatar.
*
2018-12-04 08:58:23 +01:00
* @param string $avatarUrl mxc url from previously uploaded
2018-11-27 13:59:27 +01:00
* @return mixed //FIXME: add proper type
2018-12-01 01:47:23 +01:00
* @throws Exceptions\MatrixException
* @throws Exceptions\MatrixHttpLibException
* @throws Exceptions\MatrixRequestException
2018-11-27 13:59:27 +01:00
*/
2018-12-04 08:58:23 +01:00
public function setAvatarUrl(string $avatarUrl) {
2018-11-27 13:59:27 +01:00
return $this->api->setAvatarUrl($this->userId, $avatarUrl);
}
2018-12-02 11:57:02 +01:00
public function userId(): string {
return $this->userId;
}
2018-11-27 13:59:27 +01:00
}