/** * Constructs an instance of this class. * * @param string $ip server IP address * @param int|null $port server port number */ public function __construct(string $ip, $port = null) { if (IP::isValid($ip)) { $this->ip = new IP($ip); } else { $this->ip = new IP('127.0.0.1'); } $this->port = $port; }
/** * Returns the client of the request. * * @return ClientRole */ public function getClient() : ClientRole { $ip = ''; $values = $this->headers->getValues('X-Forwarded-For'); if (count($values) > 0 && IP::isValid($values[count($values) - 1])) { $ip = $values[count($values) - 1]; } elseif (IP::isValid($this->headers->get('Client-IP', ''))) { $ip = $this->headers->get('Client-IP'); } elseif (IP::isValid($this->server->get('REMOTE_ADDR', ''))) { $ip = $this->server->get('REMOTE_ADDR'); } $port = $this->server->get('REMOTE_PORT'); return new ClientRole($ip, $port); }