LKDevelopment / hetzner-cloud-php-sdk

A PHP SDK for the Hetzner Cloud API
MIT License
104 stars 51 forks source link

Labels of images won't be set correctly #26

Closed invokablegmbh closed 4 years ago

invokablegmbh commented 4 years ago

Hi there,

when using "images()", the labels of each image will not be populated.

In Models/Images/Image.php:231 you do not call the constructor with the last param:

return new self($input->id, $input->type, (property_exists($input, 'status') ? $input->status : null), $input->name, $input->description, $input->image_size, $input->disk_size, $input->created, $input->created_from, $input->bound_to, $input->os_flavor, $input->os_version, $input->rapid_deploy, Protection::parse($input->protection));

As you can see from the constructor, the fifteenth param ($labels) is missing:

public function __construct( int $id, string $type = null, string $status = null, string $name = null, string $description = null, float $imageSize = null, int $diskSize = null, string $created = null, $createdFrom = null, int $boundTo = null, string $osFlavor = null, string $osVersion = null, bool $rapidDeploy = null, Protection $protection = null, array $labels = [] )

As you are parsing the "input" variable from the json as an object and the constructor wants to have a plain array for the labels, you will have to somehow transform the "$input->labels" from stdclass to array. I did a quick hotfix for this (not nice, but works. :) ):

return new self($input->id, $input->type, (property_exists($input, 'status') ? $input->status : null), $input->name, $input->description, $input->image_size, $input->disk_size, $input->created, $input->created_from, $input->bound_to, $input->os_flavor, $input->os_version, $input->rapid_deploy, Protection::parse($input->protection), json_decode(json_encode($input->labels),1));

Would you add a fix in your repo?

And: Thanks a lot for your work on this module!

LKaemmerling commented 4 years ago

Thank you! I have released a new version 1.8.1 with a cleaner fix for this :)

FYI: You can use get_object_vars for easily transforming an object (the properties) into an assoc array.