Skip to content

Commit 949b732

Browse files
Space improvements: new methods & SpaceEnvironment (Preview URLs) support (#28)
* Adding Space class helpers * Handling Environments / Preview URLs
1 parent 6b1f319 commit 949b732

File tree

6 files changed

+325
-82
lines changed

6 files changed

+325
-82
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.0.8 - 2025-12-08
4+
- Space class improvement, added some helper methos like `isOwnedByUser()`, `domain()`, `isDemo()`, `fistToken()`, `removeDemoMode()`
5+
- Added Space methods for handling environments/Preview URLs
6+
37
## 1.0.7 - 2025-12-07
48
- Added support for creating workflow stage changes via the Management API.
59

README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,54 @@ $spaceId = "12345";
185185
$spaceApi = new SpaceApi($clientEU);
186186
$space = $spaceApi->get($spaceId)->data();
187187

188-
printf(" The name for the Space id : %s is : %s . Plan: %s - %s" ,
188+
printf(
189+
"Space ID: %s\n
190+
Name: %s\n
191+
Plan: %s (%s)\n
192+
Domain: %s\n
193+
Is Demo: %s\n
194+
First Token: %s\n",
189195
$spaceId,
190196
$space->name(),
191197
$space->planLevel(),
192-
$space->planDescription()
198+
$space->planDescription(),
199+
$space->domain(),
200+
$space->isDemo() ? "yes" : "no",
201+
$space->firstToken()
193202
);
194203
```
204+
205+
### Environments / Preview URLs
206+
207+
Preview URLs allow you to define multiple environments (domains or locations) for quickly switching between different preview targets inside the story editor—such as local development, staging, or production.
208+
209+
#### Listing Environments
210+
211+
You can retrieve all configured environments for a space and iterate through them:
212+
213+
```php
214+
foreach ($space->environments() as $key => $environment) {
215+
echo "Name: " . $environment->name() . PHP_EOL;
216+
echo "Location: " . $environment->location() . PHP_EOL;
217+
}
218+
```
219+
220+
#### Adding a New Environment (Preview URL)
221+
222+
To add a new preview environment, create a `SpaceEnvironment` instance and attach it to the space:
223+
224+
```php
225+
$previewEnvironment = new SpaceEnvironment(
226+
"Local Development",
227+
$previewURLlocalhost
228+
);
229+
230+
$editSpace->addEnvironment($previewEnvironment);
231+
```
232+
233+
You can repeat this for any number of environments—such as staging, QA, production—allowing editors to switch preview contexts seamlessly.
234+
235+
195236
### Update Space settings
196237

197238
You can edit space settings using the `update()` method.

src/Data/Space.php

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Space extends BaseData
1616
public function __construct(string $name)
1717
{
1818
$this->data = [];
19-
$this->data['name'] = $name;
19+
$this->data["name"] = $name;
2020
}
2121

2222
/**
@@ -35,47 +35,85 @@ public static function make(array $data = []): self
3535
}
3636

3737
return $space;
38-
3938
}
4039

4140
public function setName(string $name): void
4241
{
43-
$this->set('name', $name);
42+
$this->set("name", $name);
4443
}
4544

4645
public function setDomain(string $domain): void
4746
{
48-
$this->set('domain', $domain);
47+
$this->set("domain", $domain);
4948
}
5049

5150
public function name(): string
5251
{
53-
return $this->getString('name', "");
52+
return $this->getString("name", "");
5453
}
5554

5655
public function region(): string
5756
{
58-
return $this->getString('region', "");
57+
return $this->getString("region", "");
5958
}
6059

6160
public function id(): string
6261
{
63-
return $this->getString('id', "");
62+
return $this->getString("id", "");
63+
}
64+
65+
/**
66+
* Retrieves the domain associated with the Space.
67+
*
68+
* Returns the value stored under the "domain" key as a string.
69+
*
70+
* @return string The domain name.
71+
*/
72+
public function domain(): string
73+
{
74+
return $this->getString("domain");
75+
}
76+
77+
/**
78+
* Retrieves the first token associated with the Space.
79+
*
80+
* Returns the value stored under the "first_token" key.
81+
*
82+
* @return string The first token, or an empty string if none is defined.
83+
*/
84+
public function firstToken(): string
85+
{
86+
return $this->getString("first_token", "");
87+
}
88+
89+
public function environments(): SpaceEnvironments
90+
{
91+
return SpaceEnvironments::make($this->getArray("environments"));
92+
}
93+
94+
public function addEnvironment(
95+
SpaceEnvironment $spaceEnvironment,
96+
): SpaceEnvironments {
97+
$environments = $this->getArray("environments");
98+
$environments[] = $spaceEnvironment->toArray();
99+
$this->set("environments", $environments);
100+
101+
return SpaceEnvironments::make($environments);
64102
}
65103

66104
public function createdAt(): null|string
67105
{
68-
return $this->getFormattedDateTime('created_at', "", format: "Y-m-d");
106+
return $this->getFormattedDateTime("created_at", "", format: "Y-m-d");
69107
}
70108

71109
public function updatedAt(): null|string
72110
{
73-
return $this->getFormattedDateTime('updated_at', "", format: "Y-m-d");
111+
return $this->getFormattedDateTime("updated_at", "", format: "Y-m-d");
74112
}
75113

76114
public function planLevel(): string
77115
{
78-
return $this->getString('plan_level');
116+
return $this->getString("plan_level");
79117
}
80118

81119
public function planDescription(): null|string
@@ -85,6 +123,39 @@ public function planDescription(): null|string
85123

86124
public function ownerId(): string
87125
{
88-
return $this->getString('owner_id', "");
126+
return $this->getString("owner_id", "");
127+
}
128+
129+
/**
130+
* Determines whether the current entity is owned by the given user.
131+
*
132+
* Compares the space's owner ID with the ID of the provided user instance.
133+
*
134+
* @param User $user The user to check ownership against.
135+
* @return bool True if the user owns the entity, false otherwise.
136+
*/
137+
public function isOwnedByUser(User $user): bool
138+
{
139+
return $this->ownerId() === $user->id();
140+
}
141+
142+
/**
143+
* Checks whether the entity is marked as a demo instance.
144+
*
145+
* Retrieves the `is_demo` flag and returns its boolean value.
146+
*
147+
* @return bool True if the entity is marked as a demo, false otherwise.
148+
*/
149+
public function isDemo(): bool
150+
{
151+
return $this->getBoolean("is_demo", false);
152+
}
153+
154+
/**
155+
* Remove the Demo mode flag
156+
*/
157+
public function removeDemoMode(): void
158+
{
159+
$this->set("is_demo", false);
89160
}
90161
}

src/Data/SpaceEnvironment.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Storyblok\ManagementApi\Data;
6+
7+
use Storyblok\ManagementApi\Exceptions\StoryblokFormatException;
8+
9+
class SpaceEnvironment extends BaseData
10+
{
11+
public function __construct(string $name, string $location)
12+
{
13+
$this->data = [];
14+
$this->data["location"] = $location;
15+
$this->data["name"] = $name;
16+
}
17+
18+
/**
19+
* @param mixed[] $data
20+
* @throws StoryblokFormatException
21+
*/
22+
public static function make(array $data = []): self
23+
{
24+
$dataObject = new StoryblokData($data);
25+
if (!$dataObject->hasKey("name")) {
26+
throw new StoryblokFormatException(
27+
"Environments/Preview URL is not valid, missing the name",
28+
);
29+
}
30+
31+
if (!$dataObject->hasKey("location")) {
32+
throw new StoryblokFormatException(
33+
"Environments/Preview URL is not valid, missing the location/URL",
34+
);
35+
}
36+
37+
return new self(
38+
$dataObject->getString("name"),
39+
$dataObject->getString("location"),
40+
);
41+
}
42+
43+
public function setName(string $name): void
44+
{
45+
$this->set("name", $name);
46+
}
47+
48+
/**
49+
* Environment/Preview URL name
50+
*/
51+
public function name(): string
52+
{
53+
return $this->getString("name");
54+
}
55+
56+
public function setLocation(string $location): void
57+
{
58+
$this->set("location", $location);
59+
}
60+
61+
/**
62+
* Environment/Preview URL location
63+
*/
64+
public function location(): string
65+
{
66+
return $this->getString("location");
67+
}
68+
69+
/**
70+
* Validates if the Environment/Preview URL data contains all required fields and valid values
71+
*/
72+
public function isValid(): bool
73+
{
74+
return $this->hasKey("name") && $this->hasKey("location");
75+
}
76+
}

src/Data/SpaceEnvironments.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Storyblok\ManagementApi\Data;
6+
7+
use Storyblok\ManagementApi\Data\StoryblokData;
8+
9+
class SpaceEnvironments extends StoryblokData
10+
{
11+
#[\Override]
12+
public function getDataClass(): string
13+
{
14+
return SpaceEnvironment::class;
15+
}
16+
17+
#[\Override]
18+
public static function make(array $data = []): self
19+
{
20+
return new self($data);
21+
}
22+
23+
public function howManyEnvironments(): int
24+
{
25+
return $this->count();
26+
}
27+
}

0 commit comments

Comments
 (0)