Skip to content

Commit a1a23f4

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

18 files changed

Lines changed: 141 additions & 2 deletions

File tree

.jeeves.phpunit_v1.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ Mygento:
153153
type: varchar
154154
subname:
155155
type: varchar
156+
options:
157+
type: json
156158
family:
157159
type: varchar
158160
active:

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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,30 @@ public function genModel(
6767
if ($notNullable && $value['type'] == 'boolean') {
6868
$cast = ' (bool) ';
6969
}
70+
$getterBody = '$this->getData(self::' . strtoupper($name) . ')';
71+
if ($value['type'] == 'json') {
72+
$getterBody = 'json_decode($this->getData(self::OPTIONS) ?? ' . ($notNullable ? '\'[]\'' : '\'null\'') . ', true)';
73+
}
7074

7175
$getterName = $this->createGetterName($name, $value);
7276
$getter = $class->addMethod($getterName[0])
7377
->addComment($getterName[1])
7478
->setVisibility('public')
75-
->setBody('return' . $cast . '$this->getData(self::' . strtoupper($name) . ');');
79+
->setBody('return' . $cast . $getterBody . ';');
7680

7781
$setterName = $this->createSetterName($name, $value);
7882
$setter = $class->addMethod($setterName[0])
7983
->addComment($setterName[1])
8084
->setVisibility('public');
8185
$setParam = $setter->addParameter($this->snakeCaseToCamelCase($name));
82-
$setter->setBody('return $this->setData(self::' . strtoupper($name) . ', $' . $this->snakeCaseToCamelCase($name) . ');');
86+
$setterBody = '$' . $this->snakeCaseToCamelCase($name);
87+
if ($value['type'] == 'json' && !$notNullable) {
88+
$setterBody = 'null === $options ? json_encode($options) : null';
89+
}
90+
if ($value['type'] == 'json' && $notNullable) {
91+
$setterBody = 'json_encode($options)';
92+
}
93+
$setter->setBody('return $this->setData(self::' . strtoupper($name) . ', ' . $setterBody . ');');
8394

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

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/PosterInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface PosterInterface extends IdentityInterface
1313
public const ENTITY_ID = 'entity_id';
1414
public const NAME = 'name';
1515
public const SUBNAME = 'subname';
16+
public const OPTIONS = 'options';
1617
public const FAMILY = 'family';
1718
public const ACTIVE = 'active';
1819
public const PRODUCT_ID = 'product_id';
@@ -54,6 +55,18 @@ public function getSubname(): ?string;
5455
*/
5556
public function setSubname(?string $subname): self;
5657

58+
/**
59+
* Get options
60+
* @return array|null
61+
*/
62+
public function getOptions(): ?array;
63+
64+
/**
65+
* Set options
66+
* @return $this
67+
*/
68+
public function setOptions(?array $options): self;
69+
5770
/**
5871
* Get family
5972
* @return string|null

test/Expectations/Crud/81/v1/Model/Poster.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ public function setSubname(?string $subname): self
6464
return $this->setData(self::SUBNAME, $subname);
6565
}
6666

67+
/**
68+
* Get options
69+
*/
70+
public function getOptions(): ?array
71+
{
72+
return json_decode($this->getData(self::OPTIONS) ?? 'null', true);
73+
}
74+
75+
/**
76+
* Set options
77+
*/
78+
public function setOptions(?array $options): self
79+
{
80+
return $this->setData(self::OPTIONS, null === $options ? json_encode($options) : null);
81+
}
82+
6783
/**
6884
* Get family
6985
*/

test/Expectations/Crud/81/v1/etc/db_schema.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<column xsi:type="int" name="entity_id" nullable="false" identity="false" unsigned="true" comment="Banner ID"/>
7676
<column xsi:type="varchar" name="name" nullable="true" length="255" comment="Name"/>
7777
<column xsi:type="varchar" name="subname" nullable="true" length="255" comment="Subname"/>
78+
<column xsi:type="json" name="options" nullable="true" comment="Options"/>
7879
<column xsi:type="varchar" name="family" nullable="true" length="255" comment="Family"/>
7980
<column xsi:type="boolean" name="active" nullable="false" comment="Active"/>
8081
<column xsi:type="int" name="product_id" nullable="true" identity="false" unsigned="true" comment="Product_id"/>

test/Expectations/Crud/82/v1/Api/Data/PosterInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface PosterInterface extends IdentityInterface
1313
public const ENTITY_ID = 'entity_id';
1414
public const NAME = 'name';
1515
public const SUBNAME = 'subname';
16+
public const OPTIONS = 'options';
1617
public const FAMILY = 'family';
1718
public const ACTIVE = 'active';
1819
public const PRODUCT_ID = 'product_id';
@@ -54,6 +55,18 @@ public function getSubname(): ?string;
5455
*/
5556
public function setSubname(?string $subname): self;
5657

58+
/**
59+
* Get options
60+
* @return array|null
61+
*/
62+
public function getOptions(): ?array;
63+
64+
/**
65+
* Set options
66+
* @return $this
67+
*/
68+
public function setOptions(?array $options): self;
69+
5770
/**
5871
* Get family
5972
* @return string|null

0 commit comments

Comments
 (0)