Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGE LOG

* Add PHP 8.5 support
* Fixed the `Droplet::create` SSH keys parameter type documentation
* Fixed hydration of object-backed App Platform and project resource fields


## 5.0.5 (03/05/2025)
Expand Down
29 changes: 29 additions & 0 deletions src/Entity/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,33 @@ final class App extends AbstractEntity
public string $liveDomain;

public array $domains;

public function build(array $parameters): void
{
foreach ($parameters as $property => $value) {
if (
\in_array(static::convertToCamelCase($property), ['spec', 'activeDeployment', 'inProgressDeployment', 'region', 'domains'], true) &&
($value instanceof \stdClass || \is_array($value))
) {
$parameters[$property] = self::normalizeArray($value);
}
}

parent::build($parameters);
}

private static function normalizeArray(array|\stdClass $value): array
{
if ($value instanceof \stdClass) {
$value = \get_object_vars($value);
}

foreach ($value as $key => $subValue) {
if ($subValue instanceof \stdClass || \is_array($subValue)) {
$value[$key] = self::normalizeArray($subValue);
}
}

return $value;
}
}
29 changes: 29 additions & 0 deletions src/Entity/AppDeployment.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,33 @@ final class AppDeployment extends AbstractEntity
public string $phase;

public string $tierSlug;

public function build(array $parameters): void
{
foreach ($parameters as $property => $value) {
if (
\in_array(static::convertToCamelCase($property), ['spec', 'services', 'staticSites', 'workers', 'jobs', 'progress'], true) &&
($value instanceof \stdClass || \is_array($value))
) {
$parameters[$property] = self::normalizeArray($value);
}
}

parent::build($parameters);
}

private static function normalizeArray(array|\stdClass $value): array
{
if ($value instanceof \stdClass) {
$value = \get_object_vars($value);
}

foreach ($value as $key => $subValue) {
if ($subValue instanceof \stdClass || \is_array($subValue)) {
$value[$key] = self::normalizeArray($subValue);
}
}

return $value;
}
}
11 changes: 11 additions & 0 deletions src/Entity/ProjectResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ final class ProjectResource extends AbstractEntity
public array $links;

public string $status;

public function build(array $parameters): void
{
foreach ($parameters as $property => $value) {
if ('links' === static::convertToCamelCase($property) && $value instanceof \stdClass) {
$parameters[$property] = \get_object_vars($value);
}
}

parent::build($parameters);
}
}
165 changes: 165 additions & 0 deletions tests/Entity/AppDeploymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testConstructor(): void
'staticSites' => [],
'workers' => [],
'jobs' => [],
'phaseLastUpdatedAt' => '2021-02-10T17:05:30Z',
'createdAt' => '2021-02-10T17:05:30Z',
'updatedAt' => '2021-02-10T17:05:30Z',
'cause' => 'cause',
Expand All @@ -51,6 +52,7 @@ public function testConstructor(): void
self::assertSame($values['staticSites'], $entity->staticSites);
self::assertSame($values['workers'], $entity->workers);
self::assertSame($values['jobs'], $entity->jobs);
self::assertSame($values['phaseLastUpdatedAt'], $entity->phaseLastUpdatedAt);
self::assertSame($values['createdAt'], $entity->createdAt);
self::assertSame($values['updatedAt'], $entity->updatedAt);
self::assertSame($values['cause'], $entity->cause);
Expand All @@ -60,9 +62,172 @@ public function testConstructor(): void
self::assertSame($values['tierSlug'], $entity->tierSlug);

self::assertSame($values['staticSites'], $entity->static_sites);
self::assertSame($values['phaseLastUpdatedAt'], $entity->phase_last_updated_at);
self::assertSame($values['createdAt'], $entity->created_at);
self::assertSame($values['updatedAt'], $entity->updated_at);
self::assertSame($values['clonedFrom'], $entity->cloned_from);
self::assertSame($values['tierSlug'], $entity->tier_slug);
}

public function testConstructorAcceptsApiShapedObjects(): void
{
$payload = \json_decode(<<<'JSON'
{
"id": "b6bdf840-2854-4f87-a36c-5f231c617c84",
"spec": {
"name": "sample-golang",
"services": [
{
"name": "web",
"github": {
"repo": "digitalocean/sample-golang",
"branch": "main"
},
"routes": [
{
"path": "/"
}
]
}
]
},
"services": [
{
"name": "web",
"source_commit_hash": "abc123",
"alerts": [
{
"rule": "CPU_UTILIZATION"
}
]
}
],
"static_sites": [
{
"name": "docs",
"routes": [
{
"path": "/docs"
}
]
}
],
"workers": [
{
"name": "queue",
"instance_count": 1
}
],
"jobs": [
{
"name": "migrate",
"kind": "POST_DEPLOY"
}
],
"phase_last_updated_at": "2024-01-02T00:03:00Z",
"created_at": "2024-01-02T00:00:00Z",
"updated_at": "2024-01-02T00:04:00Z",
"cause": "manual",
"cloned_from": "dep-0",
"progress": {
"success_steps": 1,
"steps": [
{
"name": "build",
"components": [
{
"name": "web"
}
]
}
]
},
"phase": "ACTIVE",
"tier_slug": "basic"
}
JSON, false, 512, \JSON_THROW_ON_ERROR);

$expectedSpec = [
'name' => 'sample-golang',
'services' => [
[
'name' => 'web',
'github' => [
'repo' => 'digitalocean/sample-golang',
'branch' => 'main',
],
'routes' => [
[
'path' => '/',
],
],
],
],
];
$expectedServices = [
[
'name' => 'web',
'source_commit_hash' => 'abc123',
'alerts' => [
[
'rule' => 'CPU_UTILIZATION',
],
],
],
];
$expectedStaticSites = [
[
'name' => 'docs',
'routes' => [
[
'path' => '/docs',
],
],
],
];
$expectedWorkers = [
[
'name' => 'queue',
'instance_count' => 1,
],
];
$expectedJobs = [
[
'name' => 'migrate',
'kind' => 'POST_DEPLOY',
],
];
$expectedProgress = [
'success_steps' => 1,
'steps' => [
[
'name' => 'build',
'components' => [
[
'name' => 'web',
],
],
],
],
];

$entity = new AppDeployment($payload);

self::assertInstanceOf(AbstractEntity::class, $entity);
self::assertInstanceOf(AppDeployment::class, $entity);
self::assertSame('b6bdf840-2854-4f87-a36c-5f231c617c84', $entity->id);
self::assertSame($expectedSpec, $entity->spec);
self::assertSame($expectedServices, $entity->services);
self::assertSame($expectedStaticSites, $entity->staticSites);
self::assertSame($expectedWorkers, $entity->workers);
self::assertSame($expectedJobs, $entity->jobs);
self::assertSame($expectedProgress, $entity->progress);
self::assertSame('2024-01-02T00:03:00Z', $entity->phaseLastUpdatedAt);
self::assertSame('dep-0', $entity->clonedFrom);
self::assertSame('basic', $entity->tierSlug);
self::assertSame($expectedStaticSites, $entity->static_sites);
self::assertSame('2024-01-02T00:03:00Z', $entity->phase_last_updated_at);
self::assertSame('dep-0', $entity->cloned_from);
self::assertSame('basic', $entity->tier_slug);
}
}
Loading