Skip to content

Commit f8577f0

Browse files
committed
Part 3 - code from post
1 parent c8dc0d0 commit f8577f0

File tree

10 files changed

+323
-22
lines changed

10 files changed

+323
-22
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"guzzlehttp/guzzle": "~5.0"
2020
},
2121
"require-dev": {
22-
"phpunit/phpunit" : "~4"
22+
"phpunit/phpunit" : "~4",
23+
"symfony/var-dumper": "~2"
2324
},
2425
"autoload": {
2526
"psr-4": {

src/Abstracts/Api.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Swader\Diffbot\Abstracts;
44

5+
use GuzzleHttp\Client;
6+
use Swader\Diffbot\Diffbot;
57
use Swader\Diffbot\Exceptions\DiffbotException;
68

79
/**
@@ -22,6 +24,10 @@ abstract class Api implements \Swader\Diffbot\Interfaces\Api
2224
/** @var array Settings on which optional fields to fetch */
2325
protected $fieldSettings = [];
2426

27+
/** @var Diffbot The parent class which spawned this one */
28+
protected $diffbot;
29+
30+
2531
public function __construct($url)
2632
{
2733
if (!is_string($url)) {
@@ -91,4 +97,46 @@ public function __call($name, $arguments)
9197
}
9298
throw new \BadMethodCallException($name . ': such a field does not exist for this API class.');
9399
}
100+
101+
public function call()
102+
{
103+
$response = $this->diffbot->getHttpClient()->get($this->buildUrl());
104+
return $this->diffbot->getEntityFactory()->createAppropriate($response);
105+
}
106+
107+
protected function buildUrl()
108+
{
109+
$url = rtrim($this->apiUrl, '/') . '/';
110+
111+
// Add Token
112+
$url .= '?token=' . $this->diffbot->getToken();
113+
114+
// Add URL
115+
$url .= '&url='.urlencode($this->url);
116+
117+
// Add Custom Fields
118+
$fields = static::getOptionalFields();
119+
$fieldString = '';
120+
foreach ($fields as $field) {
121+
$methodName = 'get' . ucfirst($field);
122+
$fieldString .= ($this->$methodName()) ? $field . ',' : '';
123+
}
124+
$fieldString = trim($fieldString, ',');
125+
if ($fieldString != '') {
126+
$url .= '&fields=' . $fieldString;
127+
}
128+
129+
return $url;
130+
}
131+
132+
/**
133+
* Sets the Diffbot instance on the child class
134+
* Used to later fetch the token, HTTP client, EntityFactory, etc
135+
* @param Diffbot $d
136+
* @return $this
137+
*/
138+
public function registerDiffbot(Diffbot $d) {
139+
$this->diffbot = $d;
140+
return $this;
141+
}
94142
}

src/Abstracts/Entity.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Abstracts;
4+
5+
use GuzzleHttp\Message\Response;
6+
7+
abstract class Entity
8+
{
9+
/** @var Response */
10+
protected $response;
11+
12+
/** @var array */
13+
protected $objects;
14+
15+
public function __construct(Response $response)
16+
{
17+
$this->response = $response;
18+
$this->objects = $response->json()['objects'][0];
19+
}
20+
21+
/**
22+
* Returns the original response that was passed into the Entity
23+
* @return Response
24+
*/
25+
public function getResponse()
26+
{
27+
return $this->response;
28+
}
29+
}

src/Api/Product.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,4 @@ public static function getOptionalFields()
3333
{
3434
return self::$optionalFields;
3535
}
36-
37-
public function call()
38-
{
39-
}
4036
}

src/Diffbot.php

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Swader\Diffbot\Api\Image;
88
use Swader\Diffbot\Api\Analyze;
99
use Swader\Diffbot\Api\Article;
10+
use GuzzleHttp\Client;
11+
use Swader\Diffbot\Factory\Entity;
12+
use Swader\Diffbot\Interfaces\EntityFactory;
1013

1114
/**
1215
* Class Diffbot
@@ -18,10 +21,16 @@
1821
class Diffbot
1922
{
2023
/** @var string The API access token */
21-
private static $token = null;
24+
protected static $token = null;
2225

2326
/** @var string The instance token, settable once per new instance */
24-
private $instanceToken;
27+
protected $instanceToken;
28+
29+
/** @var Client The HTTP clients to perform requests with */
30+
protected $client;
31+
32+
/** @var EntityFactory The Factory which created Entities from Responses */
33+
protected $factory;
2534

2635
/**
2736
* @param string|null $token The API access token, as obtained on diffbot.com/dev
@@ -72,6 +81,54 @@ public function getToken()
7281
return ($this->instanceToken) ? $this->instanceToken : self::$token;
7382
}
7483

84+
/**
85+
* Sets the client to be used for querying the API endpoints
86+
*
87+
* @param Client $client
88+
* @return $this
89+
*/
90+
public function setHttpClient(Client $client = null)
91+
{
92+
if ($client === null) {
93+
$client = new Client();
94+
}
95+
$this->client = $client;
96+
return $this;
97+
}
98+
99+
/**
100+
* Returns either the instance of the Guzzle client that has been defined, or null
101+
* @return Client|null
102+
*/
103+
public function getHttpClient()
104+
{
105+
return $this->client;
106+
}
107+
108+
/**
109+
* Sets the Entity Factory which will create the Entities from Responses
110+
* @param EntityFactory $factory
111+
* @return $this
112+
*/
113+
public function setEntityFactory(EntityFactory $factory = null)
114+
{
115+
if ($factory === null) {
116+
$factory = new Entity();
117+
}
118+
$this->factory = $factory;
119+
return $this;
120+
}
121+
122+
/**
123+
* Returns the Factory responsible for creating Entities from Responses
124+
* @return EntityFactory
125+
*/
126+
public function getEntityFactory()
127+
{
128+
return $this->factory;
129+
}
130+
131+
75132
/**
76133
* Creates a Product API interface
77134
*
@@ -80,40 +137,57 @@ public function getToken()
80137
*/
81138
public function createProductAPI($url)
82139
{
83-
return new Product($url);
140+
$api = new Product($url);
141+
if (!$this->getHttpClient()) {
142+
$this->setHttpClient();
143+
}
144+
return $api->registerDiffbot($this);
84145
}
85146

