AgentTrait.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Http\Traits;
  3. use Jenssegers\Agent\Agent;
  4. trait AgentTrait
  5. {
  6. // 단말기 종류 확인
  7. public function device(?string $useragent = null): string
  8. {
  9. return (new Agent)->device($useragent ?? $_SERVER["HTTP_USER_AGENT"] ?? null);
  10. }
  11. // 운영체제 정보
  12. public function platform(?string $useragent = null): string
  13. {
  14. return (new Agent)->platform($useragent ?? $_SERVER["HTTP_USER_AGENT"] ?? null);
  15. }
  16. // 브라우저 종류 확인
  17. public function browser(?string $useragent = null): string
  18. {
  19. return (new Agent)->browser($useragent ?? $_SERVER["HTTP_USER_AGENT"] ?? null);
  20. }
  21. // 수집 로봇 이름
  22. public function robot(?string $useragent = null): string
  23. {
  24. return (new Agent)->robot($useragent ?? $_SERVER["HTTP_USER_AGENT"] ?? null);
  25. }
  26. // 브라우저 버전 정보
  27. public function version(?string $useragent = null): string
  28. {
  29. return (new Agent)->version($useragent ?? $_SERVER["HTTP_USER_AGENT"] ?? null);
  30. }
  31. // 브라우저 언어 정보
  32. public function languages(): string
  33. {
  34. return json_encode((new Agent)->languages());
  35. }
  36. // 단말기 정보
  37. public function deviceType(): int
  38. {
  39. if ($this->isDesktop()) {
  40. return DEVICE_TYPE_1; // desktop
  41. } else if ($this->isPhone()) {
  42. return DEVICE_TYPE_2; // phone
  43. } else if ($this->isMobile()) {
  44. return DEVICE_TYPE_3; // mobile
  45. } else if ($this->isTablet()) {
  46. return DEVICE_TYPE_4; // tablet
  47. } else if ($this->isRobot()) {
  48. return DEVICE_TYPE_5; // robot
  49. }
  50. return DEVICE_TYPE_0; // ALL
  51. }
  52. public function isDesktop()
  53. {
  54. return (new Agent)->isDesktop();
  55. }
  56. public function isMobile()
  57. {
  58. return (new Agent)->isMobile();
  59. }
  60. public function isPhone()
  61. {
  62. return (new Agent)->isPhone();
  63. }
  64. public function isTablet()
  65. {
  66. return (new Agent)->isTablet();
  67. }
  68. public function isRobot()
  69. {
  70. return (new Agent)->isRobot();
  71. }
  72. }