@@ 22-73 (lines=52) @@ | ||
19 | * |
|
20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-and-query.html |
|
21 | */ |
|
22 | class AndQuery implements BuilderInterface |
|
23 | { |
|
24 | /** |
|
25 | * @var array |
|
26 | */ |
|
27 | private $queries; |
|
28 | ||
29 | /** |
|
30 | * @param array $queries |
|
31 | */ |
|
32 | public function __construct(array $queries = []) |
|
33 | { |
|
34 | $this->queries = $queries; |
|
35 | } |
|
36 | ||
37 | /** |
|
38 | * adds a query to join with AND operator |
|
39 | * |
|
40 | * @param BuilderInterface $query |
|
41 | */ |
|
42 | public function addQuery(BuilderInterface $query) |
|
43 | { |
|
44 | $this->queries[] = $query; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * {@inheritdoc} |
|
49 | */ |
|
50 | public function getType() |
|
51 | { |
|
52 | return 'and'; |
|
53 | } |
|
54 | ||
55 | /** |
|
56 | * {@inheritdoc} |
|
57 | */ |
|
58 | public function toArray() |
|
59 | { |
|
60 | $output = []; |
|
61 | foreach ($this->queries as $query) { |
|
62 | try { |
|
63 | $output[] = $query->toArray(); |
|
64 | } catch (\Error $e) { |
|
65 | throw new InvalidArgumentException( |
|
66 | 'Queries given to `AND` query must be instances of BuilderInterface' |
|
67 | ); |
|
68 | } |
|
69 | } |
|
70 | ||
71 | return [$this->getType() => $output]; |
|
72 | } |
|
73 | } |
|
74 |
@@ 22-73 (lines=52) @@ | ||
19 | * |
|
20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-or-query.html |
|
21 | */ |
|
22 | class OrQuery implements BuilderInterface |
|
23 | { |
|
24 | /** |
|
25 | * @var array |
|
26 | */ |
|
27 | private $queries; |
|
28 | ||
29 | /** |
|
30 | * @param array $queries |
|
31 | */ |
|
32 | public function __construct(array $queries = []) |
|
33 | { |
|
34 | $this->queries = $queries; |
|
35 | } |
|
36 | ||
37 | /** |
|
38 | * adds a query to join with AND operator |
|
39 | * |
|
40 | * @param BuilderInterface $query |
|
41 | */ |
|
42 | public function addQuery(BuilderInterface $query) |
|
43 | { |
|
44 | $this->queries[] = $query; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * {@inheritdoc} |
|
49 | */ |
|
50 | public function getType() |
|
51 | { |
|
52 | return 'or'; |
|
53 | } |
|
54 | ||
55 | /** |
|
56 | * {@inheritdoc} |
|
57 | */ |
|
58 | public function toArray() |
|
59 | { |
|
60 | $output = []; |
|
61 | foreach ($this->queries as $query) { |
|
62 | try { |
|
63 | $output[] = $query->toArray(); |
|
64 | } catch (\Error $e) { |
|
65 | throw new InvalidArgumentException( |
|
66 | 'Queries given to `OR` query must be instances of BuilderInterface' |
|
67 | ); |
|
68 | } |
|
69 | } |
|
70 | ||
71 | return [$this->getType() => $output]; |
|
72 | } |
|
73 | } |
|
74 |