Skip to content

Commit 60fda35

Browse files
committed
💥
1 parent ac57548 commit 60fda35

File tree

6 files changed

+216
-3
lines changed

6 files changed

+216
-3
lines changed

src/ElasticQuery.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ElasticQuery implements \Spameri\ElasticQuery\Entity\ArrayInterface
3535
public function __construct(
3636
?\Spameri\ElasticQuery\Query\QueryCollection $query = NULL
3737
, ?\Spameri\ElasticQuery\Filter\FilterCollection $filter = NULL
38-
, $sort = NULL
38+
, \Spameri\ElasticQuery\Options\SortCollection $sort = NULL
3939
, $aggregation = NULL
4040
, int $from = NULL
4141
, int $size = NULL
@@ -75,7 +75,7 @@ public function toArray() : array
7575
}
7676

7777
if ($this->sort) {
78-
$array['sort'] = $this->sort;
78+
$array['sort'] = $this->sort->toArray();
7979
}
8080

8181
if ($this->aggregation) {

src/Options/Sort.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Options;
4+
5+
6+
class Sort extends \Spameri\ElasticQuery\Entity\AbstractEntity
7+
{
8+
public const ASC = 'ASC';
9+
public const DESC = 'DESC';
10+
11+
/**
12+
* @var string
13+
*/
14+
private $field;
15+
/**
16+
* @var string
17+
*/
18+
private $type;
19+
/**
20+
* @var string
21+
*/
22+
private $missing;
23+
24+
25+
public function __construct(
26+
string $field
27+
, string $type = self::DESC
28+
, string $missing = '_last'
29+
)
30+
{
31+
if ( ! \in_array($type, [self::ASC, self::DESC], TRUE)) {
32+
throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException(
33+
'Sorting type ' . $type . ' is out of allowed range. See \Spameri\ElasticQuery\Options\Sort for reference.'
34+
);
35+
}
36+
37+
$this->field = $field;
38+
$this->type = $type;
39+
$this->missing = $missing;
40+
}
41+
42+
43+
public function key() : string
44+
{
45+
return $this->field;
46+
}
47+
48+
49+
public function toArray() : array
50+
{
51+
return [
52+
$this->field => [
53+
'order' => $this->type,
54+
'missing' => $this->missing,
55+
],
56+
];
57+
}
58+
59+
}

src/Options/SortCollection.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Options;
4+
5+
6+
class SortCollection extends \Spameri\ElasticQuery\Query\AbstractLeafQuery
7+
{
8+
9+
/**
10+
* @var \Spameri\ElasticQuery\Options\Sort[]
11+
*/
12+
private $collection;
13+
14+
15+
public function __construct(
16+
Sort ... $collection
17+
)
18+
{
19+
$this->collection = $collection;
20+
}
21+
22+
23+
public function key() : string
24+
{
25+
return '';
26+
}
27+
28+
29+
public function toArray() : array
30+
{
31+
$array = [];
32+
33+
foreach ($this->collection as $sort) {
34+
$array[] = $sort->toArray();
35+
}
36+
37+
return $array;
38+
}
39+
40+
}

src/Query/MustNotCollection.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Query;
4+
5+
6+
class MustNotCollection implements \Spameri\ElasticQuery\Collection\CollectionInterface
7+
{
8+
9+
/**
10+
* @var \Spameri\ElasticQuery\Query\AbstractLeafQuery[]
11+
*/
12+
private $collection;
13+
14+
15+
public function __construct(
16+
\Spameri\ElasticQuery\Query\AbstractLeafQuery ... $collection
17+
)
18+
{
19+
$this->collection = $collection;
20+
}
21+
22+
23+
public function add(
24+
\Spameri\ElasticQuery\Query\AbstractLeafQuery $item
25+
) : void
26+
{
27+
$this->collection[$item->key()] = $item;
28+
}
29+
30+
31+
public function remove(
32+
string $key
33+
) : bool
34+
{
35+
if (isset($this->collection[$key])) {
36+
unset($this->collection[$key]);
37+
38+
return TRUE;
39+
}
40+
41+
return FALSE;
42+
}
43+
44+
45+
public function get(
46+
string $key
47+
) : ?\Spameri\ElasticQuery\Query\AbstractLeafQuery
48+
{
49+
if (isset($this->collection[$key])) {
50+
return $this->collection[$key];
51+
}
52+
53+
return NULL;
54+
}
55+
56+
57+
public function isValue(
58+
string $key
59+
) : bool
60+
{
61+
if (isset($this->collection[$key])) {
62+
return TRUE;
63+
}
64+
65+
return FALSE;
66+
}
67+
68+
69+
public function count() : int
70+
{
71+
return \count($this->collection);
72+
}
73+
74+
75+
public function keys() : array
76+
{
77+
return \array_map('\strval', \array_keys($this->collection));
78+
}
79+
80+
81+
public function clear() : void
82+
{
83+
$this->collection = [];
84+
}
85+
86+
87+
public function getIterator() : \ArrayIterator
88+
{
89+
return new \ArrayIterator($this->collection);
90+
}
91+
92+
}

src/Query/QueryCollection.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,33 @@ class QueryCollection extends \Spameri\ElasticQuery\Query\AbstractLeafQuery
1414
* @var \Spameri\ElasticQuery\Query\ShouldCollection
1515
*/
1616
private $shouldCollection;
17+
/**
18+
* @var null|\Spameri\ElasticQuery\Query\MustNotCollection
19+
*/
20+
private $mustNotCollection;
1721

1822

1923
public function __construct(
2024
?\Spameri\ElasticQuery\Query\MustCollection $mustCollection = NULL,
21-
?\Spameri\ElasticQuery\Query\ShouldCollection $shouldCollection = NULL
25+
?\Spameri\ElasticQuery\Query\ShouldCollection $shouldCollection = NULL,
26+
?\Spameri\ElasticQuery\Query\MustNotCollection $mustNotCollection = NULL
2227
)
2328
{
2429
if ( ! $mustCollection) {
2530
$mustCollection = new \Spameri\ElasticQuery\Query\MustCollection();
2631
}
2732

33+
if ( ! $mustNotCollection) {
34+
$mustNotCollection = new \Spameri\ElasticQuery\Query\MustNotCollection();
35+
}
36+
2837
if ( ! $shouldCollection) {
2938
$shouldCollection = new \Spameri\ElasticQuery\Query\ShouldCollection();
3039
}
3140

3241
$this->mustCollection = $mustCollection;
3342
$this->shouldCollection = $shouldCollection;
43+
$this->mustNotCollection = $mustNotCollection;
3444
}
3545

3646

@@ -59,6 +69,12 @@ public function toArray() : array
5969
foreach ($this->mustCollection as $item) {
6070
$array['bool']['must'][] = $item->toArray();
6171
}
72+
73+
/** @var \Spameri\ElasticQuery\Query\AbstractLeafQuery $item */
74+
foreach ($this->mustNotCollection as $item) {
75+
$array['bool']['must_not'][] = $item->toArray();
76+
}
77+
6278
/** @var \Spameri\ElasticQuery\Query\AbstractLeafQuery $item */
6379
foreach ($this->shouldCollection as $item) {
6480
$array['bool']['should'][] = $item->toArray();

src/Query/Terms.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public function __construct(
2626
float $boost = 1.0
2727
)
2828
{
29+
if ( ! \count($query)) {
30+
throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException(
31+
'Terms query must contain values, empty array given.'
32+
);
33+
}
34+
2935
$this->field = $field;
3036
$this->query = $query;
3137
$this->boost = $boost;

0 commit comments

Comments
 (0)