Просмотр исходного кода

Merge pull request #6 from jonnnnyw/scrutinizer-patch-1

Scrutinizer Auto-Fixes
Jonny Wenmoth 12 лет назад
Родитель
Сommit
40009bdca4

+ 318 - 320
src/JonnyW/PhantomJs/Client.php

@@ -24,330 +24,328 @@ use JonnyW\PhantomJs\Message\ResponseInterface;
  */
 class Client implements ClientInterface
 {
-	/**
-	 * Client instance
-	 *
-	 * @var JonnyW\PhantomJs\ClientInterface
-	 */
-	private static $instance;
-
-	/**
-	 * Message factory instance
-	 *
-	 * @var JonnyW\PhantomJs\Message\FactoryInterface
-	 */
-	protected $factory;
-
-	/**
-	 * Path to phantomJS executable
-	 *
-	 * @var string
-	 */
-	protected $phantomJS;
-	
-	/**
-	 * Request timeout period
-	 *
-	 * @var int
-	 */
-	protected $timeout;
-
-	/**
-	 * Internal constructor
-	 *
-	 * @param FactoryInterface $factory
-	 * @return void
-	 */
-	public function __construct(FactoryInterface $factory = null)
-	{
-		if(!$factory instanceof FactoryInterface) {
-			$factory = Factory::getInstance();
-		}
-
-		$this->factory  = $factory;
-		$this->phantomJS  = 'bin/phantomjs';
-		$this->timeout   = 5000;
-	}
-
-	/**
-	 * Get singleton instance
-	 *
-	 * @param FactoryInterface $factory
-	 * @return Client
-	 */
-	public static function getInstance(FactoryInterface $factory = null)
-	{
-		if(!self::$instance instanceof ClientInterface) {
-			self::$instance = new Client($factory);
-		}
-
-		return self::$instance;
-	}
-
-	/**
-	 * Get message factory instance
-	 *
-	 * @return JonnyW\PhantomJs\Message\FactoryInterface
-	 */
-	public function getMessageFactory()
-	{
-		return $this->factory;
-	}
-
-	/**
-	 * Send request
-	 *
-	 * @param RequestInterface $request
-	 * @param ResponseInterface $response
-	 * @param string $file
-	 * @return ResponseInterface
-	 */
-	public function send(RequestInterface $request, ResponseInterface $response, $file = null)
-	{
-		if(!is_null($file)) {
-			return $this->capture($request, $response, $file);
-		}
-
-		return $this->open($request, $response);
-	}
-
-	/**
-	 * Open page
-	 *
-	 * @param RequestInterface $request
-	 * @param ResponseInterface $response
-	 * @return ResponseInterface
-	 */
-	public function open(RequestInterface $request, ResponseInterface $response)
-	{
-		return $this->request($request, $response, $this->openCmd);
-	}
-
-	/**
-	 * Screen capture
-	 *
-	 * @param RequestInterface $request
-	 * @param ResponseInterface $response
-	 * @param string $file
-	 * @return ResponseInterface
-	 */
-	public function capture(RequestInterface $request, ResponseInterface $response, $file)
-	{
-		if(!is_writable(dirname($file))) {
-			throw new NotWriteableException(sprintf('Path is not writeable by PhantomJs: %s', $file));
-		}
-
-		$cmd = sprintf($this->captureCmd, $file);
-
-		return $this->request($request, $response, $cmd);
-	}
-
-	/**
-	 * Set new PhantomJs path
-	 *
-	 * @param string $path
-	 * @return Client
-	 */
-	public function setPhantomJs($path)
-	{
-		if(!file_exists($path) || !is_executable($path)) {
-			throw new NoPhantomJsException(sprintf('PhantomJs file does not exist or is not executable: %s', $path));
-		}
-
-		$this->phantomJS = $path;
-
-		return $this;
-	}
-
-	/**
-	 * Set timeout period (in milliseconds)
-	 *
-	 * @param int $period
-	 * @return Client
-	 */
-	public function setTimeout($period)
-	{
-		$this->timeout = $period;
-
-		return $this;
-	}
-
-	/**
-	 * Make PhantomJS request
-	 *
-	 * @param RequestInterface $request
-	 * @param ResponseInterface $response
-	 * @param string $cmd
-	 * @return ResponseInterface
-	 */
-	protected function request(RequestInterface $request, ResponseInterface $response, $cmd)
-	{
-		// Validate PhantomJS executable
-		if(!file_exists($this->phantomJS) || !is_executable($this->phantomJS)) {
-			throw new NoPhantomJsException(sprintf('PhantomJs file does not exist or is not executable: %s', $this->phantomJS));
-		}
-
-		try {
-
-			$script = false;
-
-			$data = sprintf(
-				$this->wrapper,
-				$request->getHeaders('json'),
-				$this->timeout,
-				$request->getUrl(),
-				$request->getMethod(),
-				$request->getBody(),
-				$cmd
-			);
-
-			$script = $this->writeScript($data);
-			$cmd  = escapeshellcmd(sprintf("%s %s", $this->phantomJS, $script));
-
-			$result = shell_exec($cmd);
-			$result = $this->parse($result);
-
-			$this->removeScript($script);
-
-			$response->setData($result);
-		}
-		catch(NotWriteableException $e) {
-			throw $e;
-		}
-		catch(\Exception $e) {
-
-			$this->removeScript($script);
-
-			throw new CommandFailedException(sprintf('Error when executing PhantomJs command: %s - %s', $cmd, $e->getMessage()));
-		}
-
-		return $response;
-	}
-
-	/**
-	 * Write temporary script file and
-	 * return path to file
-	 *
-	 * @param string $data
-	 * @return string
-	 */
-	protected function writeScript($data)
-	{
-		$file = tempnam('/tmp', 'phantomjs');
-
-		// Could not create tmp file
-		if(!$file || !is_writable($file)) {
-			throw new NotWriteableException('Could not create tmp file on system. Please check your tmp directory and make sure it is writeable.');
-		}
-
-		// Could not write script data to tmp file
-		if(file_put_contents($file, $data) === false) {
-
-			$this->removeScript($file);
-
-			throw new NotWriteableException(sprintf('Could not write data to tmp file: %s. Please check your tmp directory and make sure it is writeable.', $file));
-		}
-
-		return $file;
-	}
-
-	/**
-	 * Remove temporary script file
-	 *
-	 * @param string|boolean $file
-	 * @return Client
-	 */
-	protected function removeScript($file)
-	{
-		if(is_string($file) && file_exists($file)) {
-			unlink($file);
-		}
-
-		return $this;
-	}
-
-	/**
-	 * If data from JSON string format
-	 * and return array
-	 *
-	 * @param string $data
-	 * @return array
-	 */
-	protected function parse($data)
-	{
-		// Data is invalid
-		if($data === null || !is_string($data)) {
-			return array();
-		}
-
-		// Not a JSON string
-		if(substr($data, 0, 1) !== '{') {
-			return array();
-		}
-
-		// Return decoded JSON string
-		return (array) json_decode($data, true);
-	}
-
-	/**
-	 * PhantomJs base wrapper
-	 *
-	 * @var string
-	 */
-	protected $wrapper = <<<EOF
-
-	var page = require('webpage').create(),
-		response = {},
-		headers = %1\$s;
-
-	page.settings.resourceTimeout = %2\$s;
-	page.onResourceTimeout = function(e) {
-		response 		= e;
-		response.status = e.errorCode;
-	};
-
-	page.onResourceReceived = function (r) {
-		if(!response.status) response = r;
-	};
-
-	page.customHeaders = headers ? headers : {};
-
-	page.open('%3\$s', '%4\$s', '%5\$s', function(status) {
-
-		if(status === 'success') {
-			%6\$s
-		}
-
-		console.log(JSON.stringify(response, undefined, 4));
-		phantom.exit();
-	});
+    /**
+     * Client instance
+     *
+     * @var JonnyW\PhantomJs\ClientInterface
+     */
+    private static $instance;
+
+    /**
+     * Message factory instance
+     *
+     * @var JonnyW\PhantomJs\Message\FactoryInterface
+     */
+    protected $factory;
+
+    /**
+     * Path to phantomJS executable
+     *
+     * @var string
+     */
+    protected $phantomJS;
+
+    /**
+     * Request timeout period
+     *
+     * @var int
+     */
+    protected $timeout;
+
+    /**
+     * Internal constructor
+     *
+     * @param  FactoryInterface $factory
+     * @return void
+     */
+    public function __construct(FactoryInterface $factory = null)
+    {
+        if (!$factory instanceof FactoryInterface) {
+            $factory = Factory::getInstance();
+        }
+
+        $this->factory  = $factory;
+        $this->phantomJS  = 'bin/phantomjs';
+        $this->timeout   = 5000;
+    }
+
+    /**
+     * Get singleton instance
+     *
+     * @param  FactoryInterface $factory
+     * @return Client
+     */
+    public static function getInstance(FactoryInterface $factory = null)
+    {
+        if (!self::$instance instanceof ClientInterface) {
+            self::$instance = new Client($factory);
+        }
+
+        return self::$instance;
+    }
+
+    /**
+     * Get message factory instance
+     *
+     * @return JonnyW\PhantomJs\Message\FactoryInterface
+     */
+    public function getMessageFactory()
+    {
+        return $this->factory;
+    }
+
+    /**
+     * Send request
+     *
+     * @param  RequestInterface  $request
+     * @param  ResponseInterface $response
+     * @param  string            $file
+     * @return ResponseInterface
+     */
+    public function send(RequestInterface $request, ResponseInterface $response, $file = null)
+    {
+        if (!is_null($file)) {
+            return $this->capture($request, $response, $file);
+        }
+
+        return $this->open($request, $response);
+    }
+
+    /**
+     * Open page
+     *
+     * @param  RequestInterface  $request
+     * @param  ResponseInterface $response
+     * @return ResponseInterface
+     */
+    public function open(RequestInterface $request, ResponseInterface $response)
+    {
+        return $this->request($request, $response, $this->openCmd);
+    }
+
+    /**
+     * Screen capture
+     *
+     * @param  RequestInterface  $request
+     * @param  ResponseInterface $response
+     * @param  string            $file
+     * @return ResponseInterface
+     */
+    public function capture(RequestInterface $request, ResponseInterface $response, $file)
+    {
+        if (!is_writable(dirname($file))) {
+            throw new NotWriteableException(sprintf('Path is not writeable by PhantomJs: %s', $file));
+        }
+
+        $cmd = sprintf($this->captureCmd, $file);
+
+        return $this->request($request, $response, $cmd);
+    }
+
+    /**
+     * Set new PhantomJs path
+     *
+     * @param  string $path
+     * @return Client
+     */
+    public function setPhantomJs($path)
+    {
+        if (!file_exists($path) || !is_executable($path)) {
+            throw new NoPhantomJsException(sprintf('PhantomJs file does not exist or is not executable: %s', $path));
+        }
+
+        $this->phantomJS = $path;
+
+        return $this;
+    }
+
+    /**
+     * Set timeout period (in milliseconds)
+     *
+     * @param  int    $period
+     * @return Client
+     */
+    public function setTimeout($period)
+    {
+        $this->timeout = $period;
+
+        return $this;
+    }
+
+    /**
+     * Make PhantomJS request
+     *
+     * @param  RequestInterface  $request
+     * @param  ResponseInterface $response
+     * @param  string            $cmd
+     * @return ResponseInterface
+     */
+    protected function request(RequestInterface $request, ResponseInterface $response, $cmd)
+    {
+        // Validate PhantomJS executable
+        if (!file_exists($this->phantomJS) || !is_executable($this->phantomJS)) {
+            throw new NoPhantomJsException(sprintf('PhantomJs file does not exist or is not executable: %s', $this->phantomJS));
+        }
+
+        try {
+
+            $script = false;
+
+            $data = sprintf(
+                $this->wrapper,
+                $request->getHeaders('json'),
+                $this->timeout,
+                $request->getUrl(),
+                $request->getMethod(),
+                $request->getBody(),
+                $cmd
+            );
+
+            $script = $this->writeScript($data);
+            $cmd  = escapeshellcmd(sprintf("%s %s", $this->phantomJS, $script));
+
+            $result = shell_exec($cmd);
+            $result = $this->parse($result);
+
+            $this->removeScript($script);
+
+            $response->setData($result);
+        } catch (NotWriteableException $e) {
+            throw $e;
+        } catch (\Exception $e) {
+
+            $this->removeScript($script);
+
+            throw new CommandFailedException(sprintf('Error when executing PhantomJs command: %s - %s', $cmd, $e->getMessage()));
+        }
+
+        return $response;
+    }
+
+    /**
+     * Write temporary script file and
+     * return path to file
+     *
+     * @param  string $data
+     * @return string
+     */
+    protected function writeScript($data)
+    {
+        $file = tempnam('/tmp', 'phantomjs');
+
+        // Could not create tmp file
+        if (!$file || !is_writable($file)) {
+            throw new NotWriteableException('Could not create tmp file on system. Please check your tmp directory and make sure it is writeable.');
+        }
+
+        // Could not write script data to tmp file
+        if (file_put_contents($file, $data) === false) {
+
+            $this->removeScript($file);
+
+            throw new NotWriteableException(sprintf('Could not write data to tmp file: %s. Please check your tmp directory and make sure it is writeable.', $file));
+        }
+
+        return $file;
+    }
+
+    /**
+     * Remove temporary script file
+     *
+     * @param  string|boolean $file
+     * @return Client
+     */
+    protected function removeScript($file)
+    {
+        if (is_string($file) && file_exists($file)) {
+            unlink($file);
+        }
+
+        return $this;
+    }
+
+    /**
+     * If data from JSON string format
+     * and return array
+     *
+     * @param  string $data
+     * @return array
+     */
+    protected function parse($data)
+    {
+        // Data is invalid
+        if ($data === null || !is_string($data)) {
+            return array();
+        }
+
+        // Not a JSON string
+        if (substr($data, 0, 1) !== '{') {
+            return array();
+        }
+
+        // Return decoded JSON string
+        return (array) json_decode($data, true);
+    }
+
+    /**
+     * PhantomJs base wrapper
+     *
+     * @var string
+     */
+    protected $wrapper = <<<EOF
+
+    var page = require('webpage').create(),
+        response = {},
+        headers = %1\$s;
+
+    page.settings.resourceTimeout = %2\$s;
+    page.onResourceTimeout = function (e) {
+        response 		= e;
+        response.status = e.errorCode;
+    };
+
+    page.onResourceReceived = function (r) {
+        if(!response.status) response = r;
+    };
+
+    page.customHeaders = headers ? headers : {};
+
+    page.open('%3\$s', '%4\$s', '%5\$s', function (status) {
+
+        if (status === 'success') {
+            %6\$s
+        }
+
+        console.log(JSON.stringify(response, undefined, 4));
+        phantom.exit();
+    });
 EOF;
 
-	/**
-	 * PhantomJs screen capture
-	 * command template
-	 *
-	 * @var string
-	 */
-	protected $captureCmd = <<<EOF
+    /**
+     * PhantomJs screen capture
+     * command template
+     *
+     * @var string
+     */
+    protected $captureCmd = <<<EOF
 
-			page.render('%1\$s');
+            page.render('%1\$s');
 
-			response.content = page.evaluate(function () {
-				return document.getElementsByTagName('html')[0].innerHTML
-			});
+            response.content = page.evaluate(function () {
+                return document.getElementsByTagName('html')[0].innerHTML
+            });
 EOF;
 
-	/**
-	 * PhantomJs page open
-	 * command template
-	 *
-	 * @var string
-	 */
-	protected $openCmd = <<<EOF
-
-			response.content = page.evaluate(function () {
-				return document.getElementsByTagName('html')[0].innerHTML
-			});
+    /**
+     * PhantomJs page open
+     * command template
+     *
+     * @var string
+     */
+    protected $openCmd = <<<EOF
+
+            response.content = page.evaluate(function () {
+                return document.getElementsByTagName('html')[0].innerHTML
+            });
 EOF;
-}
+}

