Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
Elasticsearch DSL
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Public
Elasticsearch DSL
Commits
b448600b
Commit
b448600b
authored
9 years ago
by
Simonas Šerlinskas
Browse files
Options
Downloads
Patches
Plain Diff
introducing builder bag instead of named builder bag
parent
b263d6b9
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Aggregation/AbstractAggregation.php
+27
-29
27 additions, 29 deletions
src/Aggregation/AbstractAggregation.php
src/BuilderBag.php
+28
-29
28 additions, 29 deletions
src/BuilderBag.php
with
55 additions
and
58 deletions
src/Aggregation/AbstractAggregation.php
+
27
−
29
View file @
b448600b
...
@@ -11,18 +11,18 @@
...
@@ -11,18 +11,18 @@
namespace
ONGR\ElasticsearchDSL\Aggregation
;
namespace
ONGR\ElasticsearchDSL\Aggregation
;
use
ONGR\ElasticsearchDSL\NamedBuilderBag
;
use
ONGR\ElasticsearchDSL\BuilderBag
;
use
ONGR\ElasticsearchDSL\NamedBuilderInterface
;
use
ONGR\ElasticsearchDSL\BuilderInterface
;
use
ONGR\ElasticsearchDSL\NameAwareTrait
;
use
ONGR\ElasticsearchDSL\ParametersTrait
;
use
ONGR\ElasticsearchDSL\ParametersTrait
;
/**
/**
* AbstractAggregation class.
* AbstractAggregation class.
*/
*/
abstract
class
AbstractAggregation
implements
Named
BuilderInterface
abstract
class
AbstractAggregation
implements
BuilderInterface
{
{
use
ParametersTrait
;
use
ParametersTrait
;
use
NameAwareTrait
;
const
PREFIX
=
'agg_'
;
/**
/**
* @var string
* @var string
...
@@ -30,20 +30,10 @@ abstract class AbstractAggregation implements NamedBuilderInterface
...
@@ -30,20 +30,10 @@ abstract class AbstractAggregation implements NamedBuilderInterface
private
$field
;
private
$field
;
/**
/**
* @var string
* @var BuilderBag
*/
private
$name
;
/**
* @var NamedBuilderBag
*/
*/
private
$aggregations
;
private
$aggregations
;
/**
* @return string
*/
abstract
public
function
getType
();
/**
/**
* Abstract supportsNesting method.
* Abstract supportsNesting method.
*
*
...
@@ -63,9 +53,7 @@ abstract class AbstractAggregation implements NamedBuilderInterface
...
@@ -63,9 +53,7 @@ abstract class AbstractAggregation implements NamedBuilderInterface
*/
*/
public
function
__construct
(
$name
)
public
function
__construct
(
$name
)
{
{
$this
->
name
=
$name
;
$this
->
setName
(
$name
);
$this
->
aggregations
=
new
NamedBuilderBag
();
}
}
/**
/**
...
@@ -84,14 +72,6 @@ abstract class AbstractAggregation implements NamedBuilderInterface
...
@@ -84,14 +72,6 @@ abstract class AbstractAggregation implements NamedBuilderInterface
return
$this
->
field
;
return
$this
->
field
;
}
}
/**
* {@inheritdoc}
*/
public
function
getName
()
{
return
self
::
PREFIX
.
$this
->
name
;
}
/**
/**
* Adds a sub-aggregation.
* Adds a sub-aggregation.
*
*
...
@@ -99,17 +79,25 @@ abstract class AbstractAggregation implements NamedBuilderInterface
...
@@ -99,17 +79,25 @@ abstract class AbstractAggregation implements NamedBuilderInterface
*/
*/
public
function
addAggregation
(
AbstractAggregation
$abstractAggregation
)
public
function
addAggregation
(
AbstractAggregation
$abstractAggregation
)
{
{
if
(
!
$this
->
aggregations
)
{
$this
->
aggregations
=
$this
->
createBuilderBag
();
}
$this
->
aggregations
->
add
(
$abstractAggregation
);
$this
->
aggregations
->
add
(
$abstractAggregation
);
}
}
/**
/**
* Returns all sub aggregations.
* Returns all sub aggregations.
*
*
* @return
AbstractAggregation
[]
* @return
BuilderBag
[]
*/
*/
public
function
getAggregations
()
public
function
getAggregations
()
{
{
return
$this
->
aggregations
->
all
();
if
(
$this
->
aggregations
)
{
return
$this
->
aggregations
->
all
();
}
else
{
return
[];
}
}
}
/**
/**
...
@@ -149,4 +137,14 @@ abstract class AbstractAggregation implements NamedBuilderInterface
...
@@ -149,4 +137,14 @@ abstract class AbstractAggregation implements NamedBuilderInterface
return
$result
;
return
$result
;
}
}
/**
* Creates BuilderBag new instance.
*
* @return BuilderBag
*/
private
function
createBuilderBag
()
{
return
new
BuilderBag
();
}
}
}
This diff is collapsed.
Click to expand it.
src/
Named
BuilderBag.php
→
src/BuilderBag.php
+
28
−
29
View file @
b448600b
...
@@ -14,27 +14,17 @@ namespace ONGR\ElasticsearchDSL;
...
@@ -14,27 +14,17 @@ namespace ONGR\ElasticsearchDSL;
/**
/**
* Container for named builders.
* Container for named builders.
*/
*/
class
Named
BuilderBag
class
BuilderBag
{
{
/**
/**
* @var
Named
BuilderInterface[]
* @var BuilderInterface[]
*/
*/
private
$bag
=
[];
private
$bag
=
[];
/**
/**
* @param
Named
BuilderInterface[] $builders
* @param BuilderInterface[] $builders
*/
*/
public
function
__construct
(
array
$builders
=
[])
public
function
__construct
(
$builders
=
[])
{
$this
->
set
(
$builders
);
}
/**
* Replaces builders with new ones.
*
* @param NamedBuilderInterface[] $builders
*/
public
function
set
(
array
$builders
)
{
{
foreach
(
$builders
as
$builder
)
{
foreach
(
$builders
as
$builder
)
{
$this
->
add
(
$builder
);
$this
->
add
(
$builder
);
...
@@ -44,15 +34,25 @@ class NamedBuilderBag
...
@@ -44,15 +34,25 @@ class NamedBuilderBag
/**
/**
* Adds a builder.
* Adds a builder.
*
*
* @param NamedBuilderInterface $builder
* @param BuilderInterface $builder
*
* @return string
*/
*/
public
function
add
(
Named
BuilderInterface
$builder
)
public
function
add
(
BuilderInterface
$builder
)
{
{
$this
->
bag
[
$builder
->
getName
()]
=
$builder
;
if
(
method_exists
(
$builder
,
'getName'
))
{
$name
=
$builder
->
getName
();
}
else
{
$name
=
uniqid
();
}
$this
->
bag
[
$name
]
=
$builder
;
return
$name
;
}
}
/**
/**
* Checks if builder
is set by
name.
* Checks if builder
exists by a specific
name.
*
*
* @param string $name Builder name.
* @param string $name Builder name.
*
*
...
@@ -66,7 +66,7 @@ class NamedBuilderBag
...
@@ -66,7 +66,7 @@ class NamedBuilderBag
/**
/**
* Removes a builder by name.
* Removes a builder by name.
*
*
* @param string $name
B
uilder name.
* @param string $name
b
uilder name.
*/
*/
public
function
remove
(
$name
)
public
function
remove
(
$name
)
{
{
...
@@ -84,9 +84,9 @@ class NamedBuilderBag
...
@@ -84,9 +84,9 @@ class NamedBuilderBag
/**
/**
* Returns a builder by name.
* Returns a builder by name.
*
*
* @param string $name
B
uilder name.
* @param string $name
b
uilder name.
*
*
* @return
Named
BuilderInterface
* @return BuilderInterface
*/
*/
public
function
get
(
$name
)
public
function
get
(
$name
)
{
{
...
@@ -96,16 +96,16 @@ class NamedBuilderBag
...
@@ -96,16 +96,16 @@ class NamedBuilderBag
/**
/**
* Returns all builders contained.
* Returns all builders contained.
*
*
* @param string|null $type
B
uilder type.
* @param string|null $type
b
uilder type.
*
*
* @return
Named
BuilderInterface[]
* @return BuilderInterface[]
*/
*/
public
function
all
(
$type
=
null
)
public
function
all
(
$type
=
null
)
{
{
return
array_filter
(
return
array_filter
(
$this
->
bag
,
$this
->
bag
,
/** @var
Named
BuilderInterface $builder */
/** @var BuilderInterface $builder */
function
(
Named
BuilderInterface
$builder
)
use
(
$type
)
{
function
(
BuilderInterface
$builder
)
use
(
$type
)
{
return
$type
===
null
||
$builder
->
getType
()
==
$type
;
return
$type
===
null
||
$builder
->
getType
()
==
$type
;
}
}
);
);
...
@@ -116,11 +116,10 @@ class NamedBuilderBag
...
@@ -116,11 +116,10 @@ class NamedBuilderBag
*/
*/
public
function
toArray
()
public
function
toArray
()
{
{
$out
=
[];
$out
put
=
[];
foreach
(
$this
->
all
()
as
$builder
)
{
foreach
(
$this
->
all
()
as
$builder
)
{
$out
=
array_merge
(
$out
,
$builder
->
toArray
());
$out
put
=
array_merge
(
$out
put
,
$builder
->
toArray
());
}
}
return
$output
;
return
$out
;
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment