Add new classes and methods for session management
This commit is contained in:
parent
b9c84aeb27
commit
764ec51fa4
12 changed files with 1026 additions and 28 deletions
96
src/Socialbox/Objects/PeerAddress.php
Normal file
96
src/Socialbox/Objects/PeerAddress.php
Normal file
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Objects;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Socialbox\Classes\Validator;
|
||||
use Socialbox\Enums\ReservedUsernames;
|
||||
|
||||
class PeerAddress
|
||||
{
|
||||
private string $username;
|
||||
private string $domain;
|
||||
|
||||
/**
|
||||
* Constructs a PeerAddress object from a given username and domain
|
||||
*
|
||||
* @param string $username The username of the peer
|
||||
* @param string $domain The domain of the peer
|
||||
*/
|
||||
public function __construct(string $username, string $domain)
|
||||
{
|
||||
$this->username = $username;
|
||||
$this->domain = $domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a PeerAddress from a full peer address (eg; john@example.com)
|
||||
*
|
||||
* @param string $address The full address of the peer
|
||||
* @return PeerAddress The constructed PeerAddress object
|
||||
*/
|
||||
public static function fromAddress(string $address): PeerAddress
|
||||
{
|
||||
if(!Validator::validatePeerAddress($address))
|
||||
{
|
||||
throw new InvalidArgumentException("Invalid peer address: $address");
|
||||
}
|
||||
|
||||
$parts = explode('@', $address);
|
||||
return new PeerAddress($parts[0], $parts[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the username of the peer
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUsername(): string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the domain of the peer
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDomain(): string
|
||||
{
|
||||
return $this->domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the peer is the host
|
||||
*
|
||||
* @return bool True if the peer is the host, false otherwise
|
||||
*/
|
||||
public function isHost(): bool
|
||||
{
|
||||
return $this->username === ReservedUsernames::HOST->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the peer requires authentication, for example, the anonymous user does not require authentication
|
||||
*
|
||||
* @return bool True if authentication is required, false otherwise
|
||||
*/
|
||||
public function authenticationRequired(): bool
|
||||
{
|
||||
return match($this->username)
|
||||
{
|
||||
ReservedUsernames::ANONYMOUS->value => false,
|
||||
default => true
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full address of the peer
|
||||
*
|
||||
* @return string The full address of the peer
|
||||
*/
|
||||
public function getAddress(): string
|
||||
{
|
||||
return sprintf("%s@%s", $this->username, $this->domain);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue