* @copyright 2014 Fabian Grutschus. All rights reserved. * @license BSD * @link http://github.com/fabiang/xmpp */ namespace Fabiang\Xmpp; use Fabiang\Xmpp\Connection\ConnectionInterface; use Fabiang\Xmpp\Protocol\ImplementationInterface; use Fabiang\Xmpp\Protocol\DefaultImplementation; use Psr\Log\LoggerInterface; /** * Xmpp connection options. * * @package Xmpp */ class Options { /** * * @var ImplementationInterface */ protected $implementation; /** * * @var string */ protected $address; /** * Connection object. * * @var ConnectionInterface */ protected $connection; /** * PSR-3 Logger interface. * * @var LoggerInterface */ protected $logger; /** * * @var string */ protected $to; /** * * @var string */ protected $username; /** * * @var string */ protected $password; /** * * @var string */ protected $jid; /** * * @var string */ protected $sid; /** * * @var boolean */ protected $authenticated = false; /** * * @var array */ protected $users = []; /** * Timeout for connection. * * @var integer */ protected $timeout = 30; /** * Authentication methods. * * @var array */ protected $authenticationClasses = [ 'digest-md5' => '\\Fabiang\\Xmpp\\EventListener\\Stream\\Authentication\\DigestMd5', 'plain' => '\\Fabiang\\Xmpp\\EventListener\\Stream\\Authentication\\Plain', 'anonymous' => '\\Fabiang\\Xmpp\\EventListener\\Stream\\Authentication\\Anonymous' ]; /** * Options used to create a stream context * * @var array */ protected $contextOptions = []; /** * Constructor. * * @param string $address Server address */ public function __construct($address = null) { if (null !== $address) { $this->setAddress($address); } } /** * Get protocol implementation. * * @return ImplementationInterface */ public function getImplementation() { if (null === $this->implementation) { $this->setImplementation(new DefaultImplementation()); } return $this->implementation; } /** * Set protocol implementation. * * @param ImplementationInterface $implementation * @return $this */ public function setImplementation(ImplementationInterface $implementation) { $this->implementation = $implementation; return $this; } /** * Get server address. * * @return string */ public function getAddress() { return $this->address; } /** * Set server address. * * When a address is passed this setter also calls setTo with the hostname part of the address. * * @param string $address Server address * @return $this */ public function setAddress($address) { $this->address = (string) $address; if (false !== ($host = parse_url($address, PHP_URL_HOST))) { $this->setTo($host); } return $this; } /** * Get connection object. * * @return ConnectionInterface */ public function getConnection() { return $this->connection; } /** * Set connection object. * * @param ConnectionInterface $connection * @return $this */ public function setConnection(ConnectionInterface $connection) { $this->connection = $connection; return $this; } /** * Get logger instance. * * @return LoggerInterface */ public function getLogger() { return $this->logger; } /** * Set logger instance. * * @param \Psr\Log\LoggerInterface $logger PSR-3 Logger * @return $this */ public function setLogger(LoggerInterface $logger) { $this->logger = $logger; return $this; } /** * Get server name. * * @return string */ public function getTo() { return $this->to; } /** * Set server name. * * This value is send to the server in requests as to="" attribute. * * @param string $to * @return $this */ public function setTo($to) { $this->to = (string) $to; return $this; } /** * Get username. * * @return string */ public function getUsername() { return $this->username; } /** * Set username. * * @param string $username * @return $this */ public function setUsername($username) { $this->username = (string) $username; return $this; } /** * Get resource. * * @return string */ public function getResource() { $username = $this->getUsername(); $username = explode('/', $username); return isset($username[1]) ? $username[1] : ''; } /** * Get password. * * @return string */ public function getPassword() { return $this->password; } /** * Set password. * * @param string $password * @return $this */ public function setPassword($password) { $this->password = (string) $password; return $this; } /** * Get users jid. * * @return string */ public function getJid() { return $this->jid; } /** * Set users jid. * * @param string $jid * @return $this */ public function setJid($jid) { $this->jid = (string) $jid; return $this; } /** * Get users jid. * * @return string */ public function getSid() { return $this->sid; } /** * Set users jid. * * @param string $jid * @return $this */ public function setSid($sid) { $this->sid = (string) $sid; return $this; } /** * Is user authenticated. * * @return boolean */ public function isAuthenticated() { return $this->authenticated; } /** * Set authenticated. * * @param boolean $authenticated Flag * @return $this */ public function setAuthenticated($authenticated) { $this->authenticated = (bool) $authenticated; return $this; } /** * Get users. * * @return Protocol\User\User[] */ public function getUsers() { return $this->users; } /** * Set users. * * @param array $users User list * @return $this */ public function setUsers(array $users) { $this->users = $users; return $this; } /** * Get authentication classes. * * @return array */ public function getAuthenticationClasses() { return $this->authenticationClasses; } /** * * @param array $authenticationClasses Authentication classes * @return $this */ public function setAuthenticationClasses(array $authenticationClasses) { $this->authenticationClasses = $authenticationClasses; return $this; } /** * Get timeout for connection. * * @return integer */ public function getTimeout() { return $this->timeout; } /** * Set timeout for connection. * * @param integer $timeout Seconds * @return \Fabiang\Xmpp\Options */ public function setTimeout($timeout) { $this->timeout = (int) $timeout; return $this; } /** * Get context options for connection * * @return array */ public function getContextOptions() { return $this->contextOptions; } /** * Set context options for connection * * @param array $contextOptions * @return \Fabiang\Xmpp\Options */ public function setContextOptions($contextOptions) { $this->contextOptions = (array) $contextOptions; return $this; } }