-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathMakeNotionModel.php
77 lines (60 loc) · 2.43 KB
/
MakeNotionModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace FiveamCode\LaravelNotionApi\Console\Commands;
use FiveamCode\LaravelNotionApi\Notion;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class MakeNotionModel extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'notion:model {database_id} {database_name?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Make Notion Model based on a provided {database_id}';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$databaseId = $this->argument('database_id');
$databaseName = $this->argument('database_name');
$notion = new Notion(config('laravel-notion-api.notion-api-token'));
$this->info("Fetching structure of {$databaseId} database from Notion...");
$databaseStruct = $notion->databases()->find($databaseId);
$phpDocsProperties = '';
$visibleArray = '';
$propertyTypeMape = '';
$propertyTitleMap = '';
if ($databaseName === null) {
$databaseName = Str::singular(Str::studly(Str::slug($databaseStruct->getTitle(), '_')));
}
foreach ($databaseStruct->getProperties() as $propertyInfo) {
$reflection = new \ReflectionMethod($propertyInfo, 'getContent');
$propType = $reflection->getReturnType() ?? 'mixed';
$notionPropType = Str::studly(Str::slug($propertyInfo->getType(), '_'));
if ($reflection->getReturnType() !== null && ! $propType->isBuiltin()) {
$propType = '\\'.$propType->getName();
}
$propName = Str::slug($propertyInfo->getTitle(), '_');
$phpDocsProperties .= " * @property {$propType} \${$propName} $notionPropType \n";
$visibleArray .= " '$propName',\n";
$propertyTypeMape .= " '$propName' => '$notionPropType',\n";
$propertyTitleMap .= " '$propName' => '{$propertyInfo->getTitle()}',\n";
}
$contents = '<?php
';
File::ensureDirectoryExists('app/NotionModels');
File::put("app/NotionModels/$databaseName.php", $contents);
$this->info("Model for database {$this->argument('database_id')} has been created at 'app/NotionModels/$databaseName.php' .");
return 0;
}
}