ConversionFailed.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * League.Uri (https://uri.thephpleague.com)
  4. *
  5. * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace League\Uri\Exceptions;
  12. use League\Uri\Idna\Error;
  13. use League\Uri\Idna\Result;
  14. use Stringable;
  15. final class ConversionFailed extends SyntaxError
  16. {
  17. private function __construct(
  18. string $message,
  19. private readonly string $host,
  20. private readonly Result $result
  21. ) {
  22. parent::__construct($message);
  23. }
  24. public static function dueToIdnError(Stringable|string $host, Result $result): self
  25. {
  26. $reasons = array_map(fn (Error $error): string => $error->description(), $result->errors());
  27. return new self('Host `'.$host.'` is invalid: '.implode('; ', $reasons).'.', (string) $host, $result);
  28. }
  29. public function getHost(): string
  30. {
  31. return $this->host;
  32. }
  33. public function getResult(): Result
  34. {
  35. return $this->result;
  36. }
  37. }