+ 34 - 34
src/JonnyW/PhantomJs/ClientInterface.php

@@ -18,40 +18,40 @@ use JonnyW\PhantomJs\Message\ResponseInterface;
  */
 interface ClientInterface
 {
-	/**
-	 * Send request
-	 *
-	 * @param RequestInterface $request
-	 * @param ResponseInterface $response
-	 * @param string $file
-	 * @return ResponseInterface
-	 */
-	public function send(RequestInterface $request, ResponseInterface $response, $file = null);
+    /**
+     * Send request
+     *
+     * @param  RequestInterface  $request
+     * @param  ResponseInterface $response
+     * @param  string            $file
+     * @return ResponseInterface
+     */
+    public function send(RequestInterface $request, ResponseInterface $response, $file = null);
 
-	/**
-	 * Open page
-	 *
-	 * @param RequestInterface $request
-	 * @param ResponseInterface $response
-	 * @return ResponseInterface
-	 */
-	public function open(RequestInterface $request, ResponseInterface $response);
+    /**
+     * Open page
+     *
+     * @param  RequestInterface  $request
+     * @param  ResponseInterface $response
+     * @return ResponseInterface
+     */
+    public function open(RequestInterface $request, ResponseInterface $response);
 
-	/**
-	 * Screen capture
-	 *
-	 * @param RequestInterface $request
-	 * @param ResponseInterface $response
-	 * @param string $file
-	 * @return ResponseInterface
-	 */
-	public function capture(RequestInterface $request, ResponseInterface $response, $file);
+    /**
+     * Screen capture
+     *
+     * @param  RequestInterface  $request
+     * @param  ResponseInterface $response
+     * @param  string            $file
+     * @return ResponseInterface
+     */
+    public function capture(RequestInterface $request, ResponseInterface $response, $file);
 
-	/**
-	 * Set new PhantomJs path
-	 *
-	 * @param string $path
-	 * @return Client
-	 */
-	public function setPhantomJs($path);
-}
+    /**
+     * Set new PhantomJs path
+     *
+     * @param  string $path
+     * @return Client
+     */
+    public function setPhantomJs($path);
+}

+ 39 - 39
src/JonnyW/PhantomJs/Message/Factory.php

@@ -20,46 +20,46 @@ use JonnyW\PhantomJs\Message\Response;
  */
 class Factory implements FactoryInterface
 {
-	/**
-	 * Client instance
-	 *
-	 * @var JonnyW\PhantomJs\Message\FactoryInterface
-	 */
-	private static $instance;
+    /**
+     * Client instance
+     *
+     * @var JonnyW\PhantomJs\Message\FactoryInterface
+     */
+    private static $instance;
 
-	/**
-	 * Get singleton instance
-	 *
-	 * @return Factory
-	 */
-	public static function getInstance()
-	{
-		if(!self::$instance instanceof FactoryInterface) {
-			self::$instance = new Factory();
-		}
+    /**
+     * Get singleton instance
+     *
+     * @return Factory
+     */
+    public static function getInstance()
+    {
+        if (!self::$instance instanceof FactoryInterface) {
+            self::$instance = new Factory();
+        }
 
-		return self::$instance;
-	}
+        return self::$instance;
+    }
 
-	/**
-	 * Create request instance
-	 *
-	 * @param string $method
-	 * @param string $url
-	 * @return Request
-	 */
-	public function createRequest($method = RequestInterface::METHOD_GET, $url = null)
-	{
-		return new Request($method, $url);
-	}
+    /**
+     * Create request instance
+     *
+     * @param  string  $method
+     * @param  string  $url
+     * @return Request
+     */
+    public function createRequest($method = RequestInterface::METHOD_GET, $url = null)
+    {
+        return new Request($method, $url);
+    }
 
-	/**
-	 * Create response instance
-	 *
-	 * @return Response
-	 */
-	public function createResponse()
-	{
-		return new Response();
-	}
-}
+    /**
+     * Create response instance
+     *
+     * @return Response
+     */
+    public function createResponse()
+    {
+        return new Response();
+    }
+}

