Skip to content

Commit

Permalink
Merge branch '3.1.0-develop' into add-templates-desc-to-data-model-an…
Browse files Browse the repository at this point in the history
…d-data-model-interface
  • Loading branch information
coderimus authored Jan 25, 2021
2 parents 0612ec3 + 3ef179e commit 1e56d54
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
#parse("PHP File Header.php")
declare(strict_types=1);

#if (${NAMESPACE})
namespace ${NAMESPACE};
Expand Down Expand Up @@ -28,15 +29,20 @@ interface ${NAME}
#set ($propertyLowerCamel = $propertyData.get(4))

/**
* @return $propertyType
* Getter for $propertyUpperCamel.
*
* @return $propertyType|null
*/
public function get$propertyUpperCamel();
public function get$propertyUpperCamel(): ?$propertyType;

/**
* @param $propertyType $$propertyLowerCamel
* @return $this
* Setter for $propertyUpperCamel.
*
* @param $propertyType|null $$propertyLowerCamel
*
* @return void
*/
public function set$propertyUpperCamel($$propertyLowerCamel);
public function set$propertyUpperCamel(?$propertyType $$propertyLowerCamel): void;
#end
#end
}
51 changes: 47 additions & 4 deletions resources/fileTemplates/internal/Magento Data Model.php.ft
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
#parse("PHP File Header.php")
declare(strict_types=1);

#if (${NAMESPACE})
namespace ${NAMESPACE};
Expand All @@ -9,33 +10,75 @@
#foreach ($use in $uses.split(","))
use $use;
#end
#if (${HASINTERFACE} == "true")
#set ($hasInterface = true)
#else
#set ($hasInterface = false)
#end

class ${NAME} #if (${EXTENDS})extends ${EXTENDS} #end #if (${IMPLEMENTS})implements ${IMPLEMENTS}#end
class ${NAME} #if (${EXTENDS})extends ${EXTENDS} #end #if (${IMPLEMENTS} && $hasInterface)implements ${IMPLEMENTS}#end
{
#if (${PROPERTIES} && !$hasInterface)
/**
* String constants for property names
*/
#set ($properties = ${PROPERTIES})
#foreach ($property in $properties.split(","))
#set ($propertyData = $property.split(";"))
#set ($propertyUpperSnake = $propertyData.get(0))
#set ($propertyLowerSnake = $propertyData.get(1))
const $propertyUpperSnake = "$propertyLowerSnake";
#end

#end
#if (${PROPERTIES})
#set ($properties = ${PROPERTIES})
#foreach ($property in $properties.split(","))
#set ($propertyData = $property.split(";"))
#set ($propertyUpperSnake = $propertyData.get(0))
#set ($propertyType = $propertyData.get(2))
#set ($propertyUpperCamel = $propertyData.get(3))
#set ($propertyLowerCamel = $propertyData.get(4))
#if(!($foreach.first))

#end
#if ($hasInterface)
/**
* @inheritDoc
*/
public function get$propertyUpperCamel()
#else
/**
* Getter for $propertyUpperCamel.
*
* @return $propertyType|null
*/
#end
public function get$propertyUpperCamel(): ?$propertyType
{
#if($propertyType == 'string')
return $this->getData(self::$propertyUpperSnake);
#{else}
return $this->getData(self::$propertyUpperSnake) === null ? null
: ($propertyType) $this->getData(self::$propertyUpperSnake);
#end
}

#if ($hasInterface)
/**
* @inheritDoc
*/
public function set$propertyUpperCamel($$propertyLowerCamel)
#else
/**
* Setter for $propertyUpperCamel.
*
* @param $propertyType|null $$propertyLowerCamel
*
* @return void
*/
#end
public function set$propertyUpperCamel(?$propertyType $$propertyLowerCamel): void
{
return $this->setData(self::$propertyUpperSnake, $$propertyLowerCamel);
$this->setData(self::$propertyUpperSnake, $$propertyLowerCamel);
}
#end
#end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,97 @@ public class DataModelData {
private final String fqn;
private final String interfaceFQN;
private final String properties;
private final boolean withInterface;

/**
* Constructor.
*
* @param namespace String
* @param name String
* @param moduleName String
* @param fqn String
* @param interfaceFQN String
* @param properties String
* @param hasInterface boolean
*/
public DataModelData(
final String namespace,
final String name,
final String moduleName,
final String fqn,
final String interfaceFQN,
final String properties
final String properties,
final boolean hasInterface
) {
this.namespace = namespace;
this.name = name;
this.moduleName = moduleName;
this.fqn = fqn;
this.interfaceFQN = interfaceFQN;
this.properties = properties;
this.withInterface = hasInterface;
}

/**
* Get Namespace.
*
* @return String
*/
public String getNamespace() {
return namespace;
}

/**
* Get Name.
*
* @return String
*/
public String getName() {
return name;
}

/**
* Get module name.
*
* @return String
*/
public String getModuleName() {
return moduleName;
}

/**
* Get FQN.
*
* @return String
*/
public String getFQN() {
return fqn;
}

/**
* Get Interface FQN.
*
* @return String
*/
public String getInterfaceFQN() {
return interfaceFQN;
}

/**
* Get Properties.
*
* @return String
*/
public String getProperties() {
return properties;
}

/**
* Check if model has interface.
*
* @return boolean
*/
public boolean hasInterface() {
return withInterface;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.NewDataModelDialog">
<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">
<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">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="463" height="423"/>
<xy x="48" y="54" width="618" height="423"/>
</constraints>
<properties>
<preferredSize width="500" height="600"/>
Expand Down Expand Up @@ -41,7 +41,7 @@
<grid id="ebe15" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="5" left="5" bottom="5" right="5"/>
<constraints>
<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"/>
<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"/>
</constraints>
<properties/>
<border type="empty" title="Properties" title-justification="2" title-position="2"/>
Expand Down Expand Up @@ -89,7 +89,7 @@
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<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"/>
<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"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -127,6 +127,15 @@
</grid>
</children>
</grid>
<component id="530db" class="javax.swing.JCheckBox" binding="createInterface">
<constraints>
<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"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Create Interface"/>
</properties>
</component>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class NewDataModelDialog extends AbstractDialog {
private JButton buttonCancel;
private JTable propertyTable;
private JButton addProperty;
private JCheckBox createInterface;

@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, MODEL_NAME})
Expand Down Expand Up @@ -137,9 +138,12 @@ private void onOK() {
if (validateFormFields()) {
buildNamespaces();
formatProperties();
generateModelInterfaceFile();
generateModelFile();
generatePreference();

if (isInterfaceShouldBeCreated()) {
generateModelInterfaceFile();
generatePreference();
}
this.setVisible(false);
}
}
Expand Down Expand Up @@ -206,7 +210,8 @@ private void generateModelFile() {
getModuleName(),
getModelFQN(),
getInterfaceFQN(),
getProperties()
getProperties(),
isInterfaceShouldBeCreated()
)).generate(NewDataModelAction.ACTION_NAME, true);
}

