[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/algo26-matthias/idna-convert/src/ -> ToIdn.php (source)

   1  <?php
   2  namespace Algo26\IdnaConvert;
   3  
   4  use Algo26\IdnaConvert\Exception\InvalidCharacterException;
   5  use Algo26\IdnaConvert\Exception\InvalidIdnVersionException;
   6  use Algo26\IdnaConvert\Punycode\ToPunycode;
   7  use Algo26\IdnaConvert\TranscodeUnicode\TranscodeUnicode;
   8  
   9  class ToIdn extends AbstractIdnaConvert implements IdnaConvertInterface
  10  {
  11      /** @var TranscodeUnicode */
  12      private $unicodeTransCoder;
  13  
  14      /** @var ToPunycode */
  15      private $punycodeEncoder;
  16  
  17      /**
  18       * @throws InvalidIdnVersionException
  19       */
  20      public function __construct($idnVersion = null)
  21      {
  22          $this->unicodeTransCoder = new TranscodeUnicode();
  23          $this->punycodeEncoder = new ToPunycode($idnVersion);
  24      }
  25  
  26      /**
  27       * @param string $host
  28       *
  29       * @return string
  30       * @throws InvalidCharacterException
  31       * @throws Exception\AlreadyPunycodeException
  32       */
  33      public function convert(string $host): string
  34      {
  35          if (strlen($host) === 0) {
  36              return $host;
  37          }
  38  
  39          if (strpos('/', $host) !== false
  40              || strpos(':', $host) !== false
  41              || strpos('?', $host) !== false
  42              || strpos('@', $host) !== false
  43          ) {
  44              throw new InvalidCharacterException('Neither email addresses nor URLs are allowed', 205);
  45          }
  46  
  47          // These three punctuation characters are treated like the dot
  48          $host = str_replace(['。', '.', '。'], '.', $host);
  49  
  50          // Operate per label
  51          $hostLabels = explode('.', $host);
  52          foreach ($hostLabels as $index => $label) {
  53              $asUcs4Array = $this->unicodeTransCoder->convert(
  54                  $label,
  55                  $this->unicodeTransCoder::FORMAT_UTF8,
  56                  $this->unicodeTransCoder::FORMAT_UCS4_ARRAY
  57              );
  58              $encoded = $this->punycodeEncoder->convert($asUcs4Array);
  59              if ($encoded) {
  60                  $hostLabels[$index] = $encoded;
  61              }
  62          }
  63  
  64          return implode('.', $hostLabels);
  65      }
  66  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer