Skip to content

Instantly share code, notes, and snippets.

@webnitros
Created December 6, 2023 11:30
Show Gist options
  • Save webnitros/2e65df9160572c69014cfd76df6be6bc to your computer and use it in GitHub Desktop.
Save webnitros/2e65df9160572c69014cfd76df6be6bc to your computer and use it in GitHub Desktop.
Создание описание для openapi
<?php
/**
* Created by Andrey Stepanenko.
* User: webnitros
* Date: 06.05.2023
* Time: 01:15
*/
namespace App\Console\Commands;
use App\Models\Product;
use App\Models\PropertyReference;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;
class SchemaCommand extends Command
{
protected $signature = 'schema';
protected $description = 'Создание описание для OPENAPI с типами товаров';
public function handle(): void
{
$Product = Product::where('id', 1639)->first()->toArray();
foreach (PropertyReference::all() as $item) {
$name = $item->name;
$type = $item->type;
$field = $item->field;
if ('array' === $type) {
$type = 'json';
}
$value = @$Product[$field];
switch ($type) {
case 'json':
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
break;
case 'decimal':
$value = (float)$value;
break;
case 'integer':
$value = (int)$value;
break;
case 'boolean':
$value = $value ? 'true' : 'false';
break;
case 'text':
case 'string':
$value = "'{$value}'";
break;
default:
break;
}
echo "new OAT\Property(property: '{$field}', type: '{$type}', example: {$value}, description: '{$name}')," . PHP_EOL;
}
$tableName = 'products';
// Получаем список столбцов таблицы
$columnNames = Schema::getColumnListing($tableName);
foreach ($columnNames as $columnName) {
// Получаем тип поля столбца
$columnType = Schema::getColumnType($tableName, $columnName);
// Выводим информацию о столбце
echo "Столбец: $columnName, Тип: $columnType\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment