Skip to content

Commit e8e2089

Browse files
author
Caitlin Bales (MSFT)
committed
Adds PHP model generation
1 parent 331c1dd commit e8e2089

File tree

10 files changed

+583
-2
lines changed

10 files changed

+583
-2
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<# // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. \n See License in the project root for license information. #>
2+
<#@ template debug="true" hostspecific="true" language="C#" #>
3+
<#@ output extension="\\" #>
4+
<#
5+
CustomT4Host host = (CustomT4Host) Host;
6+
OdcmModel model = host.CurrentModel;
7+
CodeWriterPHP writer = (CodeWriterPHP) host.CodeWriter;
8+
OdcmClass complex = (OdcmClass)host.CurrentType;
9+
#>
10+
<#=writer.WriteHeader(writer.GetDocBlock(complex.Name.ToCheckedCase()))#>
11+
namespace Microsoft\Graph\Model;
12+
<#=writer.GetClassBlock(complex.Name.ToCheckedCase().ToString(), "Model")#>
13+
class <#=complex.Name.ToUpperFirstChar()#>
14+
{
15+
private $_propDict;
16+
/**
17+
* <#=complex.Name.ToCheckedCase()#> constructor
18+
*
19+
* @param array $propDict List of properties to set, defaults to an empty array
20+
*
21+
* @return null
22+
*/
23+
public function __construct($propDict=array())
24+
{
25+
$this->_propDict = $propDict;
26+
}
27+
<#
28+
foreach(var property in complex.Properties.Where(prop => prop.Type.GetTypeString() != "bytes")){
29+
var propertyName = property.Name.ToUnderscore();
30+
if (!property.Type.IsComplex()) {
31+
#>
32+
33+
/**
34+
* Gets the <#=property.Name#>
35+
*
36+
* @return <#=property.Type.GetTypeString()#> The <#=property.Name#>
37+
*/
38+
public function get<#=property.Name.ToCheckedCase()#>()
39+
{
40+
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->propDict)) {
41+
<#
42+
if (property.Type.GetTypeString() == "datetime") {
43+
#>
44+
return datetime.strptime($this->_propDict["<#=property.Name.ToCamelize()#>"].replace("Z", ""), "%Y-%m-%dT%H:%M:%S.%f");
45+
<#
46+
} else {
47+
#>
48+
return $this->propDict["<#=property.Name.ToCamelize()#>"];
49+
<#
50+
}
51+
#>
52+
} else {
53+
return null;
54+
}
55+
}
56+
57+
/**
58+
* Sets the <#=property.Name#>
59+
*
60+
* @param <#=property.Type.GetTypeString()#> $val The value of the <#=property.Name#>
61+
*
62+
* @return null
63+
*/
64+
public function set<#=property.Name.ToCheckedCase()#>($val)
65+
{
66+
<#
67+
if (property.Type.GetTypeString() == "datetime") {
68+
#>
69+
$this->_propDict["<#=propertyName#>"] = $val->isoformat()+"Z";
70+
<#
71+
} else {
72+
#>
73+
$this->_propDict["<#=propertyName#>"] = $val;
74+
<#
75+
}
76+
#>
77+
}
78+
<#
79+
} else {
80+
#>
81+
82+
/**
83+
* Gets the <#=property.Name#>
84+
*
85+
* @return <#=property.Type.GetTypeString()#> The <#=property.Name#>
86+
*/
87+
public function get<#=property.Name.ToCheckedCase()#>()
88+
{
89+
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->propDict)) {
90+
if (is_a($this->propDict["<#=property.Name.ToCamelize()#>"], '<#=property.Type.GetTypeString()#>')) {
91+
return $this->propDict["<#=property.Name.ToCamelize()#>"];
92+
} else {
93+
$this->propDict["<#=property.Name.ToCamelize()#>"] = new <#=property.Type.GetTypeString()#>($this->propDict["<#=property.Name.ToCamelize()#>"]);
94+
return $this->propDict["<#=property.Name.ToCamelize()#>"];
95+
}
96+
}
97+
return null;
98+
}
99+
100+
/**
101+
* Sets the <#=property.Name#>
102+
*
103+
* @param <#=property.Type.GetTypeString()#> $val The value to assign to the <#=property.Name#>
104+
*
105+
* @return null
106+
*/
107+
public function set<#=property.Name.ToCheckedCase()#>($val)
108+
{
109+
$this->propDict["<#=property.Name#>"] = $val;
110+
}
111+
<#
112+
}
113+
}
114+
#>
115+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<# // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. #>
2+
<#@ template debug="true" hostspecific="true" language="C#" #>
3+
<#@ output extension="\\" #>
4+
<#
5+
CustomT4Host host = (CustomT4Host) Host;
6+
OdcmModel model = host.CurrentModel;
7+
CodeWriterPHP writer = (CodeWriterPHP) host.CodeWriter;
8+
OdcmClass entity = host.CurrentType.AsOdcmClass();
9+
TemplateWriterSettings settings = ConfigurationService.Settings;
10+
11+
#>
12+
<#=writer.WriteHeader(writer.GetDocBlock(entity.Name.ToCheckedCase()))#>
13+
namespace Microsoft\Graph\Model;
14+
15+
<#=writer.GetClassBlock(entity.Name.ToCheckedCase().ToString(), "Model")#>
16+
class <#=entity.Name.ToCheckedCase()#>
17+
{
18+
private $_propDict;
19+
/**
20+
* Construct a new <#=entity.Name.ToUpperFirstChar()#>
21+
*
22+
* @param array $propDict A list of settings to set
23+
*/
24+
function __construct($propDict = array())
25+
{
26+
$this->_propDict = $propDict;
27+
}
28+
<#
29+
foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString() != "bytes")){
30+
var propertyName = property.Name.ToUnderscore();
31+
var propertyNameCap = property.Name.ToUpperFirstChar();
32+
if (property.Type.IsComplex()) {
33+
if (property.IsCollection()) {
34+
#>
35+
36+
/**
37+
* Gets the <#=property.Name#>
38+
*
39+
* @return <#=propertyNameCap#>CollectionPage The <#=property.Name#>
40+
*/
41+
public function get<#=property.Name.ToCheckedCase()#>()
42+
{
43+
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
44+
return <#=propertyNameCap#>CollectionPage($this->_propDict["<#=property.Name.ToCamelize()#>"]);
45+
} else {
46+
return null;
47+
}
48+
}
49+
50+
<#
51+
} else {
52+
#>
53+
54+
/**
55+
* Gets the <#=property.Name#>
56+
*
57+
* @return <#=property.Type.GetTypeString()#> The <#=property.Name#>
58+
*/
59+
public function get<#=property.Name.ToCheckedCase()#>()
60+
{
61+
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
62+
if (is_a($this->_propDict["<#=property.Name.ToCamelize()#>"], '<#=property.Type.GetTypeString()#>')) {
63+
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
64+
} else {
65+
$this->_propDict["<#=property.Name.ToCamelize()#>"] = new <#=property.Type.GetTypeString()#>($this->_propDict["<#=property.Name.ToCamelize()#>"]);
66+
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
67+
}
68+
}
69+
return null;
70+
}
71+
72+
/**
73+
* Sets the <#=property.Name#>
74+
*
75+
* @param string $val The <#=propertyName#>
76+
*
77+
* @return null
78+
*/
79+
public function set<#=property.Name.ToCheckedCase()#>($val)
80+
{
81+
$this->_propDict["<#=propertyName#>"] = $val;
82+
}
83+
<#
84+
}
85+
} else {
86+
#>
87+
88+
/**
89+
* Gets the <#=property.Name#>
90+
*
91+
* @return <#=property.Type.GetTypeString()#> The <#=property.Name#>
92+
*/
93+
public function get<#=property.Name.ToCheckedCase()#>()
94+
{
95+
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
96+
<#
97+
if (property.Type.GetTypeString() == "datetime") {
98+
#>
99+
return datetime.strptime($this->_propDict["<#=property.Name.ToCamelize()#>"].replace("Z", ""), "%Y-%m-%dT%H:%M:%S.%f");
100+
<#
101+
} else {
102+
#>
103+
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
104+
<#
105+
}
106+
#>
107+
} else {
108+
return null;
109+
}
110+
}
111+
112+
/**
113+
* Sets the <#=property.Name#>
114+
*
115+
* @param string $val The <#=propertyName#>
116+
*
117+
* @return null
118+
*/
119+
public function set<#=property.Name.ToCheckedCase()#>($val)
120+
{
121+
<#
122+
if (property.Type.GetTypeString() == "datetime") {
123+
#>
124+
$this->_propDict["<#=property.Name.ToCamelize()#>"] = $val->isoformat()+"Z";
125+
<#
126+
} else {
127+
#>
128+
$this->_propDict["<#=property.Name.ToCamelize()#>"] = $val;
129+
<#
130+
}
131+
#>
132+
}
133+
<#
134+
}
135+
}
136+
#>
137+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<# // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. #>
2+
<#@ template debug="true" hostspecific="true" language="C#" #>
3+
<#@ output extension="\\" #>
4+
<#
5+
CustomT4Host host = (CustomT4Host) Host;
6+
OdcmModel model = host.CurrentModel;
7+
CodeWriterPHP writer = (CodeWriterPHP) host.CodeWriter;
8+
var enumT = host.CurrentType.AsOdcmEnum();
9+
#>
10+
<#=writer.WriteHeader(writer.GetDocBlock(enumT.Name.ToCheckedCase()))#>
11+
namespace Microsoft\Graph\Model;
12+
13+
<#=writer.GetClassBlock(enumT.Name.ToCheckedCase().ToString(), "Model")#>
14+
class <#=enumT.Name.ToUpperFirstChar()#> extends SplEnum
15+
{
16+
/**
17+
* The Enum <#=enumT.Name.ToUpperFirstChar()#>
18+
*/
19+
<#
20+
int count = 0;
21+
foreach(var value in enumT.Members)
22+
{
23+
count++;
24+
#>
25+
const <#= value.Name.ToUnderscore().ToUpper()#> = <#= count#>;
26+
<#
27+
}
28+
#>
29+
}

