|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Psonic; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Psonic\Channels\Channel; |
|
7
|
|
|
use Psonic\Contracts\Client; |
|
|
|
|
|
|
8
|
|
|
use Psonic\Commands\Search\QueryCommand; |
|
9
|
|
|
use Psonic\Commands\Search\SuggestCommand; |
|
10
|
|
|
use Psonic\Exceptions\CommandFailedException; |
|
11
|
|
|
use Psonic\Commands\Search\StartSearchChannelCommand; |
|
12
|
|
|
|
|
13
|
|
|
class Search extends Channel |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Search Channel constructor. |
|
17
|
|
|
* @param Client $client |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct(Client $client) |
|
20
|
|
|
{ |
|
21
|
|
|
parent::__construct($client); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return mixed|Contracts\Response|void |
|
26
|
|
|
* @throws Exceptions\ConnectionException |
|
27
|
|
|
*/ |
|
28
|
|
|
public function connect($password = 'SecretPassword') |
|
29
|
|
|
{ |
|
30
|
|
|
parent::connect(); |
|
31
|
|
|
|
|
32
|
|
|
$response = $this->send(new StartSearchChannelCommand($password)); |
|
33
|
|
|
|
|
34
|
|
|
if ($bufferSize = $response->get('bufferSize')) { |
|
35
|
|
|
$this->bufferSize = (int)$bufferSize; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $response; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param $collection |
|
43
|
|
|
* @param $bucket |
|
44
|
|
|
* @param $terms |
|
45
|
|
|
* @param $limit |
|
46
|
|
|
* @param $offset |
|
47
|
|
|
* @param $locale |
|
48
|
|
|
* @return array |
|
49
|
|
|
* @throws CommandFailedException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function query($collection, $bucket, $terms, $limit = null, $offset = null, $locale = null): array |
|
52
|
|
|
{ |
|
53
|
|
|
$response = $this->send(new QueryCommand($collection, $bucket, $terms, $limit, $offset, $locale)); |
|
54
|
|
|
|
|
55
|
|
|
if (!$response->getStatus() == 'PENDING') { |
|
56
|
|
|
throw new CommandFailedException; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$results = $this->read(); |
|
60
|
|
|
|
|
61
|
|
|
if (!$results->getStatus() == 'EVENT') { |
|
62
|
|
|
throw new CommandFailedException; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $results->getResults(); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $collection |
|
70
|
|
|
* @param $bucket |
|
71
|
|
|
* @param $terms |
|
72
|
|
|
* @param $limit |
|
73
|
|
|
* @return array |
|
74
|
|
|
* @throws CommandFailedException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function suggest($collection, $bucket, $terms, $limit = null): array |
|
77
|
|
|
{ |
|
78
|
|
|
$response = $this->send(new SuggestCommand($collection, $bucket, $terms, $limit)); |
|
79
|
|
|
|
|
80
|
|
|
if (!$response->getStatus() == 'PENDING') { |
|
81
|
|
|
throw new CommandFailedException; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$results = $this->read(); |
|
85
|
|
|
|
|
86
|
|
|
if (!$results->getStatus() == 'EVENT') { |
|
87
|
|
|
throw new CommandFailedException; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $results->getResults(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: