From 98ffe6132ac0aa277e283f132909da89baec7aa1 Mon Sep 17 00:00:00 2001 From: Jonathan Webb Date: Thu, 7 May 2015 20:04:55 -0400 Subject: [PATCH] Modified Collection and Element classes to accept \GuzzleHttp\Message\Response as a constructor arg for #54. This is a backward-compatible modification to Acquia\Rest\Collection and Acquia\Rest\Element so that the constructor may be used interchangeably with Guzzle 3.x and higher. Since guzzle isn't strictly required for the acquia/acquia-sdk-php-rest sub-package, I've added the old and present versions of Guzzle as composer suggestions, and removed the requirement on Guzzle 3. The constructor for Acquia\Rest\Collection is no longer strongly typed, but will throw an InvalidArgumentException if an unusual object is passed to it. --- src/Acquia/Rest/Collection.php | 16 +++++++++++----- src/Acquia/Rest/Element.php | 18 +++++++++--------- src/Acquia/Rest/composer.json | 5 ++++- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/Acquia/Rest/Collection.php b/src/Acquia/Rest/Collection.php index 536410b..efe169d 100644 --- a/src/Acquia/Rest/Collection.php +++ b/src/Acquia/Rest/Collection.php @@ -2,8 +2,6 @@ namespace Acquia\Rest; -use Guzzle\Http\Message\RequestInterface; - class Collection extends \ArrayObject { /** @@ -29,11 +27,19 @@ class Collection extends \ArrayObject protected $collectionProperty; /** - * @param \Guzzle\Http\Message\RequestInterface $request + * @param \Guzzle\Http\Message\RequestInterface|\GuzzleHttp\Message\Response $dataSource */ - public function __construct(RequestInterface $request) + public function __construct($dataSource) { - $this->response = $request->send(); + if (is_a($dataSource, '\Guzzle\Http\Message\RequestInterface')) { + $this->response = $dataSource->send(); + } elseif (is_a($dataSource, '\GuzzleHttp\Message\Response')) { + $this->response = $dataSource; + } else { + throw new \InvalidArgumentException( + sprintf("%s can't be constructed using data from %s.", get_class($this), get_class($dataSource)) + ); + } parent::__construct($this->response->json()); } diff --git a/src/Acquia/Rest/Element.php b/src/Acquia/Rest/Element.php index d72d48e..1e960bc 100644 --- a/src/Acquia/Rest/Element.php +++ b/src/Acquia/Rest/Element.php @@ -2,8 +2,6 @@ namespace Acquia\Rest; -use Guzzle\Http\Message\Request; - class Element extends \ArrayObject { /** @@ -12,16 +10,18 @@ class Element extends \ArrayObject protected $idColumn = 'name'; /** - * @param string|array|\Guzzle\Http\Message\Request $array + * @param string|array|\Guzzle\Http\Message\RequestInterface|\GuzzleHttp\Message\Response $dataSource */ - public function __construct($data) + public function __construct($dataSource) { - if ($data instanceof Request) { - $array = $data->send()->json(); - } elseif (is_string($data)) { - $array = array($this->idColumn => $data); + if (is_a($dataSource, '\Guzzle\Http\Message\RequestInterface')) { + $array = $dataSource->send()->json(); + } elseif (is_a($dataSource, '\GuzzleHttp\Message\Response')) { + $array = $dataSource->json(); + } elseif (is_string($dataSource)) { + $array = array($this->idColumn => $dataSource); } else { - $array = (array) $data; + $array = (array) $dataSource; } parent::__construct($array); } diff --git a/src/Acquia/Rest/composer.json b/src/Acquia/Rest/composer.json index 5e38b58..aadb08e 100644 --- a/src/Acquia/Rest/composer.json +++ b/src/Acquia/Rest/composer.json @@ -14,9 +14,12 @@ "require": { "php": ">=5.3.0", "acquia/acquia-sdk-php-json": "self.version", - "guzzle/service": "~3.0", "symfony/filesystem": "~2.0" }, + "suggest": { + "guzzlehttp/guzzle": "The current version of Guzzle may be useful with Collection and Element classes.", + "guzzle/service": "This older (deprecated) version of Guzzle may be useful with Collection and Element classes." + }, "autoload": { "psr-0": { "Acquia\\Rest": ""