| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |   namespace Xparse\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |   use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |   use GuzzleHttp\ClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |   use GuzzleHttp\Psr7\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |   use GuzzleHttp\RequestOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |   use GuzzleHttp\TransferStats; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |   use Psr\Http\Message\RequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |   use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |   use Xparse\ElementFinder\ElementFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |   use Xparse\ElementFinder\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |   use Xparse\ParserInterface\ParserInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |    * @package Xparse\Parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |   class Parser implements ParserInterface { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var null|\Xparse\ElementFinder\ElementFinder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $lastPage = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var ClientInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $client = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var ElementFinderFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected $elementFinderFactory = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var null|ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     protected $lastResponse = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param ClientInterface|null $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param ElementFinderFactoryInterface|null $elementFinderFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 9 |  |     public function __construct(ClientInterface $client = null, ElementFinderFactoryInterface $elementFinderFactory = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 9 |  |       if ($client === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 3 |  |         $client = new Client([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |           RequestOptions::ALLOW_REDIRECTS => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |           RequestOptions::COOKIES => new \GuzzleHttp\Cookie\CookieJar(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 9 |  |       if ($elementFinderFactory === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 9 |  |         $elementFinderFactory = new ElementFinderFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 9 |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 9 |  |       $this->elementFinderFactory = $elementFinderFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 9 |  |       $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return ElementFinder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 4 |  |     public function get($url, array $options = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 4 |  |       if (empty($url) or !is_string($url)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         throw new \InvalidArgumentException('Url must be not empty and string.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |       $request = new Request('GET', $url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 3 |  |       return $this->send($request, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return ElementFinder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  |     public function post($url, $data, array $options = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  |       if (empty($url) or !is_string($url)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         throw new \InvalidArgumentException('Url must be not empty and string.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |       $request = new Request('POST', $url, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |         'body' => $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |       ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |       return $this->send($request, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return \Xparse\ElementFinder\ElementFinder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |     public function getLastPage() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |       return $this->lastPage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param \Xparse\ElementFinder\ElementFinder $lastPage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 4 |  |     public function setLastPage($lastPage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 4 |  |       $this->lastPage = $lastPage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 4 |  |       return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @return null|ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |     public function getLastResponse() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |       return $this->lastResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @return ClientInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 4 |  |     public function getClient() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 4 |  |       return $this->client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @param $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @return \Xparse\ElementFinder\ElementFinder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 4 |  |     public function send(RequestInterface $request, array $options = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 4 |  |       $prevCallback = !empty($options['on_stats']) ? $options['on_stats'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 4 |  |       $effectiveUri = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 4 |  |       $options['on_stats'] = function (TransferStats $stats) use (&$effectiveUri, $prevCallback) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 4 |  |         $effectiveUri = $stats->getEffectiveUri(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 4 |  |         if ($prevCallback !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |           /** @var callable $prevCallback */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |           $prevCallback($stats); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 4 |  |       }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 4 |  |       $response = $this->client->send($request, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 4 |  |       $page = $this->elementFinderFactory->create($response, $effectiveUri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 4 |  |       $this->setLastPage($page); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 4 |  |       $this->lastResponse = $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 4 |  |       return $page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @return ElementFinderFactoryInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 170 | 1 |  |     public function getElementFinderFactory() { | 
            
                                                                        
                            
            
                                    
            
            
                | 171 | 1 |  |       return $this->elementFinderFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 174 |  |  |   } | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  |