You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
969 B

  1. <?php
  2. namespace p3k\XRay;
  3. use p3k\XRay\Formats;
  4. class Parser {
  5. private $http;
  6. public function __construct($http) {
  7. $this->http = $http;
  8. }
  9. public function parse($body, $url, $opts=[]) {
  10. if(isset($opts['timeout']))
  11. $this->http->set_timeout($opts['timeout']);
  12. if(isset($opts['max_redirects']))
  13. $this->http->set_max_redirects($opts['max_redirects']);
  14. // Check if the URL matches a special parser
  15. if(Formats\Instagram::matches($url)) {
  16. return Formats\Instagram::parse($this->http, $body, $url);
  17. }
  18. if(Formats\GitHub::matches($url)) {
  19. return Formats\GitHub::parse($body, $url);
  20. }
  21. if(Formats\Twitter::matches($url)) {
  22. return Formats\Twitter::parse($body, $url);
  23. }
  24. if(Formats\XKCD::matches($url)) {
  25. return Formats\XKCD::parse($body, $url);
  26. }
  27. // No special parsers matched, parse for Microformats now
  28. return Formats\HTML::parse($this->http, $body, $url, $opts);
  29. }
  30. }