Skip to content

Commit 1e56d54

Browse files
authored
Merge branch '3.1.0-develop' into add-templates-desc-to-data-model-and-data-model-interface
2 parents 0612ec3 + 3ef179e commit 1e56d54

File tree

11 files changed

+286
-32
lines changed

11 files changed

+286
-32
lines changed

resources/fileTemplates/internal/Magento Data Model Interface.php.ft

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
#parse("PHP File Header.php")
3+
declare(strict_types=1);
34

45
#if (${NAMESPACE})
56
namespace ${NAMESPACE};
@@ -28,15 +29,20 @@ interface ${NAME}
2829
#set ($propertyLowerCamel = $propertyData.get(4))
2930

3031
/**
31-
* @return $propertyType
32+
* Getter for $propertyUpperCamel.
33+
*
34+
* @return $propertyType|null
3235
*/
33-
public function get$propertyUpperCamel();
36+
public function get$propertyUpperCamel(): ?$propertyType;
3437

3538
/**
36-
* @param $propertyType $$propertyLowerCamel
37-
* @return $this
39+
* Setter for $propertyUpperCamel.
40+
*
41+
* @param $propertyType|null $$propertyLowerCamel
42+
*
43+
* @return void
3844
*/
39-
public function set$propertyUpperCamel($$propertyLowerCamel);
45+
public function set$propertyUpperCamel(?$propertyType $$propertyLowerCamel): void;
4046
#end
4147
#end
4248
}

resources/fileTemplates/internal/Magento Data Model.php.ft

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
#parse("PHP File Header.php")
3+
declare(strict_types=1);
34

45
#if (${NAMESPACE})
56
namespace ${NAMESPACE};
@@ -9,33 +10,75 @@
910
#foreach ($use in $uses.split(","))
1011
use $use;
1112
#end
13+
#if (${HASINTERFACE} == "true")
14+
#set ($hasInterface = true)
15+
#else
16+
#set ($hasInterface = false)
17+
#end
1218

