[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`).
This commit is contained in:
Arne Jørgensen 2016-04-24 23:20:39 +02:00
parent bbe12c1658
commit 9f40a82310

View File

@ -106,13 +106,17 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
return {{#parentSchema}}parent::getters() + {{/parentSchema}}self::$getters; return {{#parentSchema}}parent::getters() + {{/parentSchema}}self::$getters;
} }
{{#vars}}
/** /**
* ${{name}} {{#description}}{{{description}}}{{/description}} * Associative array for storing property values
* @var {{datatype}} * @var mixed[]
*/ */
protected ${{name}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}; protected $container = array({{#vars}}
{{/vars}} /**
* $container['{{{name}}}']{{#description}} {{{description}}}{{/description}}
* @var {{datatype}}
*/
'{{{name}}}' => {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}},
{{/vars}});
/** /**
* Constructor * Constructor
@ -123,11 +127,11 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
{{#parentSchema}}parent::__construct($data);{{/parentSchema}} {{#parentSchema}}parent::__construct($data);{{/parentSchema}}
{{#discriminator}}// Initialize discriminator property with the model name. {{#discriminator}}// Initialize discriminator property with the model name.
$discrimintor = array_search('{{discriminator}}', self::$attributeMap); $discrimintor = array_search('{{discriminator}}', self::$attributeMap);
$this->{$discrimintor} = static::$swaggerModelName; $this->container[$discrimintor] = static::$swaggerModelName;
{{/discriminator}} {{/discriminator}}
if ($data != null) { if ($data != null) {
{{#vars}}$this->{{name}} = $data["{{name}}"];{{#hasMore}} {{#vars}}$this->container['{{name}}'] = $data['{{name}}'];{{#hasMore}}
{{/hasMore}}{{/vars}} {{/hasMore}}{{/vars}}
} }
} }
@ -138,7 +142,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
*/ */
public function {{getter}}() 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}}) 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)) { if (!in_array(${{{name}}}, $allowed_values)) {
throw new \InvalidArgumentException("Invalid value for '{{name}}', must be one of {{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}"); throw new \InvalidArgumentException("Invalid value for '{{name}}', must be one of {{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}");
}{{/isEnum}} }{{/isEnum}}
$this->{{name}} = ${{name}}; $this->container['{{name}}'] = ${{name}};
return $this; return $this;
} }
{{/vars}} {{/vars}}
@ -163,7 +167,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
*/ */
public function offsetExists($offset) 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) 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) 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) public function offsetUnset($offset)
{ {
unset($this->$offset); unset($this->container[$offset]);
} }
/** /**