Skip to content

Commit 171e86a

Browse files
committed
Merge pull request #724 from webdevsHub/QueryBuilder
QueryBuilder
2 parents 95d0d67 + 8a14240 commit 171e86a

22 files changed

+2997
-0
lines changed

changes.txt

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ CHANGES
99
2014-11-19
1010
- Avoid remove previously added params when adding a suggest to the query #726
1111

12+
2014-11-16
13+
- Added Elastica\QueryBuilder #724
14+
1215
2014-11-13
1316
- fixed reserved words in queries which composed of upper case letters (Util::replaceBooleanWords) #722
1417

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace Elastica\Exception;
3+
4+
/**
5+
* QueryBuilder exception
6+
*
7+
* @package Elastica
8+
* @author Manuel Andreo Garcia <[email protected]>
9+
*/
10+
class QueryBuilderException extends \RuntimeException implements ExceptionInterface
11+
{
12+
}

lib/Elastica/QueryBuilder.php

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace Elastica;
4+
5+
use Elastica\Exception\QueryBuilderException;
6+
use Elastica\QueryBuilder\DSL;
7+
use Elastica\QueryBuilder\Facade;
8+
use Elastica\QueryBuilder\Version;
9+
use Elastica\QueryBuilder\Version\Version140;
10+
11+
/**
12+
* Query Builder
13+
*
14+
* @package Elastica
15+
* @author Manuel Andreo Garcia <[email protected]>
16+
*/
17+
class QueryBuilder
18+
{
19+
/**
20+
* @var Version
21+
*/
22+
private $_version;
23+
24+
/**
25+
* @var Facade[]
26+
*/
27+
private $_facades = array();
28+
29+
/**
30+
* Constructor
31+
*
32+
* @param Version $version
33+
*/
34+
public function __construct(Version $version = null)
35+
{
36+
$this->_version = $version ?: new Version140();
37+
38+
$this->addDSL(new DSL\Query());
39+
$this->addDSL(new DSL\Filter());
40+
$this->addDSL(new DSL\Aggregation());
41+
$this->addDSL(new DSL\Suggest());
42+
}
43+
44+
/**
45+
* Returns Facade for custom DSL object
46+
*
47+
* @param $dsl
48+
* @param array $arguments
49+
* @return Facade
50+
* @throws QueryBuilderException
51+
*/
52+
public function __call($dsl, array $arguments)
53+
{
54+
if (false === isset($this->_facades[$dsl])) {
55+
throw new QueryBuilderException('DSL "' . $dsl . '" not supported');
56+
}
57+
58+
return $this->_facades[$dsl];
59+
}
60+
61+
/**
62+
* Adds a new DSL object
63+
*
64+
* @param DSL $dsl
65+
*/
66+
public function addDSL(DSL $dsl)
67+
{
68+
$this->_facades[$dsl->getType()] = new Facade($dsl, $this->_version);
69+
}
70+
71+
/*
72+
* convenience methods
73+
*/
74+
75+
/**
76+
* Query DSL
77+
*
78+
* @return DSL\Query
79+
*/
80+
public function query()
81+
{
82+
return $this->_facades[DSL::TYPE_QUERY];
83+
}
84+
85+
/**
86+
* Filter DSL
87+
*
88+
* @return DSL\Filter
89+
*/
90+
public function filter()
91+
{
92+
return $this->_facades[DSL::TYPE_FILTER];
93+
}
94+
95+
/**
96+
* Aggregation DSL
97+
*
98+
* @return DSL\Aggregation
99+
*/
100+
public function aggregation()
101+
{
102+
return $this->_facades[DSL::TYPE_AGGREGATION];
103+
}
104+
105+
/**
106+
* Suggest DSL
107+
*
108+
* @return DSL\Suggest
109+
*/
110+
public function suggest()
111+
{
112+
return $this->_facades[DSL::TYPE_SUGGEST];
113+
}
114+
}

lib/Elastica/QueryBuilder/DSL.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Elastica\QueryBuilder;
4+
5+
/**
6+
* DSL Interface
7+
*
8+
* @package Elastica
9+
* @author Manuel Andreo Garcia <[email protected]>
10+
*/
11+
interface DSL
12+
{
13+
const TYPE_QUERY = 'query';
14+
const TYPE_FILTER = 'filter';
15+
const TYPE_AGGREGATION = 'aggregation';
16+
const TYPE_SUGGEST = 'suggest';
17+
18+
/**
19+
* must return type for QueryBuilder usage
20+
*
21+
* @return string
22+
*/
23+
public function getType();
24+
}

0 commit comments

Comments
 (0)