Completed
Push — master ( 8386c2...ffd2be )
by Simonas
61:26
created

SearchResponse::getResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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\FilterManagerBundle\Search;
13
14
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
15
use ONGR\FilterManagerBundle\Filter\ViewData;
16
use ONGR\FilterManagerBundle\SerializableInterface;
17
18
/**
19
 * This class holds full response of documents and filters data.
20
 */
21
class SearchResponse implements SerializableInterface
22
{
23
    /**
24
     * @var DocumentIterator Elasticsearch response object.
25
     */
26
    private $result;
27
28
    /**
29
     * @var ViewData[] View data from filters.
30
     */
31
    private $filters;
32
33
    /**
34
     * @var array Url Parameters represents current link to list state.
35
     */
36
    private $urlParameters;
37
38
    /**
39
     * @param ViewData[]       $filters
40
     * @param DocumentIterator $result
41
     * @param array            $urlParameters
42
     */
43
    public function __construct($filters, $result, $urlParameters)
44
    {
45
        $this->filters = $filters;
46
        $this->result = $result;
47
        $this->urlParameters = $urlParameters;
48
    }
49
50
    /**
51
     * @return \ONGR\FilterManagerBundle\Filter\ViewData[]
52
     */
53
    public function getFilters()
54
    {
55
        return $this->filters;
56
    }
57
58
    /**
59
     * @return DocumentIterator
60
     */
61
    public function getResult()
62
    {
63
        return $this->result;
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function getUrlParameters()
70
    {
71
        return $this->urlParameters;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getSerializableData()
78
    {
79
        $data = [
80
            'count' => $this->result->count(),
81
            'documents' => [],
82
            'filters' => [],
83
            'url_parameters' => $this->urlParameters,
84
        ];
85
86
        foreach ($this->result as $document) {
87
            if (!$document instanceof SerializableInterface) {
88
                throw new \LogicException(
89
                    'In order to serialize search response documents MUST implement ' .
90
                        '"ONGR\FilterManagerBundle\SerializableInterface" interface.'
91
                );
92
            }
93
94
            $data['documents'][] = $document->getSerializableData();
95
        }
96
97
        foreach ($this->filters as $name => $filter) {
98
            $data['filters'][$name] = $filter->getSerializableData();
99
        }
100
101
        return $data;
102
    }
103
}
104