+ 21 - 21
src/JonnyW/PhantomJs/Message/FactoryInterface.php

@@ -17,26 +17,26 @@ use JonnyW\PhantomJs\Message\RequestInterface;
  */
 interface FactoryInterface
 {
-	/**
-	 * Get singleton instance
-	 *
-	 * @return Factory
-	 */
-	public static function getInstance();
+    /**
+     * Get singleton instance
+     *
+     * @return Factory
+     */
+    public static function getInstance();
 
-	/**
-	 * Create request instance
-	 *
-	 * @param string $url
-	 * @param string $method
-	 * @return Request
-	 */
-	public function createRequest($url, $method = RequestInterface::METHOD_GET);
+    /**
+     * Create request instance
+     *
+     * @param  string  $url
+     * @param  string  $method
+     * @return Request
+     */
+    public function createRequest($url, $method = RequestInterface::METHOD_GET);
 
-	/**
-	 * Create response instance
-	 *
-	 * @return Response
-	 */
-	public function createResponse();
-}
+    /**
+     * Create response instance
+     *
+     * @return Response
+     */
+    public function createResponse();
+}

+ 246 - 246
src/JonnyW/PhantomJs/Message/Request.php

@@ -20,249 +20,249 @@ use JonnyW\PhantomJs\Exception\InvalidMethodException;
  */
 class Request implements RequestInterface
 {
-	/**
-	 * Headers
-	 *
-	 * @var array
-	 */
-	protected $headers;
-
-	/**
-	 * Request data
-	 *
-	 * @var array
-	 */
-	protected $data;
-
-	/**
-	 * Request method
-	 *
-	 * @var string
-	 */
-	protected $method;
-
-	/**
-	 * Request URL
-	 *
-	 * @var string
-	 */
-	protected $url;
-
-	/**
-	 * Internal constructor
-	 *
-	 * @param string $method
-	 * @param string $url
-	 * @return void
-	 */
-	public function __construct($method = RequestInterface::METHOD_GET, $url = null)
-	{
-		$this->headers  = array();
-		$this->data  = array();
-
-		$this->setMethod($method);
-
-		if($url) {
-			$this->setUrl($url);
-		}
-	}
-
-	/**
-	 * Set request method
-	 *
-	 * @param string $method
-	 * @return Request
-	 */
-	public function setMethod($method)
-	{
-		$method   = strtoupper($method);
-		$reflection  = new \ReflectionClass('JonnyW\PhantomJs\Message\RequestInterface');
-
-		// Validate method
-		if(!$reflection->hasConstant('METHOD_' . $method)) {
-			throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method));
-		}
-
-		$this->method = $method;
-
-		return $this;
-	}
-
-	/**
-	 * Get request method
-	 *
-	 * @return string
-	 */
-	public function getMethod()
-	{
-		return $this->method;
-	}
-
-	/**
-	 * Set request URL
-	 *
-	 * @param string $url
-	 * @return Request
-	 */
-	public function setUrl($url)
-	{
-		// Validate 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
-	 *
-	 * @return string
-	 */
-	public function getUrl()
-	{
-		if(!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
-			return $this->url;
-		}
-
-		$url = $this->url;
-
-		// Add query string to URL
-		if(count($this->data)) {
-
-			$url  .= false === strpos($url, '?') ? '?' : '&';
-			$url  .= urldecode(http_build_query($this->data));
-		}
-
-		return $url;
-	}
-
-	/**
-	 * Get content body
-	 *  - Returns query string if not GET or HEAD
-	 *
-	 * @return string
-	 */
-	public function getBody()
-	{
-		if(in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
-			return '';
-		}
-
-		return urldecode(http_build_query($this->getRequestData()));
-	}
-
-	/**
-	 * Set request data
-	 *
-	 * @param array $data
-	 * @return Request
-	 */
-	public function setRequestData(array $data)
-	{
-		$this->data = $data;
-
-		return $this;
-	}
-
-	/**
-	 * Get request data
-	 *
-	 * @param boolean $flat
-	 * @return array
-	 */
-	public function getRequestData($flat = true)
-	{
-		if($flat) {
-			$this->flattenData($this->data);
-		}
-
-		return $this->data;
-	}
-
-	/**
-	 * Flatten data into single
-	 * dimensional array
-	 *
-	 * @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;
-	}
-
-	/**
-	 * Set headers
-	 *
-	 * @param array $headers
-	 * @return JonnyW\PhantomJs\Message\Request
-	 */
-	public function setHeaders(array $headers)
-	{
-		$this->headers = $headers;
-	}
-
-	/**
-	 * Add single header
-	 *
-	 * @param string $header
-	 * @param string $value
-	 * @return Request
-	 */
-	public function addHeader($header, $value)
-	{
-		$this->headers[$header] = $value;
-
-		return $this;
-	}
-
-	/**
-	 * Merge headers with existing
-	 *
-	 * @param array $headers
-	 * @return Request
-	 */
-	public function addHeaders(array $headers)
-	{
-		$this->headers = array_merge($this->headers, $headers);
-
-		return $this;
-	}
-
-	/**
-	 * Get request headers
-	 *
-	 * @param string $format
-	 * @return array
-	 */
-	public function getHeaders($format = 'default')
-	{
-		if($format == 'json') {
-			return json_encode($this->headers);
-		}
-
-		return $this->headers;
-	}
-}
+    /**
+     * Headers
+     *
+     * @var array
+     */
+    protected $headers;
+
+    /**
+     * Request data
+     *
+     * @var array
+     */
+    protected $data;
+
+    /**
+     * Request method
+     *
+     * @var string
+     */
+    protected $method;
+
+    /**
+     * Request URL
+     *
+     * @var string
+     */
+    protected $url;
+
+    /**
+     * Internal constructor
+     *
+     * @param  string $method
+     * @param  string $url
+     * @return void
+     */
+    public function __construct($method = RequestInterface::METHOD_GET, $url = null)
+    {
+        $this->headers  = array();
+        $this->data  = array();
+
+        $this->setMethod($method);
+
+        if ($url) {
+            $this->setUrl($url);
+        }
+    }
+
+    /**
+     * Set request method
+     *
+     * @param  string  $method
+     * @return Request
+     */
+    public function setMethod($method)
+    {
+        $method   = strtoupper($method);
+        $reflection  = new \ReflectionClass('JonnyW\PhantomJs\Message\RequestInterface');
+
+        // Validate method
+        if (!$reflection->hasConstant('METHOD_' . $method)) {
+            throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method));
+        }
+
+        $this->method = $method;
+
+        return $this;
+    }
+
+    /**
+     * Get request method
+     *
+     * @return string
+     */
+    public function getMethod()
+    {
+        return $this->method;
+    }
+
+    /**
+     * Set request URL
+     *
+     * @param  string  $url
+     * @return Request
+     */
+    public function setUrl($url)
+    {
+        // Validate 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
+     *
+     * @return string
+     */
+    public function getUrl()
+    {
+        if (!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
+            return $this->url;
+        }
+
+        $url = $this->url;
+
+        // Add query string to URL
+        if (count($this->data)) {
+
+            $url  .= false === strpos($url, '?') ? '?' : '&';
+            $url  .= urldecode(http_build_query($this->data));
+        }
+
+        return $url;
+    }
+
+    /**
+     * Get content body
+     *  - Returns query string if not GET or HEAD
+     *
+     * @return string
+     */
+    public function getBody()
+    {
+        if (in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
+            return '';
+        }
+
+        return urldecode(http_build_query($this->getRequestData()));
+    }
+
+    /**
+     * Set request data
+     *
+     * @param  array   $data
+     * @return Request
+     */
+    public function setRequestData(array $data)
+    {
+        $this->data = $data;
+
+        return $this;
+    }
+
+    /**
+     * Get request data
+     *
+     * @param  boolean $flat
+     * @return array
+     */
+    public function getRequestData($flat = true)
+    {
+        if ($flat) {
+            $this->flattenData($this->data);
+        }
+
+        return $this->data;
+    }
+
+    /**
+     * Flatten data into single
+     * dimensional array
+     *
+     * @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;
+    }
+
+    /**
+     * Set headers
+     *
+     * @param  array                            $headers
+     * @return JonnyW\PhantomJs\Message\Request
+     */
+    public function setHeaders(array $headers)
+    {
+        $this->headers = $headers;
+    }
+
+    /**
+     * Add single header
+     *
+     * @param  string  $header
+     * @param  string  $value
+     * @return Request
+     */
+    public function addHeader($header, $value)
+    {
+        $this->headers[$header] = $value;
+
+        return $this;
+    }
+
+    /**
+     * Merge headers with existing
+     *
+     * @param  array   $headers
+     * @return Request
+     */
+    public function addHeaders(array $headers)
+    {
+        $this->headers = array_merge($this->headers, $headers);
+
+        return $this;
+    }
+
+    /**
+     * Get request headers
+     *
+     * @param  string $format
+     * @return array
+     */
+    public function getHeaders($format = 'default')
+    {
+        if ($format == 'json') {
+            return json_encode($this->headers);
+        }
+
+        return $this->headers;
+    }
+}

+ 86 - 86
src/JonnyW/PhantomJs/Message/RequestInterface.php

@@ -16,100 +16,100 @@ namespace JonnyW\PhantomJs\Message;
  */
 interface RequestInterface
 {
-	const METHOD_OPTIONS = 'OPTIONS';
-	const METHOD_GET     = 'GET';
-	const METHOD_HEAD    = 'HEAD';
-	const METHOD_POST    = 'POST';
-	const METHOD_PUT     = 'PUT';
-	const METHOD_DELETE  = 'DELETE';
-	const METHOD_PATCH   = 'PATCH';
+    const METHOD_OPTIONS = 'OPTIONS';
+    const METHOD_GET     = 'GET';
+    const METHOD_HEAD    = 'HEAD';
+    const METHOD_POST    = 'POST';
+    const METHOD_PUT     = 'PUT';
+    const METHOD_DELETE  = 'DELETE';
+    const METHOD_PATCH   = 'PATCH';
 
-	/**
-	 * Set request method
-	 *
-	 * @param string $method
-	 * @return Request
-	 */
-	public function setMethod($method);
+    /**
+     * Set request method
+     *
+     * @param  string  $method
+     * @return Request
+     */
+    public function setMethod($method);
 
-	/**
-	 * Get request method
-	 *
-	 * @return string
-	 */
-	public function getMethod();
+    /**
+     * Get request method
+     *
+     * @return string
+     */
+    public function getMethod();
 
-	/**
-	 * Set request URL
-	 *
-	 * @param string $url
-	 * @return Request
-	 */
-	public function setUrl($url);
+    /**
+     * Set request URL
+     *
+     * @param  string  $url
+     * @return Request
+     */
+    public function setUrl($url);
 
-	/**
-	 * Get request URL
-	 *  - Assembles query string for GET
-	 *  and HEAD requests
-	 *
-	 * @return string
-	 */
-	public function getUrl();
+    /**
+     * Get request URL
+     *  - Assembles query string for GET
+     *  and HEAD requests
+     *
+     * @return string
+     */
+    public function getUrl();
 
-	/**
-	 * Get content body
-	 *  - Returns query string if not GET or HEAD
-	 *
-	 * @return string
-	 */
-	public function getBody();
+    /**
+     * Get content body
+     *  - Returns query string if not GET or HEAD
+     *
+     * @return string
+     */
+    public function getBody();
 
-	/**
-	 * Set request data
-	 *
-	 * @param array $data
-	 * @return Request
-	 */
-	public function setRequestData(array $data);
+    /**
+     * Set request data
+     *
+     * @param  array   $data
+     * @return Request
+     */
+    public function setRequestData(array $data);
 
-	/**
-	 * Get request data
-	 *
-	 * @param boolean $flat
-	 * @return array
-	 */
-	public function getRequestData($flat = true);
+    /**
+     * Get request data
+     *
+     * @param  boolean $flat
+     * @return array
+     */
+    public function getRequestData($flat = true);
 
-	/**
-	 * Set headers
-	 *
-	 * @param array $headers
-	 * @return JonnyW\PhantomJs\Message\Request|null
-	 */
-	public function setHeaders(array $headers);
+    /**
+     * Set headers
+     *
+     * @param  array                                 $headers
+     * @return JonnyW\PhantomJs\Message\Request|null
+     */
+    public function setHeaders(array $headers);
 
-	/**
-	 * Add single header
-	 *
-	 * @param string $header
-	 * @param string $value
-	 * @return Request
-	 */
-	public function addHeader($header, $value);
+    /**
+     * Add single header
+     *
+     * @param  string  $header
+     * @param  string  $value
+     * @return Request
+     */
+    public function addHeader($header, $value);
 
-	/**
-	 * Merge headers with existing
-	 *
-	 * @param array $headers
-	 * @return Request
-	 */
-	public function addHeaders(array $headers);
+    /**
+     * Merge headers with existing
+     *
+     * @param  array   $headers
+     * @return Request
+     */
+    public function addHeaders(array $headers);
 
-	/**
-	 * Get request headers
-	 *
-	 * @param string $format
-	 * @return array
-	 */
-	public function getHeaders($format = 'default');
-}
+    /**
+     * Get request headers
+     *
+     * @param  string $format
+     * @return array
+     */
+    public function getHeaders($format = 'default');
+}

+ 212 - 212
src/JonnyW/PhantomJs/Message/Response.php

@@ -17,215 +17,215 @@ use JonnyW\PhantomJs\Message\ResponseInterface;
  */
 class Response implements ResponseInterface
 {
-	/**
-	 * Http headers array
-	 *
-	 * @var array
-	 */
-	protected $headers;
-
-	/**
-	 * Response int
-	 *
-	 * @var string
-	 */
-	protected $status;
-
-	/**
-	 * Response body
-	 *
-	 * @var string
-	 */
-	protected $content;
-
-	/**
-	 * Response content type header
-	 *
-	 * @var string
-	 */
-	protected $contentType;
-
-	/**
-	 * Requested URL
-	 *
-	 * @var string
-	 */
-	protected $url;
-
-	/**
-	 * Redirected URL
-	 *
-	 * @var string
-	 */
-	protected $redirectUrl;
-
-	/**
-	 * Request time string
-	 *
-	 * @var string
-	 */
-	protected $time;
-
-	/**
-	 * Set response data
-	 *
-	 * @return Response
-	 */
-	public function setData(array $data)
-	{
-		$this->headers = array();
-
-		// Set headers array
-		if(isset($data['headers'])) {
-			$this->setHeaders((array) $data['headers']);
-		}
-
-		// Set status
-		if(isset($data['status'])) {
-			$this->status = $data['status'];
-		}
-
-		// Set content
-		if(isset($data['content'])) {
-			$this->content = $data['content'];
-		}
-
-		// Set content type string
-		if(isset($data['contentType'])) {
-			$this->contentType = $data['contentType'];
-		}
-
-		// Set request URL
-		if(isset($data['url'])) {
-			$this->url = $data['url'];
-		}
-
-		// Set redirect URL
-		if(isset($data['redirectURL'])) {
-			$this->redirectUrl = $data['redirectURL'];
-		}
-
-		// Set time string
-		if(isset($data['time'])) {
-			$this->time = $data['time'];
-		}
-
-		return $this;
-	}
-
-	/**
-	 * Set headers array
-	 *
-	 * @param array $headers
-	 * @return
-	 */
-	protected function setHeaders(array $headers)
-	{
-		foreach($headers as $header) {
-
-			if(isset($header['name']) && isset($header['value'])) {
-				$this->headers[$header['name']] = $header['value'];
-			}
-		}
-
-		return $this;
-	}
-
-	/**
-	 * Get HTTP headers array
-	 *
-	 * @return array
-	 */
-	public function getHeaders()
-	{
-		return (array) $this->headers;
-	}
-
-	/**
-	 * Get HTTP header value for code
-	 *
-	 * @praam string $$code
-	 * @return mixed
-	 */
-	public function getHeader($code)
-	{
-		if(isset($this->headers[$code])) {
-			return $this->headers[$code];
-		}
-
-		return null;
-	}
-
-	/**
-	 * Get response status code
-	 *
-	 * @return integer
-	 */
-	public function getStatus()
-	{
-		return (int) $this->status;
-	}
-
-	/**
-	 * Get page content from respone
-	 *
-	 * @return string
-	 */
-	public function getContent()
-	{
-		return $this->content;
-	}
-
-	/**
-	 * Get content type header
-	 *
-	 * @return string
-	 */
-	public function getContentType()
-	{
-		return $this->contentType;
-	}
-
-	/**
-	 * Get request URL
-	 *
-	 * @return string
-	 */
-	public function getUrl()
-	{
-		return $this->url;
-	}
-
-	/**
-	 * Get redirect URL (if redirected)
-	 *
-	 * @return string
-	 */
-	public function getRedirectUrl()
-	{
-		return $this->redirectUrl;
-	}
-
-	/**
-	 * Is response a redirect
-	 *  - Checks status codes
-	 *
-	 * @return boolean
-	 */
-	public function isRedirect()
-	{
-		$status = $this->getStatus();
-
-		return (bool) ($status >= 300 && $status < 307);
-	}
-
-	/**
-	 * Get time string
-	 *
-	 * @return string
-	 */
-	public function getTime()
-	{
-		return $this->time;
-	}
-}
+    /**
+     * Http headers array
+     *
+     * @var array
+     */
+    protected $headers;
+
+    /**
+     * Response int
+     *
+     * @var string
+     */
+    protected $status;
+
+    /**
+     * Response body
+     *
+     * @var string
+     */
+    protected $content;
+
+    /**
+     * Response content type header
+     *
+     * @var string
+     */
+    protected $contentType;
+
+    /**
+     * Requested URL
+     *
+     * @var string
+     */
+    protected $url;
+
+    /**
+     * Redirected URL
+     *
+     * @var string
+     */
+    protected $redirectUrl;
+
+    /**
+     * Request time string
+     *
+     * @var string
+     */
+    protected $time;
+
+    /**
+     * Set response data
+     *
+     * @return Response
+     */
+    public function setData(array $data)
+    {
+        $this->headers = array();
+
+        // Set headers array
+        if (isset($data['headers'])) {
+            $this->setHeaders((array) $data['headers']);
+        }
+
+        // Set status
+        if (isset($data['status'])) {
+            $this->status = $data['status'];
+        }
+
+        // Set content
+        if (isset($data['content'])) {
+            $this->content = $data['content'];
+        }
+
+        // Set content type string
+        if (isset($data['contentType'])) {
+            $this->contentType = $data['contentType'];
+        }
+
+        // Set request URL
+        if (isset($data['url'])) {
+            $this->url = $data['url'];
+        }
+
+        // Set redirect URL
+        if (isset($data['redirectURL'])) {
+            $this->redirectUrl = $data['redirectURL'];
+        }
+
+        // Set time string
+        if (isset($data['time'])) {
+            $this->time = $data['time'];
+        }
+
+        return $this;
+    }
+
+    /**
+     * Set headers array
+     *
+     * @param array $headers
+     *                       @return
+     */
+    protected function setHeaders(array $headers)
+    {
+        foreach ($headers as $header) {
+
+            if (isset($header['name']) && isset($header['value'])) {
+                $this->headers[$header['name']] = $header['value'];
+            }
+        }
+
+        return $this;
+    }
+
+    /**
+     * Get HTTP headers array
+     *
+     * @return array
+     */
+    public function getHeaders()
+    {
+        return (array) $this->headers;
+    }
+
+    /**
+     * Get HTTP header value for code
+     *
+     * @praam string $$code
+     * @return mixed
+     */
+    public function getHeader($code)
+    {
+        if (isset($this->headers[$code])) {
+            return $this->headers[$code];
+        }
+
+        return null;
+    }
+
+    /**
+     * Get response status code
+     *
+     * @return integer
+     */
+    public function getStatus()
+    {
+        return (int) $this->status;
+    }
+
+    /**
+     * Get page content from respone
+     *
+     * @return string
+     */
+    public function getContent()
+    {
+        return $this->content;
+    }
+
+    /**
+     * Get content type header
+     *
+     * @return string
+     */
+    public function getContentType()
+    {
+        return $this->contentType;
+    }
+
+    /**
+     * Get request URL
+     *
+     * @return string
+     */
+    public function getUrl()
+    {
+        return $this->url;
+    }
+
+    /**
+     * Get redirect URL (if redirected)
+     *
+     * @return string
+     */
+    public function getRedirectUrl()
+    {
+        return $this->redirectUrl;
+    }
+
+    /**
+     * Is response a redirect
+     *  - Checks status codes
+     *
+     * @return boolean
+     */
+    public function isRedirect()
+    {
+        $status = $this->getStatus();
+
+        return (bool) ($status >= 300 && $status < 307);
+    }
+
+    /**
+     * Get time string
+     *
+     * @return string
+     */
+    public function getTime()
+    {
+        return $this->time;
+    }
+}

