1 | <?php |
||
20 | * Data holder for highlight api. |
||
21 | */ |
||
22 | class Highlight implements BuilderInterface |
||
23 | { |
||
24 | use ParametersTrait; |
||
25 | |||
26 | private array $fields = []; |
||
|
|||
27 | |||
28 | private ?array $tags = null; |
||
29 | |||
30 | public function addField(string $name, array $params = []): static |
||
31 | { |
||
32 | $this->fields[$name] = $params; |
||
33 | |||
34 | return $this; |
||
35 | } |
||
36 | |||
37 | public function setTags(array $preTags, array $postTags): static |
||
38 | { |
||
39 | $this->tags['pre_tags'] = $preTags; |
||
40 | $this->tags['post_tags'] = $postTags; |
||
41 | |||
42 | return $this; |
||
43 | } |
||
44 | |||
45 | public function getType(): string |
||
46 | { |
||
47 | return 'highlight'; |
||
48 | } |
||
49 | |||
50 | public function toArray(): array |
||
51 | { |
||
52 | $output = []; |
||
53 | |||
54 | if (is_array($this->tags)) { |
||
55 | $output = $this->tags; |
||
56 | } |
||
57 | |||
58 | $output = $this->processArray($output); |
||
59 | |||
60 | foreach ($this->fields as $field => $params) { |
||
61 | $output['fields'][$field] = count($params) ? $params : new \stdClass(); |
||
62 | } |
||
63 | |||
64 | return $output; |
||
65 | } |
||
66 | } |
||
67 |