-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
222 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ELASTIC_HOST=es:9200 | ||
ELASTIC_USERNAME=elastic | ||
ELASTIC_PASSWORD=elastic_password | ||
ELASTIC_INDEX=books-shop | ||
ELASTIC_MAX_OUTPUT_SIZE=100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ELASTIC_HOST= | ||
ELASTIC_USERNAME= | ||
ELASTIC_PASSWORD= | ||
ELASTIC_INDEX= | ||
ELASTIC_MAX_OUTPUT_SIZE= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App; | ||
|
||
trait Helpers | ||
{ | ||
public function env(string $key = null): bool|string | ||
{ | ||
$filePath = __DIR__ . '/../.env'; | ||
|
||
if (!file_exists($filePath)) { | ||
echo 'Copy .env.example to .env file and fill in required values.' . PHP_EOL; | ||
exit; | ||
} | ||
|
||
if ($key) { | ||
$handle = fopen($filePath, 'r'); | ||
if ($handle) { | ||
while (($line = fgets($handle)) !== false) { | ||
if (strpos($line, '=')) { | ||
[$param, $value] = explode('=', $line); | ||
|
||
if (trim($param) === $key) { | ||
return trim($value); | ||
} | ||
} | ||
} | ||
|
||
fclose($handle); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services; | ||
|
||
class PrepareOutputService | ||
{ | ||
public static function outputResult(array $items): void | ||
{ | ||
$columns = []; | ||
$rows = []; | ||
|
||
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) { | ||
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.