Set discriminator property in constructor

When using inheritance let the constructor set the discriminator
property to the model name.

This avoids redundant and error prone stuff like:

```
  $animal = new \Swagger\Client\Model\Dog();
  $animal->setClassName('Dog');
```
This commit is contained in:
Arne Jørgensen 2016-03-28 22:39:22 +02:00
parent c159239d1b
commit 65d677c385
2 changed files with 18 additions and 0 deletions

View File

@ -48,6 +48,12 @@ use \ArrayAccess;
*/
class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}implements ArrayAccess
{
/**
* The original name of the model.
* @var string
*/
static $swaggerModelName = '{{name}}';
/**
* Array of property to type mappings. Used for (de)serialization
* @var string[]
@ -115,6 +121,11 @@ class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}implements ArrayA
public function __construct(array $data = null)
{
{{#parent}}parent::__construct($data);{{/parent}}
{{#discriminator}}// Initialize discriminator property with the model name.
$discrimintor = array_search('{{discriminator}}', self::$attributeMap);
$this->{$discrimintor} = static::$swaggerModelName;
{{/discriminator}}
if ($data != null) {
{{#vars}}$this->{{name}} = $data["{{name}}"];{{#hasMore}}
{{/hasMore}}{{/vars}}

View File

@ -402,6 +402,13 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
$this->assertSame('Dog', $new_dog->getClassName());
}
// test if discriminator is initialized automatically
public function testDiscriminatorInitialization()
{
$new_dog = new Swagger\Client\Model\Dog();
$this->assertSame('Dog', $new_dog->getClassName());
}
}
?>