Parser.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace WhichBrowser;
  3. use WhichBrowser\Model\Main;
  4. class Parser extends Main
  5. {
  6. use Cache;
  7. /**
  8. * Create a new object that contains all the detected information
  9. *
  10. * @param array|string $headers Optional, an array with all of the headers or a string with just the User-Agent header
  11. * @param array $options Optional, an array with configuration options
  12. */
  13. public function __construct($headers = null, $options = [])
  14. {
  15. parent::__construct();
  16. if (!is_null($headers)) {
  17. $this->analyse($headers, $options);
  18. }
  19. }
  20. /**
  21. * Analyse the provided headers or User-Agent string
  22. *
  23. * @param array|string $headers An array with all of the headers or a string with just the User-Agent header
  24. */
  25. public function analyse($headers, $options = [])
  26. {
  27. $o = $options;
  28. if (is_string($headers)) {
  29. $h = [ 'User-Agent' => $headers ];
  30. } else {
  31. if (isset($headers['headers'])) {
  32. $h = $headers['headers'];
  33. unset($headers['headers']);
  34. $o = array_merge($headers, $options);
  35. } else {
  36. $h = $headers;
  37. }
  38. }
  39. if ($this->analyseWithCache($h, $o)) {
  40. return;
  41. }
  42. $analyser = new Analyser($h, $o);
  43. $analyser->setdata($this);
  44. $analyser->analyse();
  45. }
  46. }