86147
/**
87148
* Creates an Article API interface
88149
*
89150
* @param $url string Url to analyze
90-
* @return Product
151+
* @return Article
91152
*/
92153
public function createArticleAPI($url)
93154
{
94-
return new Article($url);
155+
$api = new Article($url);
156+
if (!$this->getHttpClient()) {
157+
$this->setHttpClient();
158+
}
159+
return $api->registerDiffbot($this);
95160
}
96161

97162
/**
98163
* Creates an Image API interface
99164
*
100165
* @param $url string Url to analyze
101-
* @return Product
166+
* @return Image
102167
*/
103168
public function createImageAPI($url)
104169
{
105-
return new Image($url);
170+
$api = new Image($url);
171+
if (!$this->getHttpClient()) {
172+
$this->setHttpClient();
173+
}
174+
return $api->registerDiffbot($this);
106175
}
107176

108177
/**
109178
* Creates an Analyze API interface
110179
*
111180
* @param $url string Url to analyze
112-
* @return Product
181+
* @return Analyze
113182
*/
114183
public function createAnalyzeAPI($url)
115184
{
116-
return new Analyze($url);
185+
$api = new Analyze($url);
186+
if (!$this->getHttpClient()) {
187+
$this->setHttpClient();
188+
}
189+
return $api->registerDiffbot($this);
117190
}
118191

192+
119193
}

src/Entity/Product.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Entity;
4+
5+
use Swader\Diffbot\Abstracts\Entity;
6+
7+
class Product extends Entity
8+
{
9+
/**
10+
* Checks if the product has been determined available
11+
* @return bool
12+
*/
13+
public function isAvailable()
14+
{
15+
return (bool)$this->objects['availability'];
16+
}
17+
18+
/**
19+
* Returns the product offer price, in USD, as a floating point number
20+
* @return float
21+
*/
22+
public function getOfferPrice()
23+
{
24+
return (float)trim($this->objects['offerPrice'], '$');
25+
}
26+
27+
/**
28+
* Returns the brand, as determined by Diffbot
29+
* @return string
30+
*/
31+
public function getBrand()
32+
{
33+
return $this->objects['brand'];
34+
}
35+
36+
/**
37+
* Returns the title, as read by Diffbot
38+
* @return string
39+
*/
40+
public function getTitle()
41+
{
42+
return $this->objects['title'];
43+
}
44+
}

src/Factory/Entity.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Factory;
4+
5+
use GuzzleHttp\Message\Response;
6+
use Swader\Diffbot\Exceptions\DiffbotException;
7+
use Swader\Diffbot\Interfaces\EntityFactory;
8+
9+
class Entity implements EntityFactory
10+
{
11+
protected $apiEntities = [
12+
'product' => '\Swader\Diffbot\Entity\Product',
13+
'article' => '\Swader\Diffbot\Entity\Article',
14+
'image' => '\Swader\Diffbot\Entity\Image',
15+
'analyze' => '\Swader\Diffbot\Entity\Analyze',
16+
'*' => '\Swader\Diffbot\Entity\Wildcard',
17+
];
18+
19+
/**
20+
* Creates an appropriate Entity from a given Response
21+
* If no valid Entity can be found for typoe of API, the Wildcard entity is selected
22+
*
23+
* @param Response $response
24+
* @return \Swader\Diffbot\Abstracts\Entity
25+
* @throws DiffbotException
26+
*/
27+
public function createAppropriate(Response $response)
28+
{
29+
$this->checkResponseFormat($response);
30+
31+
$arr = $response->json();
32+
if (isset($this->apiEntities[$arr['request']['api']])) {
33+
$class = $this->apiEntities[$arr['request']['api']];
34+
} else {
35+
$class = $this->apiEntities['*'];
36+
}
37+
return new $class($response);
38+
}
39+
40+
/**
41+
* Makes sure the Diffbot response has all the fields it needs to work properly
42+
*
43+
* @param Response $response
44+
* @throws DiffbotException
45+
*/
46+
protected function checkResponseFormat(Response $response)
47+
{
48+
$arr = $response->json();
49+
50+
if (!isset($arr['objects'])) {
51+
throw new DiffbotException('Objects property missing - cannot extract entity values');
52+
}
53+
54+
if (!isset($arr['request'])) {
55+
throw new DiffbotException('Request property not found in response!');
56+
}
57+
58+
if (!isset($arr['request']['api'])) {
59+
throw new DiffbotException('API property not found in request property of response!');
60+
}
61+
}
62+
}

src/Interfaces/EntityFactory.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Interfaces;
4+
5+
use GuzzleHttp\Message\Response;
6+
7+
interface EntityFactory
8+
{
9+
/**
10+
* Returns the appropriate entity as built by the contents of $response
11+
*
12+
* @param Response $response
13+
* @return Entity
14+
*/
15+
public function createAppropriate(Response $response);
16+
}

0 commit comments

Comments
 (0)