|
| 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 | +} |
0 commit comments