Advertisement
Ostap34PHP

Untitled

May 1st, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.36 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Services;
  4.  
  5. use ErrorException;
  6. use GuzzleHttp\Client;
  7. use GuzzleHttp\Exception\RequestException;
  8.  
  9. class Translate
  10. {
  11.     /**
  12.      * @var Client HTTP Client
  13.      */
  14.     protected $client;
  15.  
  16.     /**
  17.      * @var string|null Source language - from where the page should be translated
  18.      */
  19.     protected $source;
  20.  
  21.     /**
  22.      * @var string Target language - to which language page should be translated
  23.      */
  24.     protected $target;
  25.  
  26.     /**
  27.      * @var string Url what need to be translated
  28.      */
  29.     protected $url;
  30.  
  31.     const IFRAME_REGEX = '**some regex**';
  32.  
  33.     const URL_REGEX = '**some regex**';
  34.  
  35.     public function __construct(string $target = 'en', string $source = null)
  36.     {
  37.         $this->client = new Client();
  38.         $this->setSource($source)
  39.             ->setTarget($target);
  40.     }
  41.  
  42.     public function setTarget(string $target) : self
  43.     {
  44.         $this->target = $target;
  45.         return $this;
  46.     }
  47.  
  48.     public function setSource(string $source = null) : self
  49.     {
  50.         $this->source = $source ?? 'auto';
  51.         return $this;
  52.     }
  53.  
  54.     public function setUrl(string $url) : self
  55.     {
  56.         $this->url = $url;
  57.         return $this;
  58.     }
  59.  
  60.     public function translate()
  61.     {
  62.         try {
  63.             $response = $this->client->get(
  64.                 sprintf("example.com?hl=%s&sl=%s&u=%s",
  65.                     $this->target,
  66.                     $this->source,
  67.                     $this->url
  68.                 )
  69.             );
  70.             $body = $response->getBody();
  71.  
  72.             //Find iframe url
  73.             preg_match(self::IFRAME_REGEX, $body, $matches);
  74.             $iframeUrl = $matches[1];
  75.  
  76.             //Send request to iframe url
  77.             $response = $this->client->get($iframeUrl);
  78.             $body = $response->getBody();
  79.  
  80.             //Find translated page url
  81.             preg_match(self::URL_REGEX, $body, $matches);
  82.             $pageUrl = preg_replace('|&amp;|', '&', $matches[1]);
  83.  
  84.             $response = $this->client->get($pageUrl);
  85.             $body = $response->getBody();
  86.  
  87.             return $this->formatBody($body);
  88.         } catch (RequestException $e) {
  89.             throw new ErrorException($e->getMessage());
  90.         }
  91.     }
  92.  
  93.     private function formatBody(string $body)
  94.     {
  95.         return iconv("Windows-1251", "UTF-8", $body);
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement