Skip to content

Commit

Permalink
Modified Collection and Element classes to accept \GuzzleHttp\Message…
Browse files Browse the repository at this point in the history
…\Response as a constructor arg for acquia#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.
  • Loading branch information
webbj74-acquia committed May 8, 2015
1 parent 29329f3 commit 8380406
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
16 changes: 11 additions & 5 deletions src/Acquia/Rest/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Acquia\Rest;

use Guzzle\Http\Message\RequestInterface;

class Collection extends \ArrayObject
{
/**
Expand All @@ -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());
}

Expand Down
18 changes: 9 additions & 9 deletions src/Acquia/Rest/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Acquia\Rest;

use Guzzle\Http\Message\Request;

class Element extends \ArrayObject
{
/**
Expand All @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Acquia/Rest/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": ""
Expand Down

0 comments on commit 8380406

Please sign in to comment.