13-
class ${NAME} #if (${EXTENDS})extends ${EXTENDS} #end #if (${IMPLEMENTS})implements ${IMPLEMENTS}#end
19+
class ${NAME} #if (${EXTENDS})extends ${EXTENDS} #end #if (${IMPLEMENTS} && $hasInterface)implements ${IMPLEMENTS}#end
1420
{
21+
#if (${PROPERTIES} && !$hasInterface)
22+
/**
23+
* String constants for property names
24+
*/
25+
#set ($properties = ${PROPERTIES})
26+
#foreach ($property in $properties.split(","))
27+
#set ($propertyData = $property.split(";"))
28+
#set ($propertyUpperSnake = $propertyData.get(0))
29+
#set ($propertyLowerSnake = $propertyData.get(1))
30+
const $propertyUpperSnake = "$propertyLowerSnake";
31+
#end
32+
33+
#end
1534
#if (${PROPERTIES})
1635
#set ($properties = ${PROPERTIES})
1736
#foreach ($property in $properties.split(","))
1837
#set ($propertyData = $property.split(";"))
1938
#set ($propertyUpperSnake = $propertyData.get(0))
39+
#set ($propertyType = $propertyData.get(2))
2040
#set ($propertyUpperCamel = $propertyData.get(3))
2141
#set ($propertyLowerCamel = $propertyData.get(4))
2242
#if(!($foreach.first))
2343

2444
#end
45+
#if ($hasInterface)
2546
/**
2647
* @inheritDoc
2748
*/
28-
public function get$propertyUpperCamel()
49+
#else
50+
/**
51+
* Getter for $propertyUpperCamel.
52+
*
53+
* @return $propertyType|null
54+
*/
55+
#end
56+
public function get$propertyUpperCamel(): ?$propertyType
2957
{
58+
#if($propertyType == 'string')
3059
return $this->getData(self::$propertyUpperSnake);
60+
#{else}
61+
return $this->getData(self::$propertyUpperSnake) === null ? null
62+
: ($propertyType) $this->getData(self::$propertyUpperSnake);
63+
#end
3164
}
3265

66+
#if ($hasInterface)
3367
/**
3468
* @inheritDoc
3569
*/
36-
public function set$propertyUpperCamel($$propertyLowerCamel)
70+
#else
71+
/**
72+
* Setter for $propertyUpperCamel.
73+
*
74+
* @param $propertyType|null $$propertyLowerCamel
75+
*
76+
* @return void
77+
*/
78+
#end
79+
public function set$propertyUpperCamel(?$propertyType $$propertyLowerCamel): void
3780
{
38-
return $this->setData(self::$propertyUpperSnake, $$propertyLowerCamel);
81+
$this->setData(self::$propertyUpperSnake, $$propertyLowerCamel);
3982
}
4083
#end
4184
#end

src/com/magento/idea/magento2plugin/actions/generation/data/DataModelData.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,97 @@ public class DataModelData {
1212
private final String fqn;
1313
private final String interfaceFQN;
1414
private final String properties;
15+
private final boolean withInterface;
1516

1617
/**
1718
* Constructor.
19+
*
20+
* @param namespace String
21+
* @param name String
22+
* @param moduleName String
23+
* @param fqn String
24+
* @param interfaceFQN String
25+
* @param properties String
26+
* @param hasInterface boolean
1827
*/
1928
public DataModelData(
2029
final String namespace,
2130
final String name,
2231
final String moduleName,
2332
final String fqn,
2433
final String interfaceFQN,
25-
final String properties
34+
final String properties,
35+
final boolean hasInterface
2636
) {
2737
this.namespace = namespace;
2838
this.name = name;
2939
this.moduleName = moduleName;
3040
this.fqn = fqn;
3141
this.interfaceFQN = interfaceFQN;
3242
this.properties = properties;
43+
this.withInterface = hasInterface;
3344
}
3445

46+
/**
47+
* Get Namespace.
48+
*
49+
* @return String
50+
*/
3551
public String getNamespace() {
3652
return namespace;
3753
}
3854

55+
/**
56+
* Get Name.
57+
*
58+
* @return String
59+
*/
3960
public String getName() {
4061
return name;
4162
}
4263

64+
/**
65+
* Get module name.
66+
*
67+
* @return String
68+
*/
4369
public String getModuleName() {
4470
return moduleName;
4571
}
4672

73+
/**
74+
* Get FQN.
75+
*
76+
* @return String
77+
*/
4778
public String getFQN() {
4879
return fqn;
4980
}
5081

82+
/**
83+
* Get Interface FQN.
84+
*
85+
* @return String
86+
*/
5187
public String getInterfaceFQN() {
5288
return interfaceFQN;
5389
}
5490

91+
/**
92+
* Get Properties.
93+
*
94+
* @return String
95+
*/
5596
public String getProperties() {
5697
return properties;
5798
}
99+
100+
/**
101+
* Check if model has interface.
102+
*
103+
* @return boolean
104+
*/
105+
public boolean hasInterface() {
106+
return withInterface;
107+
}
58108
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDataModelDialog.form

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.NewDataModelDialog">
3-
<grid id="cbd77" binding="contentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="cbd77" binding="contentPanel" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="10" left="10" bottom="10" right="10"/>
55
<constraints>
6-
<xy x="48" y="54" width="463" height="423"/>
6+
<xy x="48" y="54" width="618" height="423"/>
77
</constraints>
88
<properties>
99
<preferredSize width="500" height="600"/>
@@ -41,7 +41,7 @@
4141
<grid id="ebe15" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4242
<margin top="5" left="5" bottom="5" right="5"/>
4343
<constraints>
44-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
44+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
4545
</constraints>
4646
<properties/>
4747
<border type="empty" title="Properties" title-justification="2" title-position="2"/>
@@ -89,7 +89,7 @@
8989
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
9090
<margin top="0" left="0" bottom="0" right="0"/>
9191
<constraints>
92-
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
92+
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
9393
</constraints>
9494
<properties/>
9595
<border type="none"/>
@@ -127,6 +127,15 @@
127127
</grid>
128128
</children>
129129
</grid>
130+
<component id="530db" class="javax.swing.JCheckBox" binding="createInterface">
131+
<constraints>
132+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
133+
</constraints>
134+
<properties>
135+
<selected value="true"/>
136+
<text value="Create Interface"/>
137+
</properties>
138+
</component>
130139
</children>
131140
</grid>
132141
</form>

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDataModelDialog.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public class NewDataModelDialog extends AbstractDialog {
7777
private JButton buttonCancel;
7878
private JTable propertyTable;
7979
private JButton addProperty;
80+
private JCheckBox createInterface;
8081

8182
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
8283
message = {NotEmptyRule.MESSAGE, MODEL_NAME})
@@ -137,9 +138,12 @@ private void onOK() {
137138
if (validateFormFields()) {
138139
buildNamespaces();
139140
formatProperties();
140-
generateModelInterfaceFile();
141141
generateModelFile();
142-
generatePreference();
142+
143+
if (isInterfaceShouldBeCreated()) {
144+
generateModelInterfaceFile();
145+
generatePreference();
146+
}
143147
this.setVisible(false);
144148
}
145149
}
@@ -206,7 +210,8 @@ private void generateModelFile() {
206210
getModuleName(),
207211
getModelFQN(),
208212
getInterfaceFQN(),
209-
getProperties()
213+
getProperties(),
214+
isInterfaceShouldBeCreated()
210215
)).generate(NewDataModelAction.ACTION_NAME, true);
211216
}
212217

