1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Swader\Diffbot\Factory;
|
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
6
|
|
|
use Swader\Diffbot\Entity\EntityIterator;
|
7
|
|
|
use Swader\Diffbot\Exceptions\DiffbotException;
|
8
|
|
|
use Swader\Diffbot\Interfaces\EntityFactory;
|
9
|
|
|
|
10
|
|
|
class Entity implements EntityFactory
|
11
|
|
|
{
|
12
|
|
|
protected $apiEntities = [
|
13
|
|
|
'product' => '\Swader\Diffbot\Entity\Product',
|
14
|
|
|
'article' => '\Swader\Diffbot\Entity\Article',
|
15
|
|
|
'image' => '\Swader\Diffbot\Entity\Image',
|
16
|
|
|
'discussion' => '\Swader\Diffbot\Entity\Discussion',
|
17
|
|
|
'*' => '\Swader\Diffbot\Entity\Wildcard',
|
18
|
|
|
];
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* Creates an appropriate Entity from a given Response
|
22
|
|
|
* If no valid Entity can be found for typo of API, the Wildcard entity is selected
|
23
|
|
|
*
|
24
|
|
|
* @todo: remove error avoidance when issue 12 is fixed: https://github.com/Swader/diffbot-php-client/issues/12
|
25
|
|
|
* @param Response $response
|
26
|
|
|
* @return EntityIterator
|
27
|
|
|
* @throws DiffbotException
|
28
|
|
|
*/
|
29
|
164 |
|
public function createAppropriateIterator(Response $response)
|
30
|
|
|
{
|
31
|
164 |
|
$this->checkResponseFormat($response);
|
32
|
|
|
|
33
|
|
|
set_error_handler(function() { /* ignore errors */ });
|
34
|
161 |
|
$arr = json_decode($response->getBody(), true, 512, 1);
|
35
|
161 |
|
restore_error_handler();
|
36
|
|
|
|
37
|
161 |
|
$objects = [];
|
38
|
161 |
|
foreach ($arr['objects'] as $object) {
|
39
|
161 |
|
if (isset($this->apiEntities[$object['type']])) {
|
40
|
158 |
|
$class = $this->apiEntities[$object['type']];
|
41
|
158 |
|
} else {
|
42
|
3 |
|
$class = $this->apiEntities['*'];
|
43
|
|
|
}
|
44
|
161 |
|
$objects[] = new $class($object);
|
45
|
161 |
|
}
|
46
|
|
|
|
47
|
161 |
|
return new EntityIterator($objects, $response);
|
48
|
|
|
}
|
49
|
|
|
|
50
|
|
|
/**
|
51
|
|
|
* Makes sure the Diffbot response has all the fields it needs to work properly
|
52
|
|
|
*
|
53
|
|
|
* @todo: remove error avoidance when issue 12 is fixed: https://github.com/Swader/diffbot-php-client/issues/12
|
54
|
|
|
* @param Response $response
|
55
|
|
|
* @throws DiffbotException
|
56
|
|
|
*/
|
57
|
|
|
protected function checkResponseFormat(Response $response)
|
58
|
|
|
{
|
59
|
|
|
set_error_handler(function() { /* ignore errors */ });
|
60
|
164 |
|
$arr = json_decode($response->getBody(), true, 512, 1);
|
61
|
164 |
|
restore_error_handler();
|
62
|
|
|
|
63
|
164 |
|
if (isset($arr['error'])) {
|
64
|
1 |
|
throw new DiffbotException('Diffbot returned error ' . $arr['errorCode'] . ': ' . $arr['error']);
|
65
|
|
|
}
|
66
|
|
|
|
67
|
|
|
$required = [
|
68
|
163 |
|
'objects' => 'Objects property missing - cannot extract entity values',
|
69
|
|
|
'request' => 'Request property not found in response!'
|
70
|
163 |
|
];
|
71
|
|
|
|
72
|
163 |
|
foreach ($required as $k=>$v) {
|
73
|
163 |
|
if (!isset($arr[$k])) {
|
74
|
2 |
|
throw new DiffbotException($v);
|
75
|
|
|
}
|
76
|
162 |
|
}
|
77
|
|
|
|
78
|
|
|
}
|
79
|
|
|
} |