Skip to content

Commit 8cf0d5e

Browse files
NikitaNikita
authored andcommitted
json field
1 parent 1238a57 commit 8cf0d5e

File tree

28 files changed

+271
-10
lines changed

28 files changed

+271
-10
lines changed

.jeeves.phpunit_v1.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ Mygento:
9696
type: decimal
9797
price:
9898
type: price
99+
options:
100+
type: json
99101
indexes:
100102
IX_CITY:
101103
columns: ["city"]
@@ -117,6 +119,9 @@ Mygento:
117119
type: varchar
118120
code:
119121
type: varchar
122+
options:
123+
type: json
124+
nullable: false
120125
category_id:
121126
type: int
122127
nullable: false

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"mygento/coding-standard": "~2.14.0"
2020
},
2121
"require-dev": {
22-
"phpunit/phpunit": "^10.0"
22+
"phpunit/phpunit": "^10.0",
23+
"symfony/var-dumper": "^6.4"
2324
},
2425
"autoload": {
2526
"psr-4": { "Mygento\\Jeeves\\": "src/Jeeves" }
@@ -33,4 +34,4 @@
3334
}
3435
},
3536
"bin": ["bin/jeeves"]
36-
}
37+
}

src/Jeeves/Generators/Crud/Common.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public function convertType($type)
116116
case 'real':
117117
case 'double':
118118
return 'float';
119+
case 'json':
120+
return 'array';
119121
default:
120122
return $type;
121123
}

src/Jeeves/Generators/Crud/Models/Model.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,38 @@ public function genModel(
6363
$generated = true;
6464
}
6565

66-
$cast = ' ';
66+
$cast = '';
6767
if ($notNullable && $value['type'] == 'boolean') {
68-
$cast = ' (bool) ';
68+
$cast = '(bool) ';
69+
}
70+
if ($notNullable && $withStore) {
71+
$cast = '(' . $this->convertType($value['type']) . ') ';
72+
}
73+
$getterBody = '$this->getData(self::' . strtoupper($name) . ')';
74+
if ($value['type'] == 'json') {
75+
$cast = '';
76+
$getterBody = 'json_decode($this->getData(self::OPTIONS) ?? ' . ($notNullable ? '\'[]\'' : '\'null\'') . ', true)';
6977
}
7078

7179
$getterName = $this->createGetterName($name, $value);
7280
$getter = $class->addMethod($getterName[0])
7381
->addComment($getterName[1])
7482
->setVisibility('public')
75-
->setBody('return' . $cast . '$this->getData(self::' . strtoupper($name) . ');');
83+
->setBody('return ' . $cast . $getterBody . ';');
7684

7785
$setterName = $this->createSetterName($name, $value);
7886
$setter = $class->addMethod($setterName[0])
7987
->addComment($setterName[1])
8088
->setVisibility('public');
8189
$setParam = $setter->addParameter($this->snakeCaseToCamelCase($name));
82-
$setter->setBody('return $this->setData(self::' . strtoupper($name) . ', $' . $this->snakeCaseToCamelCase($name) . ');');
90+
$setterBody = '$' . $this->snakeCaseToCamelCase($name);
91+
if ($value['type'] == 'json' && !$notNullable) {
92+
$setterBody = 'null === $options ? json_encode($options) : null';
93+
}
94+
if ($value['type'] == 'json' && $notNullable) {
95+
$setterBody = 'json_encode($options)';
96+
}
97+
$setter->setBody('return $this->setData(self::' . strtoupper($name) . ', ' . $setterBody . ');');
8398

8499
$getter->setReturnType($this->convertType($value['type']));
85100
$getter->setReturnNullable($generated ? true : !$notNullable);

src/Jeeves/Generators/Crud/Ui/Edit.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ private function getField(string $name, array $param, string $entity, string $pr
212212
}
213213

214214
switch ($param['type']) {
215+
case 'json':
216+
return [];
215217
case 'text':
216218
case 'mediumtext':
217219
case 'longtext':

src/Jeeves/Generators/Crud/Ui/Listing.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ private function getColumn(string $name, array $param, string $primaryKey): arra
442442
$notNullable = isset($param['nullable']) && $param['nullable'] === false;
443443
$options = null;
444444
switch ($param['type']) {
445+
case 'json':
446+
return [];
445447
case 'bool':
446448
case 'boolean':
447449
$filter = 'select';

src/Jeeves/Model/Crud/Database.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ private function mapColumn(string $column, array $param): DbColumn
123123
case 'text':
124124
case 'mediumtext':
125125
case 'longtext':
126+
case 'json':
126127
break;
127128
case 'varchar':
128129
$length = $param['length'] ?? 255;

src/Jeeves/Model/Crud/GraphQL.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function (string $column, array $param) {
4646
break;
4747
case 'blob':
4848
case 'varbinary':
49+
case 'json':
4950
return null;
5051
case 'int':
5152
case 'smallint':

test/Expectations/Crud/81/v1/Api/Data/CardInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface CardInterface extends IdentityInterface
1313
public const CARD_ID = 'card_id';
1414
public const TITLE = 'title';
1515
public const CODE = 'code';
16+
public const OPTIONS = 'options';
1617
public const CATEGORY_ID = 'category_id';
1718
public const IS_ACTIVE = 'is_active';
1819
public const STORE_ID = 'store_id';
@@ -53,6 +54,18 @@ public function getCode(): ?string;
5354
*/
5455
public function setCode(?string $code): self;
5556

57+
/**
58+
* Get options
59+
* @return array
60+
*/
61+
public function getOptions(): array;
62+
63+
/**
64+
* Set options
65+
* @return $this
66+
*/
67+
public function setOptions(array $options): self;
68+
5669
/**
5770
* Get category id
5871
* @return int

test/Expectations/Crud/81/v1/Api/Data/CartItemInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface CartItemInterface
1414
public const UPDATED_AT = 'updated_at';
1515
public const DISCOUNT = 'discount';
1616
public const PRICE = 'price';
17+
public const OPTIONS = 'options';
1718

1819
/**
1920
* Get cart id
@@ -115,6 +116,16 @@ public function getPrice(): ?float;
115116
*/
116117
public function setPrice(?float $price): self;
117118

119+
/**
120+
* Get options
121+
*/
122+
public function getOptions(): ?array;
123+
124+
/**
125+
* Set options
126+
*/
127+
public function setOptions(?array $options): self;
128+
118129
/**
119130
* Get ID
120131
*/

0 commit comments

Comments
 (0)