Expand Down Expand Up @@ -251,6 +256,10 @@ private void formatProperties() {
}
}

private boolean isInterfaceShouldBeCreated() {
return createInterface.isSelected();
}

private String getModuleName() {
return moduleName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.magento.idea.magento2plugin.magento.files.DataModel;
import com.magento.idea.magento2plugin.util.GetFirstClassOfFile;
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import javax.swing.JOptionPane;
Expand Down Expand Up @@ -108,13 +108,17 @@ protected void fillAttributes(final Properties attributes) {
PhpClassGeneratorUtil.getNameFromFqn(modelData.getInterfaceFQN())
);
attributes.setProperty("PROPERTIES", modelData.getProperties());
attributes.setProperty("HASINTERFACE", Boolean.toString(modelData.hasInterface()));
}

private List<String> getUses() {
return Arrays.asList(
DataModel.DATA_OBJECT,
modelData.getInterfaceFQN()
);
final List<String> usesList = new LinkedList<>();
usesList.add(DataModel.DATA_OBJECT);

if (modelData.hasInterface()) {
usesList.add(modelData.getInterfaceFQN());
}
return usesList;
}

private PhpClass createModel(final String actionName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Foo\Bar\Model\Data;

Expand All @@ -10,16 +11,33 @@ class Sample extends DataObject implements SampleInterface
/**
* @inheritDoc
*/
public function getSampleProperty()
public function getIdProperty(): ?int
{
return $this->getData(self::ID_PROPERTY) === null ? null
: (int)$this->getData(self::ID_PROPERTY);
}

/**
* @inheritDoc
*/
public function setIdProperty(?int $idProperty): void
{
$this->setData(self::ID_PROPERTY, $idProperty);
}

/**
* @inheritDoc
*/
public function getSampleProperty(): ?string
{
return $this->getData(self::SAMPLE_PROPERTY);
}

/**
* @inheritDoc
*/
public function setSampleProperty($sampleProperty)
public function setSampleProperty(?string $sampleProperty): void
{
return $this->setData(self::SAMPLE_PROPERTY, $sampleProperty);
$this->setData(self::SAMPLE_PROPERTY, $sampleProperty);
}
}
Loading

0 comments on commit 1e56d54

Please sign in to comment.