@@ -8,38 +8,33 @@ CodeWriterPHP writer = (CodeWriterPHP) host.CodeWriter;
88OdcmClass entity = host.CurrentType.AsOdcmClass();
99TemplateWriterSettings settings = ConfigurationService.Settings;
1010String entityName = entity.Name.SanitizeEntityName();
11- string targetNamespace = @"Microsoft\Graph\Model";
12- String entityBaseName = "";
13-
14- if (entity.Base != null)
15- entityBaseName = entity.Base.Name.SanitizeEntityName();
16-
17- // TemplateWriterSettings.Properties are set at the Typewriter command line. Check the command line
18- // documentation for more information on how the TemplateWriterSettings.Properties is used.
19- if (settings.Properties.ContainsKey("php.namespace"))
11+ String entityCheckedCase = entity.Name.ToCheckedCase();
12+ String targetNamespace = TypeHelperPHP.GetPHPNamespace(entity, settings);
13+ String entityBaseName = TypeHelperPHP.GetBaseTypeFullName(entity.Base, targetNamespace, settings);
14+ if (entityBaseName.Contains("\\"))
2015{
21- targetNamespace = settings.Properties["php.namespace"] ;
22- }
16+ entityBaseName = "\\" + entityBaseName ;
17+ }
2318
2419#>
2520<#=writer.WriteHeader(writer.GetDocBlock(entityName.ToCheckedCase()))#>
2621namespace <#=targetNamespace#>;
2722
2823<#=writer.GetClassBlock(entityName.ToCheckedCase().ToString(), "Model")#>
2924<#
30- if (entity.Name.ToCheckedCase() == "Entity") {
25+ if (entityCheckedCase == "Entity") {
3126#>
32- class <#=entity.Name.ToCheckedCase() #> implements \JsonSerializable
27+ class <#=entityCheckedCase #> implements \JsonSerializable
3328<#
3429} else {
3530#>
36- class <#=entityName.ToCheckedCase()#> extends <#=entityBaseName.ToCheckedCase() #>
31+ class <#=entityName.ToCheckedCase()#> extends <#=entityBaseName#>
3732<#
3833}
3934#>
4035{
4136<#
42- if (entity.Name.ToCheckedCase() == "Entity") {
37+ if (entityCheckedCase == "Entity") {
4338#>
4439 /**
4540 * The array of properties available
@@ -50,7 +45,7 @@ if (entity.Name.ToCheckedCase() == "Entity") {
5045 protected $_propDict;
5146
5247 /**
53- * Construct a new <#=entity.Name.ToCheckedCase() #>
48+ * Construct a new <#=entityCheckedCase #>
5449 *
5550 * @param array $propDict A list of properties to set
5651 */
@@ -72,7 +67,19 @@ if (entity.Name.ToCheckedCase() == "Entity") {
7267<#
7368}
7469foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString() != "bytes")){
75- String propertyName = property.Name.SanitizePropertyName(entityName).ToCamelize();
70+ String propertyName = property.Name.SanitizePropertyName(entityName).ToCamelize();
71+ var camelCasePropertyName = property.Name.ToCamelize();
72+ var propertyTypeString = property.Type.GetTypeString();
73+ var propertyNamespace = TypeHelperPHP.GetPHPNamespace(property.Type, settings);
74+
75+ // Check whether this type is a generated model type or a PHP type
76+ var fullPropertyTypeName = propertyTypeString[0] == '\\' || propertyTypeString.IsPHPPrimitiveType()
77+ ? propertyTypeString
78+ : string.Join("\\", propertyNamespace, propertyTypeString.SanitizeEntityName().ToCheckedCase());
79+
80+ var propertyTypeReference = propertyNamespace == targetNamespace && !propertyTypeString.IsPHPPrimitiveType()
81+ ? propertyTypeString.SanitizeEntityName().ToCheckedCase()
82+ : fullPropertyTypeName;
7683 if (property.Type.IsComplex()) {
7784 if (property.IsCollection()) {
7885#>
@@ -88,8 +95,8 @@ foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString(
8895 */
8996 public function get<#=propertyName.ToCheckedCase()#>()
9097 {
91- if (array_key_exists("<#=property.Name.ToCamelize() #>", $this->_propDict)) {
92- return $this->_propDict["<#=property.Name.ToCamelize() #>"];
98+ if (array_key_exists("<#=camelCasePropertyName #>", $this->_propDict)) {
99+ return $this->_propDict["<#=camelCasePropertyName #>"];
93100 } else {
94101 return null;
95102 }
@@ -102,13 +109,13 @@ foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString(
102109 * <#=property.GetSanitizedLongDescription()#>
103110<# } #>
104111 *
105- * @param <#=property.Type.GetTypeString().SanitizeEntityName().ToCheckedCase() #> $val The <#=propertyName#>
112+ * @param <#=propertyTypeReference #> $val The <#=propertyName#>
106113 *
107- * @return <#=entity.Name.ToCheckedCase() #>
114+ * @return <#=entityCheckedCase #>
108115 */
109116 public function set<#=propertyName.ToCheckedCase()#>($val)
110117 {
111- $this->_propDict["<#=property.Name.ToCamelize() #>"] = $val;
118+ $this->_propDict["<#=camelCasePropertyName #>"] = $val;
112119 return $this;
113120 }
114121
@@ -122,26 +129,20 @@ foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString(
122129 * <#=property.GetSanitizedLongDescription()#>
123130<# } #>
124131 *
125- * @return <#=property.Type.GetTypeString().SanitizeEntityName().ToCheckedCase() #> The <#=propertyName#>
132+ * @return <#=propertyTypeReference #> The <#=propertyName#>
126133 */
127134 public function get<#=propertyName.ToCheckedCase()#>()
128135 {
129- if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
130- <#
131- // Check whether this type is a generated model type or a PHP type
132- if (property.Type.GetTypeString()[0] == '\\') { #>
133- if (is_a($this->_propDict["<#=property.Name.ToCamelize()#>"], "<#=property.Type.GetTypeString()#>")) {
134- <# } else { #>
135- if (is_a($this->_propDict["<#=property.Name.ToCamelize()#>"], "<#=targetNamespace#>\<#=property.Type.GetTypeString()#>")) {
136- <# } #>
137- return $this->_propDict["<#=property.Name.ToCamelize()#>"];
136+ if (array_key_exists("<#=camelCasePropertyName#>", $this->_propDict)) {
137+ if (is_a($this->_propDict["<#=camelCasePropertyName#>"], "<#=fullPropertyTypeName#>")) {
138+ return $this->_propDict["<#=camelCasePropertyName#>"];
138139 } else {
139- <# if (property.Type.GetTypeString() == "\\GuzzleHttp\\Psr7\\Stream") { #>
140- $this->_propDict["<#=property.Name.ToCamelize() #>"] = \GuzzleHttp\Psr7\stream_for($this->_propDict["<#=property.Name.ToCamelize() #>"]);
141- return $this->_propDict["<#=property.Name.ToCamelize() #>"];
140+ <# if (propertyTypeString == "\\GuzzleHttp\\Psr7\\Stream") { #>
141+ $this->_propDict["<#=camelCasePropertyName #>"] = \GuzzleHttp\Psr7\stream_for($this->_propDict["<#=camelCasePropertyName #>"]);
142+ return $this->_propDict["<#=camelCasePropertyName #>"];
142143<# } else { #>
143- $this->_propDict["<#=property.Name.ToCamelize() #>"] = new <#=property.Type.GetTypeString().SanitizeEntityName().ToCheckedCase() #>($this->_propDict["<#=property.Name.ToCamelize() #>"]);
144- return $this->_propDict["<#=property.Name.ToCamelize() #>"];
144+ $this->_propDict["<#=camelCasePropertyName #>"] = new <#=propertyTypeReference #>($this->_propDict["<#=camelCasePropertyName #>"]);
145+ return $this->_propDict["<#=camelCasePropertyName #>"];
145146<# } #>
146147 }
147148 }
@@ -155,18 +156,18 @@ if (property.Type.GetTypeString()[0] == '\\') { #>
155156 * <#=property.GetSanitizedLongDescription()#>
156157<# } #>
157158 *
158- * @param <#=property.Type.GetTypeString().SanitizeEntityName().ToCheckedCase() #> $val The <#=propertyName#>
159+ * @param <#=propertyTypeReference #> $val The <#=propertyName#>
159160 *
160- * @return <#=entity.Name.ToCheckedCase() #>
161+ * @return <#=entityCheckedCase #>
161162 */
162163 public function set<#=propertyName.ToCheckedCase()#>($val)
163164 {
164- <# if (property.Type.GetTypeString() == "bool") { #>
165- $this->_propDict["<#=property.Name.ToCamelize() #>"] = boolval($val);
166- <# } else if (property.Type.GetTypeString() == "int") { #>
167- $this->_propDict["<#=property.Name.ToCamelize() #>"] = intval($val);
165+ <# if (propertyTypeString == "bool") { #>
166+ $this->_propDict["<#=camelCasePropertyName #>"] = boolval($val);
167+ <# } else if (propertyTypeString == "int") { #>
168+ $this->_propDict["<#=camelCasePropertyName #>"] = intval($val);
168169<# } else { #>
169- $this->_propDict["<#=property.Name.ToCamelize() #>"] = $val;
170+ $this->_propDict["<#=camelCasePropertyName #>"] = $val;
170171<# } #>
171172 return $this;
172173 }
@@ -182,19 +183,19 @@ if (property.Type.GetTypeString()[0] == '\\') { #>
182183 * <#=property.GetSanitizedLongDescription()#>
183184<# } #>
184185 *
185- * @return <#=property.Type.GetTypeString() #> The <#=propertyName#>
186+ * @return <#=propertyTypeReference #> The <#=propertyName#>
186187 */
187188 public function get<#=propertyName.ToCheckedCase()#>()
188189 {
189- if (array_key_exists("<#=property.Name.ToCamelize() #>", $this->_propDict)) {
190+ if (array_key_exists("<#=camelCasePropertyName #>", $this->_propDict)) {
190191<#
191- if (property.Type.GetTypeString() == "\\DateTime") {
192+ if (propertyTypeString == "\\DateTime") {
192193#>
193- return new \DateTime($this->_propDict["<#=property.Name.ToCamelize() #>"]);
194+ return new \DateTime($this->_propDict["<#=camelCasePropertyName #>"]);
194195<#
195196 } else {
196197#>
197- return $this->_propDict["<#=property.Name.ToCamelize() #>"];
198+ return $this->_propDict["<#=camelCasePropertyName #>"];
198199<#
199200 }
200201#>
@@ -210,23 +211,23 @@ if (property.Type.GetTypeString()[0] == '\\') { #>
210211 * <#=property.GetSanitizedLongDescription()#>
211212<# } #>
212213 *
213- * @param <#=property.Type.GetTypeString() #> $val The <#=propertyName#>
214+ * @param <#=propertyTypeReference #> $val The <#=propertyName#>
214215 *
215- * @return <#=entity.Name.ToCheckedCase() #>
216+ * @return <#=entityCheckedCase #>
216217 */
217218 public function set<#=propertyName.ToCheckedCase()#>($val)
218219 {
219220<#
220- if (property.Type.GetTypeString() == "\\DateTime") {
221+ if (propertyTypeString == "\\DateTime") {
221222#>
222- $this->_propDict["<#=property.Name.ToCamelize() #>"]
223+ $this->_propDict["<#=camelCasePropertyName #>"]
223224 = $val->format(\DateTime::ISO8601) . "Z";
224225<#
225226 } else {
226227#>
227- <# if (property.Type.GetTypeString() == "bool") { #>
228+ <# if (propertyTypeString == "bool") { #>
228229 $this->_propDict["<#=property.Name#>"] = boolval($val);
229- <# } else if (property.Type.GetTypeString() == "int") { #>
230+ <# } else if (propertyTypeString == "int") { #>
230231 $this->_propDict["<#=property.Name#>"] = intval($val);
231232<# } else { #>
232233 $this->_propDict["<#=property.Name#>"] = $val;
@@ -240,7 +241,7 @@ if (property.Type.GetTypeString()[0] == '\\') { #>
240241<#
241242 }
242243 }
243- if (entity.Name.ToCheckedCase() == "Entity") {
244+ if (entityCheckedCase == "Entity") {
244245#>
245246 /**
246247 * Gets the ODataType
0 commit comments