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