|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
6
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
7
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
8
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
9
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
10
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
11
|
|
|
* THE SOFTWARE. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Ytake\LaravelCouchbase\Query; |
|
15
|
|
|
|
|
16
|
|
|
use Couchbase\Bucket; |
|
17
|
|
|
use Couchbase\SpatialViewQuery; |
|
18
|
|
|
use Couchbase\ViewQuery; |
|
19
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
|
20
|
|
|
use Ytake\LaravelCouchbase\Events\ViewQuerying; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class View. |
|
24
|
|
|
* |
|
25
|
|
|
* @see http://developer.couchbase.com/documentation/server/4.6/developer-guide/views-intro.html |
|
26
|
|
|
* |
|
27
|
|
|
* @author Yuuki Takezawa<[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class View |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var Bucket */ |
|
32
|
|
|
protected $bucket; |
|
33
|
|
|
|
|
34
|
|
|
/** @var Dispatcher */ |
|
35
|
|
|
protected $dispatcher; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Specifies the mode of updating to perorm before and after executing the query |
|
39
|
|
|
* |
|
40
|
|
|
* @see \Couchbase\ViewQuery::UPDATE_BEFORE |
|
41
|
|
|
* @see \Couchbase\ViewQuery::UPDATE_NONE |
|
42
|
|
|
* @see \Couchbase\ViewQuery::UPDATE_AFTER |
|
43
|
|
|
*/ |
|
44
|
|
|
private $consistency = null; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* View constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Bucket $bucket |
|
50
|
|
|
* @param Dispatcher|null $dispatcher |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function __construct(Bucket $bucket, Dispatcher $dispatcher = null) |
|
53
|
|
|
{ |
|
54
|
2 |
|
$this->bucket = $bucket; |
|
55
|
2 |
|
$this->dispatcher = $dispatcher; |
|
56
|
2 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $designDoc |
|
60
|
|
|
* @param string $name |
|
61
|
|
|
* |
|
62
|
|
|
* @return ViewQuery |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function from(string $designDoc, string $name): ViewQuery |
|
65
|
|
|
{ |
|
66
|
2 |
|
return ViewQuery::from($designDoc, $name); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $designDoc |
|
71
|
|
|
* @param string $name |
|
72
|
|
|
* |
|
73
|
|
|
* @return SpatialViewQuery |
|
74
|
|
|
*/ |
|
75
|
|
|
public function fromSpatial(string $designDoc, string $name): SpatialViewQuery |
|
76
|
|
|
{ |
|
77
|
|
|
return ViewQuery::fromSpatial($designDoc, $name); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param ViewQuery $viewQuery |
|
82
|
|
|
* @param bool $jsonAsArray |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function execute(ViewQuery $viewQuery, bool $jsonAsArray = false) |
|
87
|
|
|
{ |
|
88
|
2 |
|
if (isset($this->dispatcher)) { |
|
89
|
2 |
|
$this->dispatcher->dispatch(new ViewQuerying($viewQuery)); |
|
90
|
|
|
} |
|
91
|
2 |
|
if (!is_null($this->consistency)) { |
|
92
|
|
|
$viewQuery = $viewQuery->consistency($this->consistency); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
return $this->bucket->query($viewQuery, $jsonAsArray); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param int $consistency |
|
100
|
|
|
*/ |
|
101
|
|
|
public function consistency(int $consistency): void |
|
102
|
|
|
{ |
|
103
|
|
|
$this->consistency = $consistency; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|