예제 #1
0
 /**
  * Returns the IP address from the host component.
  * @return string|null IP address from the host or null if the host is not an IP address
  */
 public function getIpAddress()
 {
     $pattern = new UriPattern();
     $pattern->matchHost($this->getHost(), $match);
     if (isset($match['IPv4address'])) {
         return $match['IPv4address'];
     } elseif (isset($match['IP_literal'])) {
         return preg_replace('/^\\[(v[^.]+\\.)?([^\\]]+)\\]$/', '$2', $match['IP_literal']);
     }
     return null;
 }
예제 #2
0
 /**
  * Parses the URL using the generic URI syntax.
  *
  * This method returns the `Uri` instance constructed from the components
  * parsed from the URL. The URL is parsed using either the absolute URI
  * pattern or the relative URI pattern based on which one matches the
  * provided string. If the URL cannot be parsed as a valid URI, null is
  * returned instead.
  *
  * @param string $uri The URL to parse
  * @return Uri|null The parsed URL or null if the URL is invalid
  */
 public function parse($uri)
 {
     if (!$this->isValidString($uri)) {
         return null;
     }
     $pattern = new UriPattern();
     $pattern->allowNonAscii($this->mode !== self::MODE_RFC3986);
     if ($pattern->matchUri($uri, $match)) {
         try {
             return $this->buildUri($match);
         } catch (\InvalidArgumentException $exception) {
             return null;
         }
     }
     return null;
 }