Passed
Branch master (8cb5b2)
by Christopher
04:37
created

Suggestion::phrase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 4
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Triadev\Leopard\Business\Dsl;
3
4
use ONGR\ElasticsearchDSL\Suggest\Suggest;
5
use Triadev\Leopard\Contract\ElasticsearchManagerContract;
6
use Triadev\Leopard\Facade\Leopard;
7
8
class Suggestion
9
{
10
    /** @var \ONGR\ElasticsearchDSL\Search */
11
    private $search;
12
    
13
    /** @var ElasticsearchManagerContract */
14
    private $manager;
15
    
16
    /**
17
     * Suggestion constructor.
18
     * @param ElasticsearchManagerContract $manager
19
     * @param \ONGR\ElasticsearchDSL\Search|null $search
20
     */
21 3
    public function __construct(
22
        ElasticsearchManagerContract $manager,
23
        ?\ONGR\ElasticsearchDSL\Search $search = null
24
    ) {
25 3
        $this->manager = $manager;
26 3
        $this->search = $search ?: new \ONGR\ElasticsearchDSL\Search();
27 3
    }
28
    
29
    /**
30
     * To dsl
31
     *
32
     * @return array
33
     */
34 3
    public function toDsl(): array
35
    {
36 3
        return $this->search->toArray();
37
    }
38
    
39
    /**
40
     * Get
41
     *
42
     * @param string $index
43
     * @return array
44
     */
45
    public function get(string $index): array
46
    {
47
        return Leopard::suggestStatement([
48
            'index' => $index,
49
            'body' => $this->toDsl()
50
        ]);
51
    }
52
    
53
    /**
54
     * Term
55
     *
56
     * @param string $name
57
     * @param string $text
58
     * @param string $field
59
     * @param array $params
60
     * @return Suggestion
61
     */
62 1
    public function term(string $name, string $text, string $field, array $params = []): Suggestion
63
    {
64 1
        $this->search->addSuggest(new Suggest(
65 1
            $name,
66 1
            'term',
67 1
            $text,
68 1
            $field,
69 1
            $params
70
        ));
71
        
72 1
        return $this;
73
    }
74
    
75
    /**
76
     * Phrase
77
     *
78
     * @param string $name
79
     * @param string $text
80
     * @param string $field
81
     * @param array $params
82
     * @return Suggestion
83
     */
84 1
    public function phrase(string $name, string $text, string $field, array $params = []): Suggestion
85
    {
86 1
        $this->search->addSuggest(new Suggest(
87 1
            $name,
88 1
            'phrase',
89 1
            $text,
90 1
            $field,
91 1
            $params
92
        ));
93
        
94 1
        return $this;
95
    }
96
    
97
    /**
98
     * Term
99
     *
100
     * @param string $name
101
     * @param string $text
102
     * @param string $field
103
     * @param array $params
104
     * @return Suggestion
105
     */
106 1
    public function completion(string $name, string $text, string $field, array $params = []): Suggestion
107
    {
108 1
        $this->search->addSuggest(new Suggest(
109 1
            $name,
110 1
            'completion',
111 1
            $text,
112 1
            $field,
113 1
            $params
114
        ));
115
        
116 1
        return $this;
117
    }
118
}
119