-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Resources Images & Image & the Server Method createImage
- Loading branch information
1 parent
41cfe0d
commit e9ab838
Showing
3 changed files
with
249 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: lukaskammerling | ||
* Date: 28.01.18 | ||
* Time: 21:01 | ||
*/ | ||
|
||
namespace LKDev\HetznerCloud\Models\Images; | ||
|
||
use LKDev\HetznerCloud\HetznerAPIClient; | ||
use LKDev\HetznerCloud\Models\Model; | ||
|
||
class Images extends Model | ||
{ /** | ||
* @var array | ||
*/ | ||
public $images; | ||
|
||
/** | ||
* Returns all image objects. | ||
* | ||
* @see https://docs.hetzner.cloud/#resources-images-get | ||
* @return array | ||
* @throws \LKDev\HetznerCloud\APIException | ||
*/ | ||
public function all(): array | ||
{ | ||
$response = $this->httpClient->get('images'); | ||
if (! HetznerAPIClient::hasError($response)) { | ||
return self::parse(json_decode((string) $response->getBody()))->images; | ||
} | ||
} | ||
|
||
/** | ||
* Returns a specific image object. | ||
* | ||
* @see https://docs.hetzner.cloud/#resources-images-get-1 | ||
* @param int $imageId | ||
* @return \LKDev\HetznerCloud\Models\Images\Image | ||
* @throws \LKDev\HetznerCloud\APIException | ||
*/ | ||
public function get(int $imageId): Image | ||
{ | ||
$response = $this->httpClient->get('images/'.$imageId); | ||
if (! HetznerAPIClient::hasError($response)) { | ||
return Image::parse(json_decode((string) $response->getBody())->image); | ||
} | ||
} | ||
|
||
/** | ||
* @param object $input | ||
* @return $this | ||
*/ | ||
public function setAdditionalData(object $input) | ||
{ | ||
$this->images = collect($input->images)->map(function ($image, $key) { | ||
return Image::parse($image); | ||
})->toArray(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param object $input | ||
* @return $this|static | ||
*/ | ||
public static function parse(object $input) | ||
{ | ||
return (new self())->setAdditionalData($input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters