Skip to content

Commit 8c53b2a

Browse files
committed
polish notion model creation and usage
1 parent cf07ff9 commit 8c53b2a

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

src/Console/Commands/MakeNotionModel.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MakeNotionModel extends Command
2121
*
2222
* @var string
2323
*/
24-
protected $description = 'Make Notion Model';
24+
protected $description = 'Make Notion Model based on a provided {database_id}';
2525

2626
/**
2727
* Execute the console command.
@@ -59,6 +59,9 @@ public function handle()
5959
$propName = Str::slug($propertyInfo->getTitle(), '_');
6060
$phpDocsProperties .= " * @property {$propType} \${$propName} $notionPropType \n";
6161
$visibleArray .= " '$propName',\n";
62+
$propertyTypeMape .= " '$propName' => '$notionPropType',\n";
63+
$propertyTitleMap .= " '$propName' => '{$propertyInfo->getTitle()}',\n";
64+
6265
}
6366

6467
$contents = "<?php
@@ -79,10 +82,12 @@ class {$databaseName} extends NotionModel
7982
public static \$visible = [
8083
$visibleArray ];
8184
82-
/**
83-
* @var {$databaseName}Properties
84-
*/
85-
public static \$props;
85+
public static \$propertyTypeMap = [
86+
$propertyTypeMape ];
87+
88+
public static \$propertyTitleMap = [
89+
$propertyTitleMap ];
90+
8691
}
8792
8893
";

src/Models/NotionModel.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ class NotionModel
4141
*/
4242
public static $convertPropsToText = false;
4343

44-
public $props;
45-
4644
public Page $page;
4745

4846
public function __construct($databaseId = null)
@@ -52,6 +50,11 @@ public function __construct($databaseId = null)
5250
}
5351
}
5452

53+
public function getPage(): Page
54+
{
55+
return $this->page;
56+
}
57+
5558
public static function createInstance()
5659
{
5760
return new static();
@@ -77,13 +80,22 @@ public static function notionInstance(): Notion
7780
}
7881

7982
/**
80-
* @return NotionModel
83+
* @return NotionQueryBuilder
8184
*/
8285
public static function query(): NotionQueryBuilder
8386
{
8487
return new NotionQueryBuilder(static::class);
8588
}
8689

90+
91+
/**
92+
* @return NotionQueryBuilder
93+
*/
94+
public static function where($property, $operator, $value = null)
95+
{
96+
return static::query()->where($property, $operator, $value);
97+
}
98+
8799
/**
88100
* @return void
89101
*/

src/Models/NotionQueryBuilder.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function pluck($value, $key = null): Collection
4040
{
4141
$pageCollection = $this->internalQuery();
4242

43-
return $pageCollection->pluck('props.'.$value, $key !== null ? 'props.'.$key : null);
43+
return $pageCollection->pluck('props.' . $value, $key !== null ? 'props.' . $key : null);
4444
}
4545

4646
private function queryToNotion(int $limit = 100): PageCollection
@@ -81,9 +81,8 @@ private function internalQuery(int $limit = 100): Collection
8181

8282
foreach ($queryResponse->asCollection() as $pageItem) {
8383
$instance = $this->modelClass::createInstance($this->modelClass::$databaseId);
84-
// $instance->page = $pageItem;
85-
86-
// $instance->{'props'} = (object)[];
84+
$instance->page = $pageItem;
85+
8786
foreach ($pageItem->getProperties() as $propertyItem) {
8887
$propertyContent = $propertyItem->getContent();
8988
if ($this->modelClass::$convertPropsToText || $this->localConvertPropsToText) {
@@ -109,6 +108,14 @@ public function get()
109108
return $this->internalQuery();
110109
}
111110

111+
/**
112+
* @return static
113+
*/
114+
public function first()
115+
{
116+
return $this->get()->first();
117+
}
118+
112119
/**
113120
* @return string
114121
*/
@@ -187,7 +194,7 @@ public function where($property, $operator, $value = null)
187194
$value = $operator;
188195
$operator = Operators::EQUALS;
189196
} else {
190-
switch ($operator) {
197+
switch (Str::lower($operator)) {
191198
case '=':
192199
$operator = Operators::EQUALS;
193200
break;
@@ -206,6 +213,9 @@ public function where($property, $operator, $value = null)
206213
case '>=':
207214
$operator = Operators::GREATER_THAN_OR_EQUAL_TO;
208215
break;
216+
case 'contains':
217+
$operator = Operators::CONTAINS;
218+
break;
209219
}
210220
}
211221

@@ -258,9 +268,9 @@ private function cacheKey(): string
258268
{
259269
$postCacheKey = '';
260270
if ($this->nextCursor !== null) {
261-
$postCacheKey = '-'.$this->nextCursor->__toString();
271+
$postCacheKey = '-' . $this->nextCursor->__toString();
262272
}
263273

264-
return $this->modelClass::$preCacheKey.$this->modelClass::$databaseId.$postCacheKey;
274+
return $this->modelClass::$preCacheKey . $this->modelClass::$databaseId . $postCacheKey;
265275
}
266276
}

0 commit comments

Comments
 (0)