ConversionFailed.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 BackedEnum;
  13. use League\Uri\Idna\Error;
  14. use League\Uri\Idna\Result;
  15. use Stringable;
  16. final class ConversionFailed extends SyntaxError
  17. {
  18. private function __construct(
  19. string $message,
  20. private readonly string $host,
  21. private readonly Result $result
  22. ) {
  23. parent::__construct($message);
  24. }
  25. public static function dueToIdnError(BackedEnum|Stringable|string $host, Result $result): self
  26. {
  27. $reasons = array_map(fn (Error $error): string => $error->description(), $result->errors());
  28. if ($host instanceof BackedEnum) {
  29. $host = (string) $host->value;
  30. }
  31. return new self('Host `'.$host.'` is invalid: '.implode('; ', $reasons).'.', (string) $host, $result);
  32. }
  33. public function getHost(): string
  34. {
  35. return $this->host;
  36. }
  37. public function getResult(): Result
  38. {
  39. return $this->result;
  40. }
  41. }