@@ -251,6 +256,10 @@ private void formatProperties() {
251256
}
252257
}
253258

259+
private boolean isInterfaceShouldBeCreated() {
260+
return createInterface.isSelected();
261+
}
262+
254263
private String getModuleName() {
255264
return moduleName;
256265
}

src/com/magento/idea/magento2plugin/actions/generation/generator/DataModelGenerator.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.magento.idea.magento2plugin.magento.files.DataModel;
2222
import com.magento.idea.magento2plugin.util.GetFirstClassOfFile;
2323
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
24-
import java.util.Arrays;
24+
import java.util.LinkedList;
2525
import java.util.List;
2626
import java.util.Properties;
2727
import javax.swing.JOptionPane;
@@ -108,13 +108,17 @@ protected void fillAttributes(final Properties attributes) {
108108
PhpClassGeneratorUtil.getNameFromFqn(modelData.getInterfaceFQN())
109109
);
110110
attributes.setProperty("PROPERTIES", modelData.getProperties());
111+
attributes.setProperty("HASINTERFACE", Boolean.toString(modelData.hasInterface()));
111112
}
112113

113114
private List<String> getUses() {
114-
return Arrays.asList(
115-
DataModel.DATA_OBJECT,
116-
modelData.getInterfaceFQN()
117-
);
115+
final List<String> usesList = new LinkedList<>();
116+
usesList.add(DataModel.DATA_OBJECT);
117+
118+
if (modelData.hasInterface()) {
119+
usesList.add(modelData.getInterfaceFQN());
120+
}
121+
return usesList;
118122
}
119123

120124
private PhpClass createModel(final String actionName) {

testData/actions/generation/generator/DataModelGenerator/generateDataModel/Sample.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace Foo\Bar\Model\Data;
45

@@ -10,16 +11,33 @@ class Sample extends DataObject implements SampleInterface
1011
/**
1112
* @inheritDoc
1213
*/
13-
public function getSampleProperty()
14+
public function getIdProperty(): ?int
15+
{
16+
return $this->getData(self::ID_PROPERTY) === null ? null
17+
: (int)$this->getData(self::ID_PROPERTY);
18+
}
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public function setIdProperty(?int $idProperty): void
24+
{
25+
$this->setData(self::ID_PROPERTY, $idProperty);
26+
}
27+
28+
/**
29+
* @inheritDoc
30+
*/
31+
public function getSampleProperty(): ?string
1432
{
1533
return $this->getData(self::SAMPLE_PROPERTY);
1634
}
1735

1836
/**
1937
* @inheritDoc
2038
*/
21-
public function setSampleProperty($sampleProperty)
39+
public function setSampleProperty(?string $sampleProperty): void
2240
{
23-
return $this->setData(self::SAMPLE_PROPERTY, $sampleProperty);
41+
$this->setData(self::SAMPLE_PROPERTY, $sampleProperty);
2442
}
2543
}

0 commit comments

Comments
 (0)