|
| 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; |
| 16 | + |
| 17 | +abstract class ESQL { |
| 18 | + public static function from(string ...$indices): From |
| 19 | + { |
| 20 | + return new From($indices); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +abstract class ESQLBase { |
| 25 | + private ?ESQLBase $parent = null; |
| 26 | + |
| 27 | + public function render(): string |
| 28 | + { |
| 29 | + $query = ""; |
| 30 | + if ($this->parent) { |
| 31 | + $query .= $this->parent->render() . "\n| "; |
| 32 | + } |
| 33 | + $query .= $this->render_internal(); |
| 34 | + return $query; |
| 35 | + } |
| 36 | + |
| 37 | + protected abstract function render_internal(): string; |
| 38 | + |
| 39 | + public function __construct(ESQLBase $parent) |
| 40 | + { |
| 41 | + $this->parent = $parent; |
| 42 | + } |
| 43 | + |
| 44 | + public function __toString(): string |
| 45 | + { |
| 46 | + return $this->render(); |
| 47 | + } |
| 48 | + |
| 49 | + public function where(string ...$expressions): Where |
| 50 | + { |
| 51 | + return new Where($this, $expressions); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +class From extends ESQLBase { |
| 56 | + private array $indices; |
| 57 | + |
| 58 | + public function __construct(array $indices) |
| 59 | + { |
| 60 | + $this->indices = $indices; |
| 61 | + } |
| 62 | + |
| 63 | + protected function render_internal(): string |
| 64 | + { |
| 65 | + return "FROM " . implode(", ", $this->indices); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class Where extends ESQLBase { |
| 70 | + private array $expressions; |
| 71 | + |
| 72 | + public function __construct(ESQLBase $parent, array $expressions) |
| 73 | + { |
| 74 | + parent::__construct($parent); |
| 75 | + $this->expressions = $expressions; |
| 76 | + } |
| 77 | + |
| 78 | + protected function render_internal(): string |
| 79 | + { |
| 80 | + return "WHERE " . implode(" AND ", $this->expressions); |
| 81 | + } |
| 82 | +} |
0 commit comments