Skip to content

Commit edc8749

Browse files
committed
Added multiple objects return support via iterator, Traits, image mock files and more tests
1 parent 52ba387 commit edc8749

26 files changed

+914
-424
lines changed

src/Abstracts/Api.php

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Swader\Diffbot\Abstracts;
44

5-
use GuzzleHttp\Client;
65
use Swader\Diffbot\Diffbot;
7-
use Swader\Diffbot\Exceptions\DiffbotException;
86

97
/**
108
* Class Api
@@ -75,33 +73,10 @@ public function setTimeout($timeout = null)
7573
return $this;
7674
}
7775

78-
public function __call($name, $arguments)
79-
{
80-
$prefix = substr(lcfirst($name), 0, 3);
81-
$field = lcfirst(substr($name, 3));
82-
83-
$fields = static::getOptionalFields();
84-
if (in_array($field, $fields)) {
85-
if ($prefix == 'get') {
86-
return (isset($this->fieldSettings[$field])) ? $this->fieldSettings[$field] : false;
87-
}
88-
89-
if ($prefix == 'set') {
90-
if (is_bool($arguments[0]) || $arguments[0] === null) {
91-
$this->fieldSettings[$field] = (bool)$arguments[0];
92-
return $this;
93-
}
94-
throw new \InvalidArgumentException('Only booleans and null are allowed as optional field flags!');
95-
}
96-
throw new \BadMethodCallException('Prefix "'.$prefix.'" not allowed.');
97-
}
98-
throw new \BadMethodCallException($name . ': such a field does not exist for this API class.');
99-
}
100-
10176
public function call()
10277
{
10378
$response = $this->diffbot->getHttpClient()->get($this->buildUrl());
104-
return $this->diffbot->getEntityFactory()->createAppropriate($response);
79+
return $this->diffbot->getEntityFactory()->createAppropriateIterator($response);
10580
}
10681

10782
public function buildUrl()
@@ -112,14 +87,14 @@ public function buildUrl()
11287
$url .= '?token=' . $this->diffbot->getToken();
11388

11489
// Add URL
115-
$url .= '&url='.urlencode($this->url);
90+
$url .= '&url=' . urlencode($this->url);
91+
11692

11793
// Add Custom Fields
118-
$fields = static::getOptionalFields();
94+
$fields = $this->fieldSettings;
11995
$fieldString = '';
120-
foreach ($fields as $field) {
121-
$methodName = 'get' . ucfirst($field);
122-
$fieldString .= ($this->$methodName()) ? $field . ',' : '';
96+
foreach ($fields as $field => $value) {
97+
$fieldString .= ($value) ? $field . ',' : '';
12398
}
12499
$fieldString = trim($fieldString, ',');
125100
if ($fieldString != '') {
@@ -135,8 +110,10 @@ public function buildUrl()
135110
* @param Diffbot $d
136111
* @return $this
137112
*/
138-
public function registerDiffbot(Diffbot $d) {
113+
public function registerDiffbot(Diffbot $d)
114+
{
139115
$this->diffbot = $d;
140116
return $this;
141117
}
118+
142119
}

src/Abstracts/Entity.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,22 @@
22

33
namespace Swader\Diffbot\Abstracts;
44

5-
use GuzzleHttp\Message\Response;
6-
75
abstract class Entity
86
{
9-
/** @var Response */
10-
protected $response;
11-
12-
/** @var array */
13-
protected $objects;
7+
/** @var array */
8+
protected $data;
149

15-
public function __construct(Response $response)
10+
public function __construct(array $data)
1611
{
17-
$this->response = $response;
18-
$this->objects = $response->json()['objects'][0];
12+
$this->data = $data;
1913
}
2014

2115
/**
2216
* Returns the original response that was passed into the Entity
23-
* @return Response
17+
* @return array
2418
*/
25-
public function getResponse()
19+
public function getData()
2620
{
27-
return $this->response;
21+
return $this->data;
2822
}
2923
}

