当前位置: 首页>>代码示例>>PHP>>正文


PHP Uri::isRelativePathReference方法代码示例

本文整理汇总了PHP中GuzzleHttp\Psr7\Uri::isRelativePathReference方法的典型用法代码示例。如果您正苦于以下问题:PHP Uri::isRelativePathReference方法的具体用法?PHP Uri::isRelativePathReference怎么用?PHP Uri::isRelativePathReference使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在GuzzleHttp\Psr7\Uri的用法示例。


在下文中一共展示了Uri::isRelativePathReference方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: relativize

 /**
  * Returns the target URI as a relative reference from the base URI.
  *
  * This method is the counterpart to resolve():
  *
  *    (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target))
  *
  * One use-case is to use the current request URI as base URI and then generate relative links in your documents
  * to reduce the document size or offer self-contained downloadable document archives.
  *
  *    $base = new Uri('http://example.com/a/b/');
  *    echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c'));  // prints 'c'.
  *    echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y'));  // prints '../x/y'.
  *    echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'.
  *    echo UriResolver::relativize($base, new Uri('http://example.org/a/b/'));   // prints '//example.org/a/b/'.
  *
  * This method also accepts a target that is already relative and will try to relativize it further. Only a
  * relative-path reference will be returned as-is.
  *
  *    echo UriResolver::relativize($base, new Uri('/a/b/c'));  // prints 'c' as well
  *
  * @param UriInterface $base   Base URI
  * @param UriInterface $target Target URI
  *
  * @return UriInterface The relative URI reference
  */
 public static function relativize(UriInterface $base, UriInterface $target)
 {
     if ($target->getScheme() !== '' && ($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '')) {
         return $target;
     }
     if (Uri::isRelativePathReference($target)) {
         // As the target is already highly relative we return it as-is. It would be possible to resolve
         // the target with `$target = self::resolve($base, $target);` and then try make it more relative
         // by removing a duplicate query. But let's not do that automatically.
         return $target;
     }
     if ($target->getAuthority() !== '' && $base->getAuthority() !== $target->getAuthority()) {
         return $target->withScheme('');
     }
     // We must remove the path before removing the authority because if the path starts with two slashes, the URI
     // would turn invalid. And we also cannot set a relative path before removing the authority, as that is also
     // invalid.
     $emptyPathUri = $target->withScheme('')->withPath('')->withUserInfo('')->withPort(null)->withHost('');
     if ($base->getPath() !== $target->getPath()) {
         return $emptyPathUri->withPath(self::getRelativePath($base, $target));
     }
     if ($base->getQuery() === $target->getQuery()) {
         // Only the target fragment is left. And it must be returned even if base and target fragment are the same.
         return $emptyPathUri->withQuery('');
     }
     // If the base URI has a query but the target has none, we cannot return an empty path reference as it would
     // inherit the base query component when resolving.
     if ($target->getQuery() === '') {
         $segments = explode('/', $target->getPath());
         $lastSegment = end($segments);
         return $emptyPathUri->withPath($lastSegment === '' ? './' : $lastSegment);
     }
     return $emptyPathUri;
 }
开发者ID:nystudio107,项目名称:instantanalytics,代码行数:60,代码来源:UriResolver.php

示例2: normalize

 /**
  * Returns a normalized URI.
  *
  * The scheme and host component are already normalized to lowercase per PSR-7 UriInterface.
  * This methods adds additional normalizations that can be configured with the $flags parameter.
  *
  * PSR-7 UriInterface cannot distinguish between an empty component and a missing component as
  * getQuery(), getFragment() etc. always return a string. This means the URIs "/?#" and "/" are
  * treated equivalent which is not necessarily true according to RFC 3986. But that difference
  * is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well.
  *
  * @param UriInterface $uri   The URI to normalize
  * @param int          $flags A bitmask of normalizations to apply, see constants
  *
  * @return UriInterface The normalized URI
  * @link https://tools.ietf.org/html/rfc3986#section-6.2
  */
 public static function normalize(UriInterface $uri, $flags = self::PRESERVING_NORMALIZATIONS)
 {
     if ($flags & self::CAPITALIZE_PERCENT_ENCODING) {
         $uri = self::capitalizePercentEncoding($uri);
     }
     if ($flags & self::DECODE_UNRESERVED_CHARACTERS) {
         $uri = self::decodeUnreservedCharacters($uri);
     }
     if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' && ($uri->getScheme() === 'http' || $uri->getScheme() === 'https')) {
         $uri = $uri->withPath('/');
     }
     if ($flags & self::REMOVE_DEFAULT_HOST && $uri->getScheme() === 'file' && $uri->getHost() === 'localhost') {
         $uri = $uri->withHost('');
     }
     if ($flags & self::REMOVE_DEFAULT_PORT && $uri->getPort() !== null && Uri::isDefaultPort($uri)) {
         $uri = $uri->withPort(null);
     }
     if ($flags & self::REMOVE_DOT_SEGMENTS && !Uri::isRelativePathReference($uri)) {
         $uri = $uri->withPath(UriResolver::removeDotSegments($uri->getPath()));
     }
     if ($flags & self::REMOVE_DUPLICATE_SLASHES) {
         $uri = $uri->withPath(preg_replace('#//++#', '/', $uri->getPath()));
     }
     if ($flags & self::SORT_QUERY_PARAMETERS && $uri->getQuery() !== '') {
         $queryKeyValues = explode('&', $uri->getQuery());
         sort($queryKeyValues);
         $uri = $uri->withQuery(implode('&', $queryKeyValues));
     }
     return $uri;
 }
开发者ID:nystudio107,项目名称:instantanalytics,代码行数:47,代码来源:UriNormalizer.php

示例3: testIsRelativePathReference

 public function testIsRelativePathReference()
 {
     $this->assertFalse(Uri::isRelativePathReference(new Uri('http://example.org')));
     $this->assertFalse(Uri::isRelativePathReference(new Uri('//example.org')));
     $this->assertFalse(Uri::isRelativePathReference(new Uri('/abs-path')));
     $this->assertTrue(Uri::isRelativePathReference(new Uri('rel-path')));
     $this->assertTrue(Uri::isRelativePathReference(new Uri('')));
 }
开发者ID:nystudio107,项目名称:instantanalytics,代码行数:8,代码来源:UriTest.php


注:本文中的GuzzleHttp\Psr7\Uri::isRelativePathReference方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。