From bb8f28511370779219dcf3c323413e2360ad1aa2 Mon Sep 17 00:00:00 2001 From: alexeys Date: Wed, 30 Aug 2023 23:35:58 +0500 Subject: [PATCH] Organize code. --- hw11/src/.env | 5 + hw11/src/.env.example | 5 + hw11/src/app/App.php | 167 +++--------------- hw11/src/app/Helpers.php | 37 ++++ .../src/app/Services/PrepareOutputService.php | 52 ++++++ .../src/app/Services/PrepareSearchService.php | 101 +++++++++++ 6 files changed, 222 insertions(+), 145 deletions(-) create mode 100644 hw11/src/.env create mode 100644 hw11/src/.env.example create mode 100644 hw11/src/app/Helpers.php create mode 100644 hw11/src/app/Services/PrepareOutputService.php create mode 100644 hw11/src/app/Services/PrepareSearchService.php diff --git a/hw11/src/.env b/hw11/src/.env new file mode 100644 index 000000000..f2105a58d --- /dev/null +++ b/hw11/src/.env @@ -0,0 +1,5 @@ +ELASTIC_HOST=es:9200 +ELASTIC_USERNAME=elastic +ELASTIC_PASSWORD=elastic_password +ELASTIC_INDEX=books-shop +ELASTIC_MAX_OUTPUT_SIZE=100 diff --git a/hw11/src/.env.example b/hw11/src/.env.example new file mode 100644 index 000000000..52ab91c17 --- /dev/null +++ b/hw11/src/.env.example @@ -0,0 +1,5 @@ +ELASTIC_HOST= +ELASTIC_USERNAME= +ELASTIC_PASSWORD= +ELASTIC_INDEX= +ELASTIC_MAX_OUTPUT_SIZE= diff --git a/hw11/src/app/App.php b/hw11/src/app/App.php index 7e417032e..cecc16661 100644 --- a/hw11/src/app/App.php +++ b/hw11/src/app/App.php @@ -4,6 +4,8 @@ namespace App; +use App\Services\PrepareOutputService; +use App\Services\PrepareSearchService; use Elastic\Elasticsearch\Client; use Elastic\Elasticsearch\ClientBuilder; use Elastic\Elasticsearch\Exception\ClientResponseException; @@ -11,26 +13,27 @@ class App { - private const ELASTIC_HOST = 'es:9200'; - private const ELASTIC_USERNAME = 'elastic'; - private const ELASTIC_PASSWORD = 'elastic_password'; - private const ELASTIC_INDEX = 'books-shop'; - private const ELASTIC_MAX_OUTPUT_SIZE = 100; + use Helpers; - private array $operators = [ - '>' => 'gt', - '>=' => 'gte', - '<' => 'lt', - '<=' => 'lte', - ]; + private string $elasticHost; + private string $elasticUsername; + private string $elasticPassword; + private string $elasticIndex; + private string $elasticMaxOutputSize; private Client $client; public function __construct() { + $this->elasticHost = $this->env('ELASTIC_HOST'); + $this->elasticUsername = $this->env('ELASTIC_USERNAME'); + $this->elasticPassword = $this->env('ELASTIC_PASSWORD'); + $this->elasticIndex = $this->env('ELASTIC_INDEX'); + $this->elasticMaxOutputSize = $this->env('ELASTIC_MAX_OUTPUT_SIZE'); + $client = ClientBuilder::create() - ->setHosts([self::ELASTIC_HOST]) - ->setBasicAuthentication(self::ELASTIC_USERNAME, self::ELASTIC_PASSWORD) + ->setHosts([$this->elasticHost]) + ->setBasicAuthentication($this->elasticUsername, $this->elasticPassword) ->build(); $this->client = $client; @@ -38,11 +41,11 @@ public function __construct() public function get(): void { - $searchParams = $this->getSearchParams(); + $searchParams = PrepareSearchService::getSearchParams(); $params = [ - 'index' => self::ELASTIC_INDEX, - 'size' => self::ELASTIC_MAX_OUTPUT_SIZE, + 'index' => $this->elasticIndex, + 'size' => $this->elasticMaxOutputSize, 'body' => [ 'sort' => [ "_score" => "desc" @@ -56,7 +59,7 @@ public function get(): void try { $result = $this->client->search($params); } catch (ClientResponseException | ServerResponseException $e) { - echo $e->getResponse() . PHP_EOL; + echo $e->getMessage() . PHP_EOL; } if (isset($result) && isset($result['hits']) && isset($result['hits']['hits'])) { @@ -64,140 +67,14 @@ public function get(): void return $item['_source']; }, $result['hits']['hits']); - $this->outputResult($items); - } - } - - private function outputResult(array $items): void - { - foreach ($items as $i => $lines) { - foreach ($lines as $key => $value) { - if (!is_array($value)) { - $columns[$key][0] = $key; - $columns[$key][] = $value; - - $rows[$i][] = $value; - } else { - foreach ($value as $subKey => $subValue) { - $complexKey = $key . ' (' . $subValue['shop'] . ')'; - $columns[$complexKey][0] = $complexKey; - $columns[$complexKey][] = $subValue['stock']; - - $rows[$i][] = $subValue['stock']; - } - } - } - } - - $rows = [[...array_keys($columns)], ...$rows]; - $lengths = array_values(array_map(fn($x) => max(array_map('mb_strlen', $x)), $columns)); - - foreach ($rows as $row) { - foreach ($row as $key => $data) { - $string = (is_bool($data)) ? (($data) ? 'true' : 'false') : (string)$data; - - $lengthDelta = $lengths[$key] - mb_strlen($string, 'UTF-8'); - for ($i = 0; $i < $lengthDelta; $i++) { - $string .= ' '; - } - $string .= ' | '; - - echo $string; - } - echo PHP_EOL; - } - } - - private function getSearchParams(): array|bool - { - $params = $_SERVER['argv']; - - if (count($params) > 1) { - unset($params[0]); - - $searchParams = []; - - foreach ($params as $param) { - [$paramKey, $paramValue] = explode(':', $param); - - if ($paramKey === 'title') { - $value = [ - 'query' => $paramValue, - 'fuzziness' => 'auto', - ]; - } elseif ($paramKey === 'category') { - $value = $paramValue; - } else { - $value = $this->getValue($paramValue); - } - - if (($paramKey === 'price' || $paramKey === 'stock') && is_array($value)) { - $key = 'range'; - $filter = 'filter'; - - if ($paramKey === 'stock') { - $paramKey = 'stock.stock'; - } - } elseif ($paramKey === 'category') { - $key = 'term'; - $filter = 'must'; - $paramKey = 'category'; - } else { - $key = 'match'; - $filter = 'must'; - } - - if (!array_key_exists($filter, $searchParams)) { - $searchParams[$filter] = []; - } - - if (count($searchParams[$filter]) === 0) { - $searchParams[$filter][] = [$key => [$paramKey => $value]]; - } else { - foreach ($searchParams[$filter] as $existKey => $existFilter) { - if (!array_key_exists($key, $existFilter)) { - $searchParams[$filter][] = [$key => [$paramKey => $value]]; - } else { - if (!array_key_exists($paramKey, $existFilter[$key])) { - $searchParams[$filter][][$key] = [$paramKey => $value]; - } else { - if (is_array($searchParams[$filter][$existKey][$key][$paramKey])) { - $searchParams[$filter][$existKey][$key][$paramKey] = array_merge( - $searchParams[$filter][$existKey][$key][$paramKey], - $value - ); - } else { - $searchParams[$filter][$existKey][$key][$paramKey] = $value; - } - } - } - } - } - } - - return $searchParams; - } - - return false; - } - - private function getValue(string $value): array|string - { - $array = explode(' ', $value); - - if (count($array) > 1) { - $array[0] = array_key_exists($array[0], $this->operators) ? $this->operators[$array[0]] : $array[0]; - - return [$array[0] => $array[1]]; + PrepareOutputService::outputResult($items); } - - return $array[0]; } public function create(): void { $params = [ - 'index' => self::ELASTIC_INDEX, + 'index' => $this->elasticIndex, 'body' => [ 'mappings' => [ 'properties' => [ diff --git a/hw11/src/app/Helpers.php b/hw11/src/app/Helpers.php new file mode 100644 index 000000000..5e8438ecc --- /dev/null +++ b/hw11/src/app/Helpers.php @@ -0,0 +1,37 @@ + $lines) { + foreach ($lines as $key => $value) { + if (!is_array($value)) { + $columns[$key][0] = $key; + $columns[$key][] = $value; + + $rows[$i][] = $value; + } else { + foreach ($value as $subKey => $subValue) { + $complexKey = $key . ' (' . $subValue['shop'] . ')'; + $columns[$complexKey][0] = $complexKey; + $columns[$complexKey][] = $subValue['stock']; + + $rows[$i][] = $subValue['stock']; + } + } + } + } + + $rows = [[...array_keys($columns)], ...$rows]; + $lengths = array_values(array_map(fn($x) => max(array_map('mb_strlen', $x)), $columns)); + + foreach ($rows as $row) { + echo '| '; + foreach ($row as $key => $data) { + $string = (is_bool($data)) ? (($data) ? 'true' : 'false') : (string)$data; + + $lengthDelta = $lengths[$key] - mb_strlen($string, 'UTF-8'); + for ($i = 0; $i < $lengthDelta; $i++) { + $string .= ' '; + } + $string .= ' | '; + + echo $string; + } + echo PHP_EOL; + } + } +} diff --git a/hw11/src/app/Services/PrepareSearchService.php b/hw11/src/app/Services/PrepareSearchService.php new file mode 100644 index 000000000..8c0d1a20d --- /dev/null +++ b/hw11/src/app/Services/PrepareSearchService.php @@ -0,0 +1,101 @@ +' => 'gt', + '>=' => 'gte', + '<' => 'lt', + '<=' => 'lte', + ]; + + public static function getSearchParams(): array|bool + { + $params = $_SERVER['argv']; + + if (count($params) > 1) { + unset($params[0]); + + $searchParams = []; + + foreach ($params as $param) { + [$paramKey, $paramValue] = explode(':', $param); + + if ($paramKey === 'title') { + $value = [ + 'query' => $paramValue, + 'fuzziness' => 'auto', + ]; + } elseif ($paramKey === 'category') { + $value = $paramValue; + } else { + $value = self::getValue($paramValue); + } + + if (($paramKey === 'price' || $paramKey === 'stock') && is_array($value)) { + $key = 'range'; + $filter = 'filter'; + + if ($paramKey === 'stock') { + $paramKey = 'stock.stock'; + } + } elseif ($paramKey === 'category') { + $key = 'term'; + $filter = 'must'; + $paramKey = 'category'; + } else { + $key = 'match'; + $filter = 'must'; + } + + if (!array_key_exists($filter, $searchParams)) { + $searchParams[$filter] = []; + } + + if (count($searchParams[$filter]) === 0) { + $searchParams[$filter][] = [$key => [$paramKey => $value]]; + } else { + foreach ($searchParams[$filter] as $existKey => $existFilter) { + if (!array_key_exists($key, $existFilter)) { + $searchParams[$filter][] = [$key => [$paramKey => $value]]; + } else { + if (!array_key_exists($paramKey, $existFilter[$key])) { + $searchParams[$filter][][$key] = [$paramKey => $value]; + } else { + if (is_array($searchParams[$filter][$existKey][$key][$paramKey])) { + $searchParams[$filter][$existKey][$key][$paramKey] = array_merge( + $searchParams[$filter][$existKey][$key][$paramKey], + $value + ); + } else { + $searchParams[$filter][$existKey][$key][$paramKey] = $value; + } + } + } + } + } + } + + return $searchParams; + } + + return false; + } + + private static function getValue(string $value): array|string + { + $array = explode(' ', $value); + + if (count($array) > 1) { + $array[0] = array_key_exists($array[0], self::OPERATORS) ? self::OPERATORS[$array[0]] : $array[0]; + + return [$array[0] => $array[1]]; + } + + return $array[0]; + } +}