| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- <?php
- /*
- * This file is part of the php-phantomjs.
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace JonnyW\PhantomJs\Http;
- use JonnyW\PhantomJs\Exception\InvalidUrlException;
- use JonnyW\PhantomJs\Exception\InvalidMethodException;
- use JonnyW\PhantomJs\Procedure\InputInterface;
- /**
- * PHP PhantomJs
- *
- * @author Jon Wenmoth <contact@jonnyw.me>
- */
- abstract class AbstractRequest
- implements RequestInterface, InputInterface
- {
- /**
- * Headers
- *
- * @var array
- * @access protected
- */
- protected $headers;
- /**
- * Request data
- *
- * @var array
- * @access protected
- */
- protected $data;
- /**
- * Request URL
- *
- * @var string
- * @access protected
- */
- protected $url;
- /**
- * Request method
- *
- * @var string
- * @access protected
- */
- protected $method;
- /**
- * Timeout period
- *
- * @var int
- * @access protected
- */
- protected $timeout;
- /**
- * Page load delay time.
- *
- * @var int
- * @access protected
- */
- protected $delay;
- /**
- * Viewport width.
- *
- * @var int
- * @access protected
- */
- protected $viewportWidth;
- /**
- * Viewport height.
- *
- * @var int
- * @access protected
- */
- protected $viewportHeight;
- /**
- * Internal constructor
- *
- * @access public
- * @param string $url (default: null)
- * @param string $method (default: RequestInterface::METHOD_GET)
- * @param int $timeout (default: 5000)
- */
- public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
- {
- $this->headers = array();
- $this->data = array();
- $this->delay = 0;
- $this->viewportWidth = 0;
- $this->viewportHeight = 0;
- $this->setMethod($method);
- $this->setTimeout($timeout);
- if ($url) {
- $this->setUrl($url);
- }
- }
- /**
- * Set request method
- *
- * @access public
- * @param string $method
- * @return \JonnyW\PhantomJs\Http\AbstractRequest
- * @throws \JonnyW\PhantomJs\Exception\InvalidMethodException
- */
- public function setMethod($method)
- {
- $method = strtoupper($method);
- $reflection = new \ReflectionClass('\JonnyW\PhantomJs\Http\RequestInterface');
- if (!$reflection->hasConstant('METHOD_' . $method)) {
- throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method));
- }
- $this->method = $method;
- return $this;
- }
- /**
- * Get request method
- *
- * @access public
- * @return string
- */
- public function getMethod()
- {
- return $this->method;
- }
- /**
- * Set timeout period
- *
- * @access public
- * @param int $timeout
- * @return \JonnyW\PhantomJs\Http\AbstractRequest
- */
- public function setTimeout($timeout)
- {
- $this->timeout = $timeout;
- return $this;
- }
- /**
- * Get timeout period
- *
- * @access public
- * @return int
- */
- public function getTimeout()
- {
- return $this->timeout;
- }
- /**
- * Set page load delay time (seconds).
- *
- * @access public
- * @param int $delay
- * @return \JonnyW\PhantomJs\Http\AbstractRequest
- */
- public function setDelay($delay)
- {
- $this->delay = (int) $delay;
- return $this;
- }
- /**
- * Get page load delay time (seconds).
- *
- * @access public
- * @return int
- */
- public function getDelay()
- {
- return (int) $this->delay;
- }
- /**
- * Set viewport size.
- *
- * @access public
- * @param int $width
- * @param int $height
- * @return void
- */
- public function setViewportSize($width, $height)
- {
- $this->viewportWidth = (int) $width;
- $this->viewportHeight = (int) $height;
- }
- /**
- * Get viewport width.
- *
- * @access public
- * @return int
- */
- public function getViewportWidth()
- {
- return (int) $this->viewportWidth;
- }
- /**
- * Get viewport height.
- *
- * @access public
- * @return int
- */
- public function getViewportHeight()
- {
- return (int) $this->viewportHeight;
- }
- /**
- * Set request URL
- *
- * @access public
- * @param string $url
- * @return \JonnyW\PhantomJs\Http\AbstractRequest
- * @throws \JonnyW\PhantomJs\Exception\InvalidUrlException
- */
- public function setUrl($url)
- {
- if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
- throw new InvalidUrlException(sprintf('Invalid URL provided: %s', $url));
- }
- $this->url = $url;
- return $this;
- }
- /**
- * Get request URL
- * - Assembles query string for GET
- * and HEAD requests
- *
- * @access public
- * @return string
- */
- public function getUrl()
- {
- if (!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
- return $this->url;
- }
- $url = $this->url;
- if (count($this->data)) {
- $url .= false === strpos($url, '?') ? '?' : '&';
- $url .= http_build_query($this->data);
- }
- return $url;
- }
- /**
- * Get content body
- * - Returns query string if not GET or HEAD
- *
- * @access public
- * @return string
- */
- public function getBody()
- {
- if (in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
- return '';
- }
- return http_build_query($this->getRequestData());
- }
- /**
- * Set request data
- *
- * @access public
- * @param array $data
- * @return \JonnyW\PhantomJs\Http\AbstractRequest
- */
- public function setRequestData(array $data)
- {
- $this->data = $data;
- return $this;
- }
- /**
- * Get request data
- *
- * @access public
- * @param boolean $flat
- * @return array
- */
- public function getRequestData($flat = true)
- {
- if ($flat) {
- return $this->flattenData($this->data);
- }
- return $this->data;
- }
- /**
- * Set headers
- *
- * @access public
- * @param array $headers
- * @return \JonnyW\PhantomJs\Http\AbstractRequest
- */
- public function setHeaders(array $headers)
- {
- $this->headers = $headers;
- }
- /**
- * Add single header
- *
- * @access public
- * @param string $header
- * @param string $value
- * @return \JonnyW\PhantomJs\Http\AbstractRequest
- */
- public function addHeader($header, $value)
- {
- $this->headers[$header] = $value;
- return $this;
- }
- /**
- * Merge headers with existing
- *
- * @access public
- * @param array $headers
- * @return \JonnyW\PhantomJs\Http\AbstractRequest
- */
- public function addHeaders(array $headers)
- {
- $this->headers = array_merge($this->headers, $headers);
- return $this;
- }
- /**
- * Get request headers
- *
- * @access public
- * @param string $format
- * @return array
- */
- public function getHeaders($format = 'default')
- {
- if ($format === 'json') {
- return json_encode($this->headers);
- }
- return $this->headers;
- }
- /**
- * Flatten data into single
- * dimensional array
- *
- * @access protected
- * @param array $data
- * @param string $prefix
- * @param string $format
- * @return array
- */
- protected function flattenData(array $data, $prefix = '', $format = '%s')
- {
- $flat = array();
- foreach ($data as $name => $value) {
- $ref = $prefix . sprintf($format, $name);
- if (is_array($value)) {
- $flat += $this->flattenData($value, $ref, '[%s]');
- continue;
- }
- $flat[$ref] = $value;
- }
- return $flat;
- }
- }
|