Skip to content

Commit b3a8289

Browse files
Adding AssetField class (#24)
* Adding Asset Field Add some helpers/methods/classes for the #22 * fix for new Rector rule * added asset methods
1 parent e846aab commit b3a8289

File tree

12 files changed

+359
-153
lines changed

12 files changed

+359
-153
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/http-client": "^7.0"
2121
},
2222
"require-dev": {
23-
"phpstan/phpstan" : "^2.0",
23+
"phpstan/phpstan": "^2.0",
2424
"rector/rector": "^2",
2525
"friendsofphp/php-cs-fixer": "^3.65",
2626
"pestphp/pest": "^3.7"

rector.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
use Rector\Config\RectorConfig;
66

77
return RectorConfig::configure()
8-
->withPaths([
9-
__DIR__ . '/src',
10-
__DIR__ . '/tests',
11-
])
8+
->withPaths([__DIR__ . "/src", __DIR__ . "/tests"])
129
->withPhpSets(php83: true)
1310
->withPreparedSets(
1411
deadCode: true,
@@ -19,12 +16,11 @@
1916
instanceOf: true,
2017

2118
earlyReturn: true,
22-
strictBooleans: true,
2319
carbon: true,
2420
rectorPreset: true,
2521
phpunitCodeQuality: true,
2622
//privatization: true,
2723
);
28-
//->withTypeCoverageLevel(100)
29-
//->withDeadCodeLevel(100)
30-
//->withCodeQualityLevel(100);
24+
//->withTypeCoverageLevel(100)
25+
//->withDeadCodeLevel(100)
26+
//->withCodeQualityLevel(100);

src/Data/Asset.php

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44

55
namespace Storyblok\ManagementApi\Data;
66

7+
use Storyblok\ManagementApi\Data\Traits\AssetMethods;
78
use Storyblok\ManagementApi\Exceptions\StoryblokFormatException;
89

910
class Asset extends BaseData
1011
{
11-
public function __construct(
12-
string $filename,
13-
) {
12+
use AssetMethods;
13+
14+
public function __construct(string $filename)
15+
{
1416
$this->data = [];
15-
$this->data['filename'] = $filename;
16-
$this->data['fieldtype'] = "asset";
17+
$this->data["filename"] = $filename;
18+
$this->data["fieldtype"] = "asset";
1719
}
1820

1921
/**
@@ -25,20 +27,23 @@ public function __construct(
2527
public static function make(array $data = []): self
2628
{
2729
$dataObject = new StoryblokData($data);
28-
if (! $dataObject->hasKey('fieldtype')) {
30+
if (!$dataObject->hasKey("fieldtype")) {
2931
$dataObject->set("fieldtype", "asset");
3032
}
3133

32-
if (!($dataObject->hasKey('filename') && $dataObject->hasKey('fieldtype'))) {
34+
if (
35+
!(
36+
$dataObject->hasKey("filename") &&
37+
$dataObject->hasKey("fieldtype")
38+
)
39+
) {
3340
// is not valid
3441
}
3542

36-
$asset = new Asset(
37-
$dataObject->getString("filename")
38-
);
43+
$asset = new Asset($dataObject->getString("filename"));
3944
$asset->setData($dataObject->toArray());
4045
// validate
41-
if (! $asset->isValid()) {
46+
if (!$asset->isValid()) {
4247
throw new StoryblokFormatException("Asset is not valid");
4348
}
4449

@@ -47,12 +52,7 @@ public static function make(array $data = []): self
4752

4853
public function isValid(): bool
4954
{
50-
return $this->hasKey('filename');
51-
}
52-
53-
public function id(): string
54-
{
55-
return $this->getString('id', "");
55+
return $this->hasKey("filename");
5656
}
5757

5858
public function filenameCDN(): string
@@ -64,34 +64,28 @@ public function filenameCDN(): string
6464
);
6565
}
6666

67-
public function filename(): string
68-
{
69-
return $this->getString('filename', "");
70-
}
71-
7267
public function contentType(): string
7368
{
74-
return $this->getString('content_type');
69+
return $this->getString("content_type");
7570
}
7671

7772
public function contentLength(): int|null
7873
{
79-
return $this->getInt('content_length');
74+
return $this->getInt("content_length");
8075
}
8176

8277
public function createdAt(): null|string
8378
{
84-
return $this->getFormattedDateTime('created_at', "", format: "Y-m-d");
79+
return $this->getFormattedDateTime("created_at", "", format: "Y-m-d");
8580
}
8681

8782
public function updatedAt(): null|string
8883
{
89-
return $this->getFormattedDateTime('updated_at', "", format: "Y-m-d");
84+
return $this->getFormattedDateTime("updated_at", "", format: "Y-m-d");
9085
}
9186

9287
public function setExternalUrl(string $url): self
9388
{
94-
9589
$this->set("filename", $url);
9690
$this->set("is_external_url", true);
9791
return $this;
@@ -101,16 +95,15 @@ public static function emptyAsset(): Asset
10195
{
10296
return self::make([
10397
"id" => null,
104-
"alt"=> null,
105-
"name"=> "",
106-
"focus"=> null,
107-
"title"=> null,
108-
"source"=> null,
109-
"filename"=> "",
110-
"copyright"=> null,
111-
"fieldtype"=> "asset",
112-
"meta_data"=> [],
98+
"alt" => "",
99+
"name" => "",
100+
"focus" => "",
101+
"title" => "",
102+
"source" => "",
103+
"filename" => "",
104+
"copyright" => "",
105+
"fieldtype" => "asset",
106+
"meta_data" => (object) [],
113107
]);
114-
115108
}
116109
}

src/Data/BaseData.php

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
* @implements ArrayAccess<int|string, mixed>
2121
* @implements Iterator<int|string, mixed>
2222
*/
23-
abstract class BaseData implements StoryblokDataInterface, Iterator, ArrayAccess, Countable
23+
abstract class BaseData implements
24+
StoryblokDataInterface,
25+
Iterator,
26+
ArrayAccess,
27+
Countable
2428
{
2529
use IterableDataTrait;
2630

@@ -47,8 +51,11 @@ public function setData(array $data): void
4751
$this->data = $data;
4852
}
4953

50-
public function getInt(int|string $key, int|null $defaultValue = null, string $charNestedKey = "."): int|null
51-
{
54+
public function getInt(
55+
int|string $key,
56+
int|null $defaultValue = null,
57+
string $charNestedKey = ".",
58+
): int|null {
5259
$returnValue = $this->get($key, null, $charNestedKey);
5360

5461
if (is_scalar($returnValue)) {
@@ -58,8 +65,11 @@ public function getInt(int|string $key, int|null $defaultValue = null, string $c
5865
return $defaultValue;
5966
}
6067

61-
public function getBoolean(int|string $key, bool $defaultValue = false, string $charNestedKey = "."): bool
62-
{
68+
public function getBoolean(
69+
int|string $key,
70+
bool $defaultValue = false,
71+
string $charNestedKey = ".",
72+
): bool {
6373
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
6474

6575
if (is_scalar($returnValue)) {
@@ -73,12 +83,15 @@ public function getBoolean(int|string $key, bool $defaultValue = false, string $
7383
* @param mixed[] $defaultValue
7484
* @return mixed[]
7585
*/
76-
public function getArray(int|string $key, array $defaultValue = [], string $charNestedKey = "."): array
77-
{
86+
public function getArray(
87+
int|string $key,
88+
array $defaultValue = [],
89+
string $charNestedKey = ".",
90+
): array {
7891
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
7992

8093
if (is_scalar($returnValue)) {
81-
return [strval($returnValue) ];
94+
return [strval($returnValue)];
8295
}
8396

8497
if ($returnValue instanceof StoryblokData) {
@@ -118,8 +131,11 @@ public function getFormattedDateTime(
118131
* @param string $charNestedKey The character used for separating nested keys (default: ".").
119132
* @return $this The current instance for method chaining.
120133
*/
121-
public function set(int|string $key, mixed $value, string $charNestedKey = "."): self
122-
{
134+
public function set(
135+
int|string $key,
136+
mixed $value,
137+
string $charNestedKey = ".",
138+
): self {
123139
if (is_string($key)) {
124140
$array = &$this->data;
125141
if ($charNestedKey === "") {
@@ -180,7 +196,6 @@ protected function returnData(mixed $value, bool $raw = false): mixed
180196
}
181197

182198
return new StoryblokData([]);
183-
184199
}
185200

186201
/**
@@ -262,8 +277,12 @@ public function dump(): void
262277
* @param bool $raw Whether to return raw data or cast it into StoryblokData if applicable.
263278
* @return mixed The value associated with the key, or the default value if the key does not exist.
264279
*/
265-
public function get(int|string $key, mixed $defaultValue = null, string $charNestedKey = ".", bool $raw = false): mixed
266-
{
280+
public function get(
281+
int|string $key,
282+
mixed $defaultValue = null,
283+
string $charNestedKey = ".",
284+
bool $raw = false,
285+
): mixed {
267286
if (is_string($key)) {
268287
if ($charNestedKey === "") {
269288
$charNestedKey = ".";
@@ -273,7 +292,10 @@ public function get(int|string $key, mixed $defaultValue = null, string $charNes
273292
if (str_contains($keyString, $charNestedKey)) {
274293
$nestedValue = $this->data;
275294
foreach (explode($charNestedKey, $keyString) as $nestedKey) {
276-
if (is_array($nestedValue) && array_key_exists($nestedKey, $nestedValue)) {
295+
if (
296+
is_array($nestedValue) &&
297+
array_key_exists($nestedKey, $nestedValue)
298+
) {
277299
$nestedValue = $nestedValue[$nestedKey];
278300
} elseif ($nestedValue instanceof StoryblokData) {
279301
$nestedValue = $nestedValue->get($nestedKey);
@@ -285,18 +307,23 @@ public function get(int|string $key, mixed $defaultValue = null, string $charNes
285307
return $this->returnData($nestedValue, $raw);
286308
}
287309

288-
if (! array_key_exists($key, $this->data)) {
310+
if (!array_key_exists($key, $this->data)) {
289311
return $defaultValue;
290312
}
313+
}
291314

315+
if (is_numeric($key) && !array_key_exists($key, $this->data)) {
316+
return $defaultValue;
292317
}
293318

294319
return $this->returnData($this->data[$key], $raw) ?? $defaultValue;
295-
296320
}
297321

298-
public function getString(int|string $key, string $defaultValue = "", string $charNestedKey = "."): string
299-
{
322+
public function getString(
323+
int|string $key,
324+
string $defaultValue = "",
325+
string $charNestedKey = ".",
326+
): string {
300327
$returnValue = $this->get($key, "", $charNestedKey);
301328

302329
if (is_scalar($returnValue)) {
@@ -306,8 +333,11 @@ public function getString(int|string $key, string $defaultValue = "", string $ch
306333
return $defaultValue;
307334
}
308335

309-
public function getStringNullable(int|string $key, string|null $defaultValue = null, string $charNestedKey = "."): string|null
310-
{
336+
public function getStringNullable(
337+
int|string $key,
338+
string|null $defaultValue = null,
339+
string $charNestedKey = ".",
340+
): string|null {
311341
$returnValue = $this->get($key, $defaultValue, $charNestedKey);
312342

313343
if (is_scalar($returnValue)) {

src/Data/Enum/Region.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
enum Region: string
88
{
99
case EU = 'EU';
10+
1011
case US = 'US';
12+
1113
case AP = 'AP';
14+
1215
case CA = 'CA';
16+
1317
case CN = 'CN';
1418

1519
/**

src/Data/Fields/AssetField.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Storyblok\ManagementApi\Data\Fields;
6+
7+
use Storyblok\ManagementApi\Data\Asset;
8+
use Storyblok\ManagementApi\Data\BaseData;
9+
use Storyblok\ManagementApi\Data\Traits\AssetMethods;
10+
11+
class AssetField extends BaseData
12+
{
13+
use AssetMethods;
14+
15+
public function __construct(string $filename = "")
16+
{
17+
$this->data = [
18+
"id" => null,
19+
"alt" => "",
20+
"name" => "",
21+
"focus" => "",
22+
"title" => "",
23+
"source" => "",
24+
"filename" => $filename,
25+
"copyright" => "",
26+
"fieldtype" => "asset",
27+
"meta_data" => (object) [],
28+
];
29+
}
30+
31+
public static function makeFromAsset(Asset $asset): self
32+
{
33+
$field = new self();
34+
$attributes = [
35+
"id",
36+
"alt",
37+
"name",
38+
"focus",
39+
"title",
40+
"source",
41+
"filename",
42+
"copyright",
43+
"fieldtype",
44+
"meta_data",
45+
];
46+
foreach ($attributes as $attribute) {
47+
$field->set($attribute, $asset->get($attribute));
48+
}
49+
50+
$field->set("name", $asset->getString("name"));
51+
$field->set("filename", $asset->filenameCDN());
52+
$field->set("is_external_url", false);
53+
$field->set("meta_data", (object) $asset->getArray("meta_data"));
54+
return $field;
55+
}
56+
57+
public function isExternalUrl(): bool
58+
{
59+
return $this->getBoolean("is_external_url", false);
60+
}
61+
}

0 commit comments

Comments
 (0)