Skip to content

Commit 0dc2d1a

Browse files
author
Caitlin Bales (MSFT)
authored
Merge pull request #76 from microsoftgraph/caitbal/working
PHP models generator
2 parents dda8645 + a5e4c6e commit 0dc2d1a

File tree

11 files changed

+686
-4
lines changed

11 files changed

+686
-4
lines changed

GraphODataTemplateWriter.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25123.0
4+
VisualStudioVersion = 14.0.25420.1
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{6ACF6CEE-A594-4DFE-9286-C7154E91738B}"
77
ProjectSection(SolutionItems) = preProject
@@ -30,4 +30,4 @@ Global
3030
GlobalSection(SolutionProperties) = preSolution
3131
HideSolutionNode = FALSE
3232
EndGlobalSection
33-
EndGlobal
33+
EndGlobal
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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.ToCheckedCase()#> extends Entity
14+
{
15+
/**
16+
* The array of properties available
17+
* to the model
18+
*
19+
* @var array(string => string)
20+
*/
21+
private $_propDict;
22+
/**
23+
* <#=complex.Name.ToCheckedCase()#> constructor
24+
*
25+
* @param array $propDict List of properties to set
26+
* Defaults to an empty array
27+
*
28+
* @return <#=complex.Name.ToCheckedCase()#>
29+
*/
30+
public function __construct($propDict=array())
31+
{
32+
parent::__construct();
33+
$this->_propDict = $propDict;
34+
return $this;
35+
}
36+
37+
/**
38+
* Gets the property dictionary of the <#=complex.Name.ToCheckedCase()#>
39+
*
40+
* @return array The list of properties
41+
*/
42+
public function getProperties()
43+
{
44+
return $this->_propDict;
45+
}
46+
<#
47+
foreach(var property in complex.Properties.Where(prop => prop.Type.GetTypeString() != "bytes")){
48+
var propertyName = property.Name.ToUnderscore();
49+
if (!property.Type.IsComplex()) {
50+
#>
51+
52+
/**
53+
* Gets the <#=property.Name#>
54+
*
55+
* @return <#=property.Type.GetTypeString()#> The <#=property.Name#>
56+
*/
57+
public function get<#=property.Name.ToCheckedCase()#>()
58+
{
59+
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
60+
<#
61+
if (property.Type.GetTypeString() == "\\DateTime") {
62+
#>
63+
return new \DateTime($this->_propDict["<#=property.Name.ToCamelize()#>"]);
64+
<#
65+
} else {
66+
#>
67+
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
68+
<#
69+
}
70+
#>
71+
} else {
72+
return null;
73+
}
74+
}
75+
76+
/**
77+
* Sets the <#=property.Name#>
78+
*
79+
* @param <#=property.Type.GetTypeString()#> $val The value of the <#=property.Name#>
80+
*
81+
* @return <#=complex.Name.ToCheckedCase()#>
82+
*/
83+
public function set<#=property.Name.ToCheckedCase()#>($val)
84+
{
85+
<#
86+
if (property.Type.GetTypeString() == "\\DateTime") {
87+
#>
88+
$this->_propDict["<#=propertyName#>"]
89+
= $val->format(\DateTime::ISO8601) . "Z";
90+
<#
91+
} else {
92+
#>
93+
$this->_propDict["<#=propertyName#>"] = $val;
94+
<#
95+
}
96+
#>
97+
return $this;
98+
}
99+
<#
100+
} else {
101+
#>
102+
103+
/**
104+
* Gets the <#=property.Name#>
105+
*
106+
* @return <#=property.Type.GetTypeString()#> The <#=property.Name#>
107+
*/
108+
public function get<#=property.Name.ToCheckedCase()#>()
109+
{
110+
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
111+
if (is_a($this->_propDict["<#=property.Name.ToCamelize()#>"], "<#=property.Type.GetTypeString()#>")) {
112+
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
113+
} else {
114+
$this->_propDict["<#=property.Name.ToCamelize()#>"] = new <#=property.Type.GetTypeString()#>($this->_propDict["<#=property.Name.ToCamelize()#>"]);
115+
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
116+
}
117+
}
118+
return null;
119+
}
120+
121+
/**
122+
* Sets the <#=property.Name#>
123+
*
124+
* @param <#=property.Type.GetTypeString()#> $val The value to assign to the <#=property.Name#>
125+
*
126+
* @return <#=complex.Name.ToCheckedCase()#> The <#=complex.Name.ToUpperFirstChar()#>
127+
*/
128+
public function set<#=property.Name.ToCheckedCase()#>($val)
129+
{
130+
<# if (property.Type.GetTypeString() == "bool") { #>
131+
$this->_propDict["<#=property.Name#>"] = boolval($val);
132+
<# } else if (property.Type.GetTypeString() == "int") { #>
133+
$this->_propDict["<#=property.Name#>"] = intval($val);
134+
<# } else { #>
135+
$this->_propDict["<#=property.Name#>"] = $val;
136+
<# } #>
137+
return $this;
138+
}
139+
<#
140+
}
141+
}
142+
#>
143+
}
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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+
<#
17+
if (entity.Name.ToCheckedCase() == "Entity") {
18+
#>
19+
class <#=entity.Name.ToCheckedCase()#>
20+
<#
21+
} else {
22+
#>
23+
class <#=entity.Name.ToCheckedCase()#> extends Entity
24+
<#
25+
}
26+
#>
27+
{
28+
/**
29+
* The array of properties available
30+
* to the model
31+
*
32+
* @var array(string => string)
33+
*/
34+
private $_propDict;
35+
36+
/**
37+
* Construct a new <#=entity.Name.ToCheckedCase()#>
38+
*
39+
* @param array $propDict A list of properties to set
40+
*
41+
* @return <#=entity.Name.ToCheckedCase()#>
42+
*/
43+
function __construct($propDict = array())
44+
{
45+
<#
46+
if (!(entity.Name.ToCheckedCase() == "Entity")) {
47+
#>
48+
parent::__construct();
49+
<#
50+
}
51+
#>
52+
$this->_propDict = $propDict;
53+
return $this;
54+
}
55+
56+
/**
57+
* Gets the property dictionary of the <#=entity.Name.ToUpperFirstChar()#>
58+
*
59+
* @return array The list of properties
60+
*/
61+
public function getProperties()
62+
{
63+
return $this->_propDict;
64+
}
65+
<#
66+
foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString() != "bytes")){
67+
var propertyName = property.Name.ToUnderscore();
68+
var propertyNameCap = property.Name.ToUpperFirstChar();
69+
if (property.Type.IsComplex()) {
70+
if (property.IsCollection()) {
71+
#>
72+
73+
/**
74+
* Gets the <#=property.Name#>
75+
*
76+
* @return array The <#=property.Name#>
77+
*/
78+
public function get<#=property.Name.ToCheckedCase()#>()
79+
{
80+
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
81+
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
82+
} else {
83+
return null;
84+
}
85+
}
86+
87+
/**
88+
* Sets the <#=property.Name#>
89+
*
90+
* @param string $val The <#=property.Name#>
91+
*
92+
* @return <#=entity.Name.ToCheckedCase()#>
93+
*/
94+
public function set<#=property.Name.ToCheckedCase()#>($val)
95+
{
96+
$this->_propDict["<#=property.Name.ToCamelize()#>"] = $val;
97+
return $this;
98+
}
99+
100+
<#
101+
} else {
102+
#>
103+
104+
/**
105+
* Gets the <#=property.Name#>
106+
*
107+
* @return <#=property.Type.GetTypeString()#> The <#=property.Name#>
108+
*/
109+
public function get<#=property.Name.ToCheckedCase()#>()
110+
{
111+
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
112+
if (is_a($this->_propDict["<#=property.Name.ToCamelize()#>"], "<#=property.Type.GetTypeString()#>")) {
113+
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
114+
} else {
115+
$this->_propDict["<#=property.Name.ToCamelize()#>"] = new <#=property.Type.GetTypeString()#>($this->_propDict["<#=property.Name.ToCamelize()#>"]);
116+
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
117+
}
118+
}
119+
return null;
120+
}
121+
122+
/**
123+
* Sets the <#=property.Name#>
124+
*
125+
* @param string $val The <#=property.Name#>
126+
*
127+
* @return <#=entity.Name.ToCheckedCase()#>
128+
*/
129+
public function set<#=property.Name.ToCheckedCase()#>($val)
130+
{
131+
<# if (property.Type.GetTypeString() == "bool") { #>
132+
$this->_propDict["<#=property.Name#>"] = boolval($val);
133+
<# } else if (property.Type.GetTypeString() == "int") { #>
134+
$this->_propDict["<#=property.Name#>"] = intval($val);
135+
<# } else { #>
136+
$this->_propDict["<#=property.Name#>"] = $val;
137+
<# } #>
138+
return $this;
139+
}
140+
<#
141+
}
142+
} else {
143+
#>
144+
145+
/**
146+
* Gets the <#=property.Name#>
147+
*
148+
* @return <#=property.Type.GetTypeString()#> The <#=property.Name#>
149+
*/
150+
public function get<#=property.Name.ToCheckedCase()#>()
151+
{
152+
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
153+
<#
154+
if (property.Type.GetTypeString() == "\\DateTime") {
155+
#>
156+
return new \DateTime($this->_propDict["<#=property.Name.ToCamelize()#>"]);
157+
<#
158+
} else {
159+
#>
160+
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
161+
<#
162+
}
163+
#>
164+
} else {
165+
return null;
166+
}
167+
}
168+
169+
/**
170+
* Sets the <#=property.Name#>
171+
*
172+
* @param <#=property.Type.GetTypeString()#> $val The <#=property.Name#>
173+
*
174+
* @return <#=entity.Name.ToCheckedCase()#>
175+
*/
176+
public function set<#=property.Name.ToCheckedCase()#>($val)
177+
{
178+
<#
179+
if (property.Type.GetTypeString() == "\\DateTime") {
180+
#>
181+
$this->_propDict["<#=property.Name.ToCamelize()#>"]
182+
= $val->format(\DateTime::ISO8601) . "Z";
183+
<#
184+
} else {
185+
#>
186+
<# if (property.Type.GetTypeString() == "bool") { #>
187+
$this->_propDict["<#=property.Name#>"] = boolval($val);
188+
<# } else if (property.Type.GetTypeString() == "int") { #>
189+
$this->_propDict["<#=property.Name#>"] = intval($val);
190+
<# } else { #>
191+
$this->_propDict["<#=property.Name#>"] = $val;
192+
<# } #>
193+
<#
194+
}
195+
#>
196+
return $this;
197+
}
198+
<#
199+
}
200+
}
201+
#>
202+
}

0 commit comments

Comments
 (0)