JsonEncodingException.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Database\Model;
  12. use RuntimeException;
  13. class JsonEncodingException extends RuntimeException
  14. {
  15. /**
  16. * Create a new JSON encoding exception for the model.
  17. *
  18. * @param string $message
  19. * @param mixed $model
  20. * @return static
  21. */
  22. public static function forModel($model, $message)
  23. {
  24. return new static('Error encoding model [' . get_class($model) . '] with ID [' . $model->getKey() . '] to JSON: ' . $message);
  25. }
  26. /**
  27. * Create a new JSON encoding exception for an attribute.
  28. *
  29. * @param string $message
  30. * @param mixed $model
  31. * @param mixed $key
  32. * @return static
  33. */
  34. public static function forAttribute($model, $key, $message)
  35. {
  36. $class = get_class($model);
  37. return new static("Unable to encode attribute [{$key}] for model [{$class}] to JSON: {$message}.");
  38. }
  39. }