/** * Handle incoming messages. * * Must be implemented by all extending classes * * @param $url * @param \React\EventLoop\LoopInterface $loop * @param \Zend\Log\LoggerInterface $logger * @throws \InvalidArgumentException */ public function __construct($url, LoopInterface $loop, LoggerInterface $logger) { $uri = new Uri($url); if ($uri->getScheme() == 'ws') { $uri->setScheme('tcp'); } elseif ($uri->getScheme() == 'wss') { $uri->setScheme('ssl'); } if ($uri->getScheme() != 'tcp' && $uri->getScheme() != 'ssl') { throw new \InvalidArgumentException("Uri scheme must be one of: tcp, ssl, ws, wss"); } $this->uri = $uri; $this->loop = $loop; $this->_streams = new SplObjectStorage(); $this->_connections = new SplObjectStorage(); $this->_context = stream_context_create(); $this->_logger = $logger; }
/** * Execute geocoding * * @param Request $request * @return Response */ public function geocode(Request $request) { if (null === $request) { throw new Exception\InvalidArgumentException('request'); } $uri = new Uri(); $uri->setScheme('https'); $uri->setHost(self::GOOGLE_MAPS_APIS_URL); $uri->setPath(self::GOOGLE_GEOCODING_API_PATH); $urlParameters = $request->getUrlParameters(); if (null === $urlParameters) { throw new Exception\RuntimeException('Invalid URL parameters'); } $uri->setQuery($urlParameters); $client = $this->getHttpClient(); $client->resetParameters(); $client->setUri($uri->toString()); $stream = $client->send(); $body = Json::decode($stream->getBody(), Json::TYPE_ARRAY); $hydrator = new ArraySerializable(); $response = new Response(); $response->setRawBody($body); if (!isset($body['status'])) { throw new Exception\RuntimeException('Invalid status'); } $response->setStatus($body['status']); if (!isset($body['results'])) { throw new Exception\RuntimeException('Invalid results'); } $resultSet = new ResultSet(); foreach ($body['results'] as $data) { $result = new Result(); $hydrator->hydrate($data, $result); $resultSet->addElement($result); } $response->setResults($resultSet); return $response; }
/** * Data provider for invalid relative URI objects * * @return array */ public function invalidRelativeUriObjectProvider() { // Empty URI is not valid $obj1 = new Uri(); // Path cannot begin with '//' $obj2 = new Uri(); $obj2->setPath('//path'); // A object with port $obj3 = new Uri(); $obj3->setPort(123); // A object with userInfo $obj4 = new Uri(); $obj4->setUserInfo('shahar:password'); // A object with scheme $obj5 = new Uri(); $obj5->setScheme('https'); // A object with host $obj6 = new Uri(); $obj6->setHost('example.com'); return array(array($obj1), array($obj2), array($obj3), array($obj4), array($obj5), array($obj6)); }
/** * Enforce the defined scheme on the URI * * This will also adjust the host and path parts of the URI as expected in * the case of scheme-less network URIs * * @param Uri $uri */ protected function enforceScheme(Uri $uri) { $path = $uri->getPath(); if (strpos($path, '/') !== false) { list($host, $path) = explode('/', $path, 2); $path = '/' . $path; } else { $host = $path; $path = ''; } // We have nothing to do if we have no host if (!$host) { return; } $uri->setScheme($this->enforcedScheme)->setHost($host)->setPath($path); }
/** * Test that setting an invalid scheme causes an exception * * @param string $scheme * @dataProvider invalidSchemeProvider */ public function testSetInvalidScheme($scheme) { $uri = new Uri; $this->setExpectedException('Zend\Uri\Exception\InvalidUriPartException'); $uri->setScheme($scheme); }
/** * Build cURL for FTP * * @param Uri $uri * @param array $options * * @return resource * @throws \InitializationException */ protected function ftpCurl(Uri $uri, array $options) { if (!extension_loaded('curl')) { throw new \InitializationException('cURL extension is not installed.'); } $uri->setScheme('ftp'); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $uri->__toString()); if (!empty($options['username']) && !empty($options['password'])) { $userpwd = $options['username'] . ':' . $options['password']; curl_setopt($curl, CURLOPT_USERPWD, $userpwd); } if (isset($options['timeout'])) { curl_setopt($curl, CURLOPT_TIMEOUT, (int) $options['timeout']); } return $curl; }
/** * Set the URI scheme * * @param string $scheme * @throws Exception\InvalidUriPartException * @return \Zend\Uri\Uri */ public function setScheme($scheme) { $this->uri->setScheme($scheme); return $this; }