+ 63 - 63
src/JonnyW/PhantomJs/Message/ResponseInterface.php

@@ -16,75 +16,75 @@ namespace JonnyW\PhantomJs\Message;
  */
 interface ResponseInterface
 {
-	/**
-	 * Set response data
-	 *
-	 * @return Response
-	 */
-	public function setData(array $data);
+    /**
+     * Set response data
+     *
+     * @return Response
+     */
+    public function setData(array $data);
 
-	/**
-	 * Get HTTP headers array
-	 *
-	 * @return array
-	 */
-	public function getHeaders();
+    /**
+     * Get HTTP headers array
+     *
+     * @return array
+     */
+    public function getHeaders();
 
-	/**
-	 * Get HTTP header value for code
-	 *
-	 * @praam string $$code
-	 * @return mixed
-	 */
-	public function getHeader($code);
+    /**
+     * Get HTTP header value for code
+     *
+     * @praam string $$code
+     * @return mixed
+     */
+    public function getHeader($code);
 
-	/**
-	 * Get response status code
-	 *
-	 * @return int|null
-	 */
-	public function getStatus();
+    /**
+     * Get response status code
+     *
+     * @return int|null
+     */
+    public function getStatus();
 
-	/**
-	 * Get page content from respone
-	 *
-	 * @return string
-	 */
-	public function getContent();
+    /**
+     * Get page content from respone
+     *
+     * @return string
+     */
+    public function getContent();
 
-	/**
-	 * Get content type header
-	 *
-	 * @return string
-	 */
-	public function getContentType();
+    /**
+     * Get content type header
+     *
+     * @return string
+     */
+    public function getContentType();
 
-	/**
-	 * Get request URL
-	 *
-	 * @return string
-	 */
-	public function getUrl();
+    /**
+     * Get request URL
+     *
+     * @return string
+     */
+    public function getUrl();
 
-	/**
-	 * Get redirect URL (if redirected)
-	 *
-	 * @return string
-	 */
-	public function getRedirectUrl();
+    /**
+     * Get redirect URL (if redirected)
+     *
+     * @return string
+     */
+    public function getRedirectUrl();
 
-	/**
-	 * Is response a redirect
-	 *  - Checks status codes
-	 *
-	 * @return boolean
-	 */
-	public function isRedirect();
+    /**
+     * Is response a redirect
+     *  - Checks status codes
+     *
+     * @return boolean
+     */
+    public function isRedirect();
 
-	/**
-	 * Get time string
-	 *
-	 * @return string
-	 */
-	public function getTime();
-}
+    /**
+     * Get time string
+     *
+     * @return string
+     */
+    public function getTime();
+}