| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * dom-factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @category Jkphl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @package Jkphl\Domfactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @subpackage Jkphl\Domfactory\Infrastructure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author Joschi Kuphal <[email protected]> / @jkphl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @license http://opensource.org/licenses/MIT The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /*********************************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  *  The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *  Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  *  Permission is hereby granted, free of charge, to any person obtaining a copy of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *  this software and associated documentation files (the "Software"), to deal in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *  the Software without restriction, including without limitation the rights to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  *  the Software, and to permit persons to whom the Software is furnished to do so, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  *  subject to the following conditions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  *  The above copyright notice and this permission notice shall be included in all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  *  copies or substantial portions of the Software. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  ***********************************************************************************/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | namespace Jkphl\Domfactory\Infrastructure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | use Guzzle\Common\Exception\RuntimeException as GuzzleRuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | use Jkphl\Domfactory\Domain\Dom as DomainDom; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | use Jkphl\Domfactory\Ports\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  * DOM factory (internal) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  * @package Jkphl\Domfactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  * @subpackage Jkphl\Domfactory\Infrastructure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | class Dom | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Create a DOM document using a HTTP client implementation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param string $url HTTP / HTTPS URL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param array $options Connection options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return \DOMDocument DOM document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @throws RuntimeException If the request wasn't successful | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @throws RuntimeException If a runtime exception occurred | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 2 |  |     protected static function createViaHttpClient($url, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |             $clientOptions = isset($options['client']) ? (array)$options['client'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |             $client = new Client(array_merge(['timeout' => 10.0], $clientOptions)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |             $requestOptions = isset($options['request']) ? (array)$options['request'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  |             $response = $client->get($url, $requestOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |             return self::createFromString(strval($response->getBody())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             // If a runtime exception occurred | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |         } catch (GuzzleRuntimeException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             throw new RuntimeException($e->getMessage(), $e->getCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * Create a DOM document from a string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param string $str String | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return \DOMDocument DOM document | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 8 |  |     public static function createFromString($str) | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 8 |  |         $dom = new DomainDom(new HtmlParser()); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 8 |  |         return $dom->load($str); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Create a DOM document via the PHP stream wrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param string $url URL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param array $options Connection options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return \DOMDocument DOM document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |     protected static function createViaStreamWrapper($url, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |         $clientOptions = isset($options['client']) ? (array)$options['client'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |         $opts = array_merge_recursive([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |             'http' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 'method' => 'GET', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 'protocol_version' => 1.1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 'user_agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.466.4 Safari/534.3', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 'max_redirects' => 10, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 'timeout' => 10.0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 'header' => "Accept-language: en\r\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |         ], $clientOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |         $context = stream_context_create($opts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |         $requestOptions = isset($options['request']) ? (array)$options['request'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |         stream_context_set_params($context, $requestOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |         $response = @file_get_contents($url, false, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |         return self::createFromString($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  |