Templates/Templates.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
<Reference Include="System.Core" />
3737
</ItemGroup>
3838
<ItemGroup>
39-
<Content Include="*\*.tt" />
39+
<Content Include="Android\BaseModel.template.tt" />
40+
<Content Include="PHP\Model\ComplexType.php.tt" />
4041
<Content Include="*\*\*.tt" />
42+
<Content Include="PHP\Model\EntityType.php.tt" />
43+
<Content Include="PHP\Model\EnumType.php.tt" />
4144
</ItemGroup>
4245
<ItemGroup>
4346
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"TemplateMapping": {
3+
"Shared": [],
4+
"PHP": [
5+
{
6+
"Template": "EntityType",
7+
"SubProcessor": "EntityType",
8+
"Type": "Model",
9+
"Name": "<Class>"
10+
},
11+
{
12+
"Template": "ComplexType",
13+
"SubProcessor": "ComplexType",
14+
"Type": "Model",
15+
"Name": "<Class>"
16+
},
17+
{
18+
"Template": "EnumType",
19+
"SubProcessor": "EnumType",
20+
"Type": "Model",
21+
"Name": "<Class>"
22+
}
23+
]
24+
}
25+
}

src/GraphODataTemplateWriter/.config/TemplateWriterSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"AvailableLanguages": [ "Android", "ObjC", "CSharp", "Python", "JavaScript" ],
2+
"AvailableLanguages": [ "Android", "ObjC", "CSharp", "Python", "JavaScript", "PHP" ],
33
"TargetLanguage": "Android",
44
"Plugins": [ ],
55
"PrimaryNamespaceName": "com",

0 commit comments

Comments
 (0)