Skip to content

Commit 04d3f81

Browse files
refactor into separate classes
1 parent c34103c commit 04d3f81

File tree

7 files changed

+207
-82
lines changed

7 files changed

+207
-82
lines changed

src/Helper/Esql.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/Helper/Esql/EsqlBase.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch\Helper\Esql;
16+
17+
abstract class EsqlBase {
18+
private ?EsqlBase $parent = null;
19+
20+
protected function format_kv(array $map): string
21+
{
22+
return implode(", ", array_map(
23+
function($k, $v) {
24+
return $k . "=" . json_encode($v);
25+
},
26+
array_keys($map),
27+
$map,
28+
));
29+
}
30+
31+
public function render(): string
32+
{
33+
$query = "";
34+
if ($this->parent) {
35+
$query .= $this->parent->render() . "\n| ";
36+
}
37+
$query .= $this->render_internal();
38+
return $query;
39+
}
40+
41+
protected abstract function render_internal(): string;
42+
43+
public function __construct(EsqlBase $parent)
44+
{
45+
$this->parent = $parent;
46+
}
47+
48+
public function __toString(): string
49+
{
50+
return $this->render() . "\n";
51+
}
52+
53+
public function limit(int $maxNumberOfRows): LimitCommand
54+
{
55+
return new LimitCommand($this, $maxNumberOfRows);
56+
}
57+
58+
public function where(string ...$expressions): WhereCommand
59+
{
60+
return new WhereCommand($this, $expressions);
61+
}
62+
}

src/Helper/Esql/FromCommand.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch\Helper\Esql;
16+
17+
class FromCommand extends EsqlBase {
18+
private array $indices;
19+
20+
public function __construct(array $indices)
21+
{
22+
$this->indices = $indices;
23+
}
24+
25+
protected function render_internal(): string
26+
{
27+
return "FROM " . implode(", ", $this->indices);
28+
}
29+
}

src/Helper/Esql/LimitCommand.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch\Helper\Esql;
16+
17+
class LimitCommand extends EsqlBase {
18+
private int $maxNumberOfRows;
19+
20+
public function __construct(EsqlBase $parent, int $maxNumberOfRows)
21+
{
22+
parent::__construct($parent);
23+
$this->maxNumberOfRows = $maxNumberOfRows;
24+
}
25+
26+
protected function render_internal(): string
27+
{
28+
return "LIMIT " . json_encode($this->maxNumberOfRows);
29+
}
30+
}

src/Helper/Esql/Query.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch\Helper\Esql;
16+
17+
abstract class Query {
18+
public static function from(string ...$indices): FromCommand
19+
{
20+
return new FromCommand($indices);
21+
}
22+
23+
public static function row(string ...$params): RowCommand
24+
{
25+
return new RowCommand($params);
26+
}
27+
}

src/Helper/Esql/RowCommand.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch\Helper\Esql;
16+
17+
class RowCommand extends EsqlBase {
18+
private array $params;
19+
20+
public function __construct(array $params)
21+
{
22+
$this->params = $params;
23+
}
24+
25+
protected function render_internal(): string
26+
{
27+
return "ROW " . $this->format_kv($this->params);
28+
}
29+
}

src/Helper/Esql/WhereCommand.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch\Helper\Esql;
16+
17+
class WhereCommand extends EsqlBase {
18+
private array $expressions;
19+
20+
public function __construct(EsqlBase $parent, array $expressions)
21+
{
22+
parent::__construct($parent);
23+
$this->expressions = $expressions;
24+
}
25+
26+
protected function render_internal(): string
27+
{
28+
return "WHERE " . implode(" AND ", $this->expressions);
29+
}
30+
}

0 commit comments

Comments
 (0)