Open Journal Systems  3.3.0
Utils.php
1 <?php
2 namespace GuzzleHttp;
3 
7 
8 final class Utils
9 {
18  public static function currentTime()
19  {
20  return function_exists('hrtime') ? hrtime(true) / 1e9 : microtime(true);
21  }
22 
31  public static function idnUriConvert(UriInterface $uri, $options = 0)
32  {
33  if ($uri->getHost()) {
34  $asciiHost = self::idnToAsci($uri->getHost(), $options, $info);
35  if ($asciiHost === false) {
36  $errorBitSet = isset($info['errors']) ? $info['errors'] : 0;
37 
38  $errorConstants = array_filter(array_keys(get_defined_constants()), function ($name) {
39  return substr($name, 0, 11) === 'IDNA_ERROR_';
40  });
41 
42  $errors = [];
43  foreach ($errorConstants as $errorConstant) {
44  if ($errorBitSet & constant($errorConstant)) {
45  $errors[] = $errorConstant;
46  }
47  }
48 
49  $errorMessage = 'IDN conversion failed';
50  if ($errors) {
51  $errorMessage .= ' (errors: ' . implode(', ', $errors) . ')';
52  }
53 
54  throw new InvalidArgumentException($errorMessage);
55  } else {
56  if ($uri->getHost() !== $asciiHost) {
57  // Replace URI only if the ASCII version is different
58  $uri = $uri->withHost($asciiHost);
59  }
60  }
61  }
62 
63  return $uri;
64  }
65 
73  private static function idnToAsci($domain, $options, &$info = [])
74  {
75  if (\preg_match('%^[ -~]+$%', $domain) === 1) {
76  return $domain;
77  }
78 
79  if (\extension_loaded('intl') && defined('INTL_IDNA_VARIANT_UTS46')) {
80  return \idn_to_ascii($domain, $options, INTL_IDNA_VARIANT_UTS46, $info);
81  }
82 
83  /*
84  * The Idn class is marked as @internal. We've locked the version to
85  * symfony/polyfill-intl-idn to avoid issues in the future.
86  */
87  return Idn::idn_to_ascii($domain, $options, Idn::INTL_IDNA_VARIANT_UTS46, $info);
88  }
89 }
GuzzleHttp
Definition: vendor/guzzlehttp/guzzle/src/Client.php:2
Psr\Http\Message\UriInterface\getHost
getHost()
GuzzleHttp\Utils
Definition: Utils.php:8
Symfony\Polyfill\Intl\Idn\Idn
Definition: Idn.php:44
Psr\Http\Message\UriInterface
Definition: UriInterface.php:24
GuzzleHttp\Utils\idnUriConvert
static idnUriConvert(UriInterface $uri, $options=0)
Definition: Utils.php:31
GuzzleHttp\Utils\currentTime
static currentTime()
Definition: Utils.php:18
Psr\Http\Message\UriInterface\withHost
withHost($host)
GuzzleHttp\Exception\InvalidArgumentException
Definition: vendor/guzzlehttp/guzzle/src/Exception/InvalidArgumentException.php:5