Highlight::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL\Highlight;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use ONGR\ElasticsearchDSL\ParametersTrait;
16
17
/**
18
 * Data holder for highlight api.
19
 */
20
class Highlight implements BuilderInterface
21
{
22
    use ParametersTrait;
23
24
    /**
25
     * @var array Holds fields for highlight.
26
     */
27
    private $fields = [];
28
29
    /**
30
     * @var array
31
     */
32
    private $tags;
33
34
    /**
35
     * @param string $name   Field name to highlight.
36
     * @param array  $params
37
     *
38
     * @return $this
39
     */
40
    public function addField($name, array $params = [])
41
    {
42
        $this->fields[$name] = $params;
43
44
        return $this;
45
    }
46
47
    /**
48
     * Sets html tag and its class used in highlighting.
49
     *
50
     * @param array $preTags
51
     * @param array $postTags
52
     *
53
     * @return $this
54
     */
55
    public function setTags(array $preTags, array $postTags)
56
    {
57
        $this->tags['pre_tags'] = $preTags;
58
        $this->tags['post_tags'] = $postTags;
59
60
        return $this;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getType()
67
    {
68
        return 'highlight';
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function toArray()
75
    {
76
        $output = [];
77
78
        if (is_array($this->tags)) {
79
            $output = $this->tags;
80
        }
81
82
        $output = $this->processArray($output);
83
84
        foreach ($this->fields as $field => $params) {
85
            $output['fields'][$field] = count($params) ? $params : new \stdClass();
86
        }
87
88
        return $output;
89
    }
90
}
91