From 9f40a82310b36764e5d8ee055983c888ac4d88a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20J=C3=B8rgensen?= Date: Sun, 24 Apr 2016 23:20:39 +0200 Subject: [PATCH] [PHP] Fix ArrayAccess interface implmentation in models The models didn't implement a generally working ArrayAccess interface. This would fail on list container types (array). This commit refactors some internals of the model object. The model properties are no longer stored as separate properties on the PHP object but as entries in a `$container` property. This is needed to make the model work also for list containers. Besides it avoids potential problems where the model would specify property names that could collide with names used by the Swagger model implementation itself (i.e. `$attributeMap`). --- .../src/main/resources/php/model.mustache | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/model.mustache b/modules/swagger-codegen/src/main/resources/php/model.mustache index 0a826f54272..47ff89a57ee 100644 --- a/modules/swagger-codegen/src/main/resources/php/model.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model.mustache @@ -106,13 +106,17 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple return {{#parentSchema}}parent::getters() + {{/parentSchema}}self::$getters; } - {{#vars}} /** - * ${{name}} {{#description}}{{{description}}}{{/description}} - * @var {{datatype}} - */ - protected ${{name}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}; - {{/vars}} + * Associative array for storing property values + * @var mixed[] + */ + protected $container = array({{#vars}} + /** + * $container['{{{name}}}']{{#description}} {{{description}}}{{/description}} + * @var {{datatype}} + */ + '{{{name}}}' => {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}, + {{/vars}}); /** * Constructor @@ -123,11 +127,11 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple {{#parentSchema}}parent::__construct($data);{{/parentSchema}} {{#discriminator}}// Initialize discriminator property with the model name. $discrimintor = array_search('{{discriminator}}', self::$attributeMap); - $this->{$discrimintor} = static::$swaggerModelName; + $this->container[$discrimintor] = static::$swaggerModelName; {{/discriminator}} if ($data != null) { - {{#vars}}$this->{{name}} = $data["{{name}}"];{{#hasMore}} + {{#vars}}$this->container['{{name}}'] = $data['{{name}}'];{{#hasMore}} {{/hasMore}}{{/vars}} } } @@ -138,7 +142,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple */ public function {{getter}}() { - return $this->{{name}}; + return $this->container['{{name}}']; } /** @@ -148,11 +152,11 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple */ public function {{setter}}(${{name}}) { - {{#isEnum}}$allowed_values = array({{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}); + {{#isEnum}}$allowed_values = array({{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}); if (!in_array(${{{name}}}, $allowed_values)) { throw new \InvalidArgumentException("Invalid value for '{{name}}', must be one of {{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}"); }{{/isEnum}} - $this->{{name}} = ${{name}}; + $this->container['{{name}}'] = ${{name}}; return $this; } {{/vars}} @@ -163,7 +167,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple */ public function offsetExists($offset) { - return isset($this->$offset); + return isset($this->container[$offset]); } /** @@ -173,7 +177,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple */ public function offsetGet($offset) { - return $this->$offset; + return isset($this->container[$offset]) ? $this->container[$offset] : null; } /** @@ -184,7 +188,11 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple */ public function offsetSet($offset, $value) { - $this->$offset = $value; + if (is_null($offset)) { + $this->container[] = $value; + } else { + $this->container[$offset] = $value; + } } /** @@ -194,7 +202,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple */ public function offsetUnset($offset) { - unset($this->$offset); + unset($this->container[$offset]); } /**