ErrorCodes.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\Core\HandlerError\ErrorCodes;
  3. abstract class ErrorCodes
  4. {
  5. const MESSAGE = 'message';
  6. const LOG = 'log';
  7. const CODE = 'code';
  8. const DEV_MESSAGE = 'dev_message';
  9. /**
  10. * @param null $code
  11. * @param null $errorToken
  12. * @return ErrorCode|null
  13. */
  14. public function getErrorMessageByCode($code = null, $errorToken = null)
  15. {
  16. $constantName = get_class($this) . '::' . $code;
  17. if (!defined($constantName))
  18. {
  19. return $this->getUndefinedErrorMessage($code, $errorToken);
  20. }
  21. return $this->getErrorCode($code, constant($constantName), $errorToken);
  22. }
  23. /**
  24. * @param null|string $code
  25. * @param null|string $errorToken
  26. * @return ErrorCode|null
  27. */
  28. public function getUndefinedErrorMessage($code = null, $errorToken = null)
  29. {
  30. return $this->getErrorCode($code, 'Invalid Error Code!', $errorToken);
  31. }
  32. /**
  33. * @param null|string $code
  34. * @param null|string $message
  35. * @param null|string $token
  36. * @return ErrorCode|null
  37. */
  38. protected function getErrorCode($code = null, $message = null, $token = null)
  39. {
  40. $token = new ErrorCode($code, $message, ($token ? : $this->genToken()));
  41. return $token;
  42. }
  43. /**
  44. * returns a string
  45. */
  46. protected function genToken()
  47. {
  48. return md5(time());
  49. }
  50. /**
  51. * @param null|string $code
  52. * @return bool
  53. */
  54. public function errorCodeExists($code = null)
  55. {
  56. $constantName = get_class($this) . '::' . $code;
  57. if (!defined($constantName))
  58. {
  59. return false;
  60. }
  61. return true;
  62. }
  63. }