|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swader\Diffbot\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Swader\Diffbot\Abstracts\Api; |
|
6
|
|
|
use Swader\Diffbot\Entity\SearchInfo; |
|
7
|
|
|
use Swader\Diffbot\Traits\DiffbotAware; |
|
8
|
|
|
use Swader\Diffbot\Entity\EntityIterator; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Search |
|
12
|
|
|
* @see https://www.diffbot.com/dev/docs/search/ |
|
13
|
|
|
* @package Swader\Diffbot\Api |
|
14
|
|
|
*/ |
|
15
|
|
|
class Search extends Api |
|
16
|
|
|
{ |
|
17
|
|
|
use DiffbotAware; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string API URL to which to send the request */ |
|
20
|
|
|
protected $apiUrl = 'https://api.diffbot.com/v3/search'; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
protected $col = null; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string Search query to execute */ |
|
26
|
|
|
protected $query = ''; |
|
27
|
|
|
|
|
28
|
|
|
/** @var SearchInfo */ |
|
29
|
|
|
protected $info; |
|
30
|
|
|
|
|
31
|
|
|
const SEARCH_ALL = 'all'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Search query. |
|
35
|
|
|
* @see https://www.diffbot.com/dev/docs/search/#query |
|
36
|
|
|
* @param string string $q |
|
37
|
|
|
*/ |
|
38
|
14 |
|
public function __construct($q) |
|
39
|
|
|
{ |
|
40
|
14 |
|
$this->query = $q; |
|
41
|
14 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Name of the collection (Crawlbot or Bulk API job name) to search. |
|
45
|
|
|
* By default the search will operate on all of your token's collections. |
|
46
|
|
|
* |
|
47
|
|
|
* @param null|string $col |
|
48
|
|
|
* @return $this |
|
49
|
|
|
*/ |
|
50
|
5 |
|
public function setCol($col = null) |
|
51
|
|
|
{ |
|
52
|
5 |
|
if ($col !== null) { |
|
53
|
4 |
|
$this->otherOptions['col'] = $col; |
|
54
|
4 |
|
} else { |
|
55
|
1 |
|
unset($this->otherOptions['col']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
5 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Number of results to return. Default is 20. To return all results in |
|
63
|
|
|
* the search, pass num=all. |
|
64
|
|
|
* @param int $num |
|
65
|
|
|
* @return $this |
|
66
|
|
|
*/ |
|
67
|
5 |
|
public function setNum($num = 20) |
|
68
|
|
|
{ |
|
69
|
5 |
|
if (!is_numeric($num) && $num !== self::SEARCH_ALL) { |
|
70
|
1 |
|
throw new \InvalidArgumentException( |
|
71
|
|
|
'Argument can only be numeric or "all" to return all results.' |
|
72
|
1 |
|
); |
|
73
|
|
|
} |
|
74
|
4 |
|
$this->otherOptions['num'] = $num; |
|
75
|
|
|
|
|
76
|
4 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Ordinal position of first result to return. (First position is 0.) |
|
81
|
|
|
* Default is 0. |
|
82
|
|
|
* @param int $start |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
5 |
|
public function setStart($start = 0) |
|
86
|
|
|
{ |
|
87
|
5 |
|
if (!is_numeric($start)) { |
|
88
|
1 |
|
throw new \InvalidArgumentException( |
|
89
|
|
|
'Argument can only be numeric.' |
|
90
|
1 |
|
); |
|
91
|
|
|
} |
|
92
|
4 |
|
$this->otherOptions['start'] = $start; |
|
93
|
|
|
|
|
94
|
4 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Builds out the URL string that gets requested once `call()` is called |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
11 |
|
public function buildUrl() |
|
103
|
|
|
{ |
|
104
|
|
|
|
|
105
|
11 |
|
$url = rtrim($this->apiUrl, '/') . '?'; |
|
106
|
|
|
|
|
107
|
|
|
// Add token |
|
108
|
11 |
|
$url .= 'token=' . $this->diffbot->getToken(); |
|
109
|
|
|
|
|
110
|
|
|
// Add query |
|
111
|
11 |
|
$url .= '&query=' . urlencode($this->query); |
|
112
|
|
|
|
|
113
|
|
|
// Add other options |
|
114
|
11 |
|
foreach ($this->otherOptions as $option => $value) { |
|
115
|
7 |
|
$url .= '&' . $option . '=' . $value; |
|
116
|
11 |
|
} |
|
117
|
|
|
|
|
118
|
11 |
|
return $url; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* If you pass in `true`, you get back a SearchInfo object related to the |
|
123
|
|
|
* last call. Keep in mind that passing in true before calling a default |
|
124
|
|
|
* call() will implicitly call the call(), and then get the SearchInfo. |
|
125
|
|
|
* |
|
126
|
|
|
* So: |
|
127
|
|
|
* |
|
128
|
|
|
* $searchApi->call() // gets entities |
|
129
|
|
|
* $searchApi->call(true) // gets SearchInfo about the executed query |
|
130
|
|
|
* |
|
131
|
|
|
* @todo: remove error avoidance when issue 12 is fixed: https://github.com/Swader/diffbot-php-client/issues/12 |
|
132
|
|
|
* @param bool $info |
|
133
|
|
|
* @return EntityIterator|SearchInfo |
|
134
|
|
|
*/ |
|
135
|
2 |
|
public function call($info = false) |
|
136
|
|
|
{ |
|
137
|
2 |
|
if (!$info) { |
|
138
|
2 |
|
$ei = parent::call(); |
|
139
|
|
|
|
|
140
|
|
|
set_error_handler(function() { /* ignore errors */ }); |
|
141
|
2 |
|
$arr = json_decode((string)$ei->getResponse()->getBody(), true, 512, 1); |
|
142
|
2 |
|
restore_error_handler(); |
|
143
|
|
|
|
|
144
|
2 |
|
unset($arr['request']); |
|
145
|
2 |
|
unset($arr['objects']); |
|
146
|
|
|
|
|
147
|
2 |
|
$this->info = new SearchInfo($arr); |
|
148
|
|
|
|
|
149
|
2 |
|
return $ei; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
if ($info && !$this->info) { |
|
153
|
1 |
|
$this->call(); |
|
154
|
1 |
|
} |
|
155
|
|
|
|
|
156
|
1 |
|
return $this->info; |
|
157
|
|
|
} |
|
158
|
|
|
} |