Skip to content

Commit

Permalink
[php-symfony] Support for default scalar value of properties in model (
Browse files Browse the repository at this point in the history
…#16605)

* Add support for default scalar value of properties in php-symfony models. Default values now returns in getter-functions

* Revert nullable type-hinting. Move default values from getter to property initialize.
Made changes in __construct:
Now value of properties rewrites only if key exist in $data array.
  • Loading branch information
reznikartem authored Oct 1, 2023
1 parent 03781d3 commit 039c169
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public String prependSlashToDataTypeOnlyIfNecessary(String dataType) {
return dataType;
}

return "\\" + dataType;
return "\\" + dataType;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}
parent::__construct($data);

{{/parentSchema}}
{{#vars}}
$this->{{name}} = $data['{{name}}'] ?? null;
{{/vars}}
if (is_array($data)) {
{{#vars}}
$this->{{name}} = array_key_exists('{{name}}', $data) ? $data['{{name}}'] : $this->{{name}};
{{/vars}}
}
}
{{#vars}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@
{{/minItems}}
{{/hasValidation}}
*/
protected {{{vendorExtensions.x-parameter-type}}} ${{name}} = null;
protected {{{vendorExtensions.x-parameter-type}}} ${{name}} = {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}};
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ class ApiResponse
*/
public function __construct(array $data = null)
{
$this->code = $data['code'] ?? null;
$this->type = $data['type'] ?? null;
$this->message = $data['message'] ?? null;
if (is_array($data)) {
$this->code = array_key_exists('code', $data) ? $data['code'] : $this->code;
$this->type = array_key_exists('type', $data) ? $data['type'] : $this->type;
$this->message = array_key_exists('message', $data) ? $data['message'] : $this->message;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ class Category
*/
public function __construct(array $data = null)
{
$this->id = $data['id'] ?? null;
$this->name = $data['name'] ?? null;
if (is_array($data)) {
$this->id = array_key_exists('id', $data) ? $data['id'] : $this->id;
$this->name = array_key_exists('name', $data) ? $data['name'] : $this->name;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,22 @@ class Order
* @Assert\Type("bool")
* @Type("bool")
*/
protected ?bool $complete = null;
protected ?bool $complete = false;

/**
* Constructor
* @param array|null $data Associated array of property values initializing the model
*/
public function __construct(array $data = null)
{
$this->id = $data['id'] ?? null;
$this->petId = $data['petId'] ?? null;
$this->quantity = $data['quantity'] ?? null;
$this->shipDate = $data['shipDate'] ?? null;
$this->status = $data['status'] ?? null;
$this->complete = $data['complete'] ?? null;
if (is_array($data)) {
$this->id = array_key_exists('id', $data) ? $data['id'] : $this->id;
$this->petId = array_key_exists('petId', $data) ? $data['petId'] : $this->petId;
$this->quantity = array_key_exists('quantity', $data) ? $data['quantity'] : $this->quantity;
$this->shipDate = array_key_exists('shipDate', $data) ? $data['shipDate'] : $this->shipDate;
$this->status = array_key_exists('status', $data) ? $data['status'] : $this->status;
$this->complete = array_key_exists('complete', $data) ? $data['complete'] : $this->complete;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ class Pet
*/
public function __construct(array $data = null)
{
$this->id = $data['id'] ?? null;
$this->category = $data['category'] ?? null;
$this->name = $data['name'] ?? null;
$this->photoUrls = $data['photoUrls'] ?? null;
$this->tags = $data['tags'] ?? null;
$this->status = $data['status'] ?? null;
if (is_array($data)) {
$this->id = array_key_exists('id', $data) ? $data['id'] : $this->id;
$this->category = array_key_exists('category', $data) ? $data['category'] : $this->category;
$this->name = array_key_exists('name', $data) ? $data['name'] : $this->name;
$this->photoUrls = array_key_exists('photoUrls', $data) ? $data['photoUrls'] : $this->photoUrls;
$this->tags = array_key_exists('tags', $data) ? $data['tags'] : $this->tags;
$this->status = array_key_exists('status', $data) ? $data['status'] : $this->status;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ class Tag
*/
public function __construct(array $data = null)
{
$this->id = $data['id'] ?? null;
$this->name = $data['name'] ?? null;
if (is_array($data)) {
$this->id = array_key_exists('id', $data) ? $data['id'] : $this->id;
$this->name = array_key_exists('name', $data) ? $data['name'] : $this->name;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@ class User
*/
public function __construct(array $data = null)
{
$this->id = $data['id'] ?? null;
$this->username = $data['username'] ?? null;
$this->firstName = $data['firstName'] ?? null;
$this->lastName = $data['lastName'] ?? null;
$this->email = $data['email'] ?? null;
$this->password = $data['password'] ?? null;
$this->phone = $data['phone'] ?? null;
$this->userStatus = $data['userStatus'] ?? null;
if (is_array($data)) {
$this->id = array_key_exists('id', $data) ? $data['id'] : $this->id;
$this->username = array_key_exists('username', $data) ? $data['username'] : $this->username;
$this->firstName = array_key_exists('firstName', $data) ? $data['firstName'] : $this->firstName;
$this->lastName = array_key_exists('lastName', $data) ? $data['lastName'] : $this->lastName;
$this->email = array_key_exists('email', $data) ? $data['email'] : $this->email;
$this->password = array_key_exists('password', $data) ? $data['password'] : $this->password;
$this->phone = array_key_exists('phone', $data) ? $data['phone'] : $this->phone;
$this->userStatus = array_key_exists('userStatus', $data) ? $data['userStatus'] : $this->userStatus;
}
}

/**
Expand Down

0 comments on commit 039c169

Please sign in to comment.