Completed
Pull Request — master (#1)
by Christopher
08:14 queued 02:30
created

AbstractSearch::toDsl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Triadev\Es\Dsl\Dsl;
3
4
use ONGR\ElasticsearchDSL\BuilderInterface;
5
use ONGR\ElasticsearchDSL\Search as OngrSearch;
6
7
abstract class AbstractSearch
8
{
9
    /** @var OngrSearch */
10
    protected $search;
11
    
12
    /** @var string|null */
13
    private $esIndex;
14
    
15
    /** @var string|null */
16
    private $esType;
17
    
18
    /**
19
     * AbstractDsl constructor.
20
     * @param OngrSearch|null $search
21
     */
22 22
    public function __construct(?OngrSearch $search = null)
23
    {
24 22
        $this->search = $search ?: new OngrSearch();
25 22
    }
26
    
27
    /**
28
     * Overwrite the default elasticsearch index
29
     *
30
     * @param string $index
31
     * @return AbstractSearch
32
     */
33
    public function esIndex(string $index) : AbstractSearch
34
    {
35
        $this->esIndex = $index;
36
        return $this;
37
    }
38
    
39
    /**
40
     * Get elasticsearch index
41
     *
42
     * @return string
43
     */
44
    public function getEsIndex() : string
45
    {
46
        return $this->esIndex ?: config('laravel-elasticsearch-dsl.index');
47
    }
48
    
49
    /**
50
     * Overwrite the default elasticsearch type
51
     *
52
     * @param string $type
53
     * @return AbstractSearch
54
     */
55
    public function esType(string $type) : AbstractSearch
56
    {
57
        $this->esType = $type;
58
        return $this;
59
    }
60
    
61
    /**
62
     * Get elasticsearch type
63
     *
64
     * @return string|null
65
     */
66
    public function getEsType() : ?string
67
    {
68
        return $this->esType;
69
    }
70
    
71
    /**
72
     * Get current search
73
     *
74
     * @return OngrSearch
75
     */
76 13
    protected function getCurrentSearch() : OngrSearch
77
    {
78 13
        return $this->search;
79
    }
80
    
81
    /**
82
     * To dsl
83
     *
84
     * @return array
85
     */
86 22
    public function toDsl() : array
87
    {
88 22
        return $this->search->toArray();
89
    }
90
    
91
    /**
92
     * Get search
93
     *
94
     * @return OngrSearch
95
     */
96 2
    public function getSearch() : OngrSearch
97
    {
98 2
        return $this->search;
99
    }
100
    
101
    /**
102
     * Get query
103
     *
104
     * @return BuilderInterface
105
     */
106 11
    public function getQuery() : BuilderInterface
107
    {
108 11
        return $this->search->getQueries();
109
    }
110
}
111