src/Api/Analyze.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,12 @@
33
namespace Swader\Diffbot\Api;
44

55
use Swader\Diffbot\Abstracts\Api;
6+
use Swader\Diffbot\Traits\StandardApi;
67

78
class Analyze extends Api
89
{
10+
use StandardApi;
11+
912
/** @var string API URL to which to send the request */
1013
protected $apiUrl = 'http://api.diffbot.com/v3/analyze';
11-
12-
protected static $optionalFields = [
13-
'links',
14-
'meta',
15-
'querystring'
16-
];
17-
18-
public static function getOptionalFields()
19-
{
20-
return self::$optionalFields;
21-
}
2214
}

src/Api/Article.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,12 @@
33
namespace Swader\Diffbot\Api;
44

55
use Swader\Diffbot\Abstracts\Api;
6+
use Swader\Diffbot\Traits\StandardApi;
67

78
class Article extends Api
89
{
10+
use StandardApi;
11+
912
/** @var string API URL to which to send the request */
1013
protected $apiUrl = 'http://api.diffbot.com/v3/article';
11-
12-
protected static $optionalFields = [
13-
'links',
14-
'meta',
15-
'querystring'
16-
];
17-
18-
public static function getOptionalFields()
19-
{
20-
return self::$optionalFields;
21-
}
2214
}

src/Api/Image.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,42 @@
33
namespace Swader\Diffbot\Api;
44

55
use Swader\Diffbot\Abstracts\Api;
6+
use Swader\Diffbot\Traits\StandardApi;
67

78
class Image extends Api
89
{
10+
use StandardApi;
11+
912
/** @var string API URL to which to send the request */
1013
protected $apiUrl = 'http://api.diffbot.com/v3/image';
1114

12-
protected static $optionalFields = [
13-
'height',
14-
'width',
15-
'links',
16-
'meta',
17-
'querystring',
18-
'breadcrumb',
15+
/**
16+
* Tells the API call to return the mentions field
17+
* @see https://www.diffbot.com/dev/docs/image/
18+
* @param $bool
19+
*/
20+
public function setMentions($bool)
21+
{
22+
$this->fieldSettings['mentions'] = (bool)$bool;
23+
}
1924

20-
'mentions',
21-
'ocr',
22-
'faces'
23-
];
25+
/**
26+
* Sets the API call to return the faces field
27+
* @see https://www.diffbot.com/dev/docs/image/
28+
* @param $bool
29+
*/
30+
public function setFaces($bool)
31+
{
32+
$this->fieldSettings['faces'] = (bool)$bool;
33+
}
2434

25-
public static function getOptionalFields()
35+
/**
36+
* Sets the API call to return the ocr field.
37+
* @see https://www.diffbot.com/dev/docs/image/
38+
* @param $bool
39+
*/
40+
public function setOcr($bool)
2641
{
27-
return self::$optionalFields;
42+
$this->fieldSettings['ocr'] = (bool)$bool;
2843
}
2944
}

src/Api/Product.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,13 @@
22

33
namespace Swader\Diffbot\Api;
44

5-
use GuzzleHttp\Client;
65
use Swader\Diffbot\Abstracts\Api;
6+
use Swader\Diffbot\Traits\StandardApi;
77

88
class Product extends Api
99
{
10+
use StandardApi;
11+
1012
/** @var string API URL to which to send the request */
1113
protected $apiUrl = 'http://api.diffbot.com/v3/product';
12-
13-
protected static $optionalFields = [
14-
'sku',
15-
'mpn',
16-
'shippingAmount',
17-
'saveAmount',
18-
'saveAmountDetails',
19-
'offerPriceDetails',
20-
'regularPriceDetails',
21-
'prefixCode',
22-
'productOrigin',
23-
'links',
24-
'meta',
25-
'querystring',
26-
'breadcrumb',
27-
'availability',
28-
'colors',
29-
'size'
30-
];
31-
32-
public static function getOptionalFields()
33-
{
34-
return self::$optionalFields;
35-
}
3614
}

0 commit comments

Comments
 (0)