AppDomain.php 984 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Support;
  3. /**
  4. * 包名风格域名 = 原生 APP 客户端(与注册域名 / 登录域名共用口径)
  5. *
  6. * 例:com.example.app
  7. */
  8. final class AppDomain
  9. {
  10. /** MySQL REGEXP 主体(不含定界符),用于 raw SQL */
  11. public const SQL_REGEXP = "^(com|org|net|io|cn)\\\\.[a-zA-Z0-9_]+(\\\\.[a-zA-Z0-9_]+)+$";
  12. /** PHP preg 模式 */
  13. public const PHP_PATTERN = '/^(com|org|net|io|cn)\.[a-z0-9_]+(\.[a-z0-9_]+)+$/i';
  14. public static function matches(string $host): bool
  15. {
  16. $host = strtolower(trim($host));
  17. if ($host === '') {
  18. return false;
  19. }
  20. return (bool)preg_match(self::PHP_PATTERN, $host);
  21. }
  22. /**
  23. * SQL 布尔表达式:列是否为包名域名
  24. *
  25. * @param string $column 已带表别名的列,如 u.register_domain
  26. */
  27. public static function sqlIsApp(string $column): string
  28. {
  29. return "({$column} REGEXP '" . self::SQL_REGEXP . "')";
  30. }
  31. }