Skip to content

Commit ec15de3

Browse files
committed
wip
1 parent 4813153 commit ec15de3

20 files changed

+1088
-140
lines changed

src/DTO/Document.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static function fromArray(array $data): self
5656
checkedOutAt: $checkedOutAt ? CarbonImmutable::parse($checkedOutAt) : null,
5757
isDeleted: Arr::get($data, 'IsDeleted'),
5858
propertyID: Arr::get($data, 'propertyID'),
59-
properties: collect($properties)->map(fn (array $property) => Property::fromArray($property)),
59+
properties: collect($properties)->map(fn (array $property) => GetProperty::fromArray($property)),
6060
files: collect($files)->map(fn (array $file) => File::fromArray($file)),
6161
);
6262
}
@@ -78,7 +78,7 @@ public function toArray(): array
7878
'checkedOutAt' => $this->checkedOutAt?->toISOString(),
7979
'isDeleted' => $this->isDeleted,
8080
'propertyID' => $this->propertyID,
81-
'properties' => $this->properties?->map(fn (Property $property) => $property->toArray())->toArray(),
81+
'properties' => $this->properties?->map(fn (GetProperty $property) => $property->toArray())->toArray(),
8282
'files' => $this->files?->map(fn (File $file) => $file->toArray())->toArray(),
8383
];
8484
}

src/DTO/File.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,26 @@
44

55
namespace CodebarAg\MFiles\DTO;
66

7-
use Carbon\CarbonImmutable;
87
use Illuminate\Support\Arr;
98

109
final class File
1110
{
1211
public function __construct(
13-
public readonly ?int $id,
14-
public readonly ?string $name,
12+
public readonly int $id,
13+
public readonly string $name,
1514
public readonly ?string $extension,
15+
public readonly ?int $version,
1616
public readonly ?int $size,
17-
public readonly ?CarbonImmutable $lastModified,
1817
) {}
1918

2019
public static function fromArray(array $data): self
2120
{
22-
$lastModified = Arr::get($data, 'LastModified');
23-
2421
return new self(
2522
id: Arr::get($data, 'ID'),
2623
name: Arr::get($data, 'Name'),
2724
extension: Arr::get($data, 'Extension'),
25+
version: Arr::get($data, 'Version'),
2826
size: Arr::get($data, 'Size'),
29-
lastModified: $lastModified ? CarbonImmutable::parse($lastModified) : null,
3027
);
3128
}
3229

@@ -36,8 +33,8 @@ public function toArray(): array
3633
'id' => $this->id,
3734
'name' => $this->name,
3835
'extension' => $this->extension,
36+
'version' => $this->version,
3937
'size' => $this->size,
40-
'lastModified' => $this->lastModified?->toISOString(),
4138
];
4239
}
4340
}

src/DTO/GetProperty.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 CodebarAg\MFiles\DTO;
6+
7+
use CodebarAg\MFiles\Enums\MFDataTypeEnum;
8+
use Illuminate\Support\Arr;
9+
10+
final class GetProperty
11+
{
12+
public function __construct(
13+
public readonly int $propertyDef,
14+
public readonly MFDataTypeEnum $dataType,
15+
public readonly mixed $value,
16+
public readonly mixed $displayValue,
17+
) {}
18+
19+
public static function fromArray(array $data): self
20+
{
21+
$dataTypeValue = Arr::get($data, 'Value.DataType');
22+
$dataType = $dataTypeValue !== null ? MFDataTypeEnum::tryFrom($dataTypeValue) : MFDataTypeEnum::TEXT;
23+
24+
$values = self::getValues($dataType, Arr::get($data, 'Value'));
25+
26+
return new self(
27+
propertyDef: Arr::get($data, 'PropertyDef'),
28+
dataType: $dataType,
29+
value: Arr::get($values, 'Value'),
30+
displayValue: Arr::get($values, 'DisplayValue'),
31+
);
32+
}
33+
34+
private static function getValues(MFDataTypeEnum $dataType, mixed $value): mixed
35+
{
36+
return match ($dataType) {
37+
MFDataTypeEnum::LOOKUP => [
38+
'Value' => Arr::get($value, 'Lookup'),
39+
'DisplayValue' => Arr::get($value, 'DisplayValue'),
40+
],
41+
MFDataTypeEnum::MULTISELECTLOOKUP => [
42+
'Value' => Arr::get($value, 'Lookups'),
43+
'DisplayValue' => Arr::get($value, 'DisplayValue'),
44+
],
45+
default => [
46+
'Value' => Arr::get($value, 'Value'),
47+
'DisplayValue' => Arr::get($value, 'DisplayValue'),
48+
],
49+
};
50+
}
51+
52+
public function toArray(): array
53+
{
54+
return [
55+
'propertyDef' => $this->propertyDef,
56+
'dataType' => $this->dataType->value,
57+
'value' => $this->value,
58+
'displayValue' => $this->displayValue,
59+
];
60+
}
61+
}

src/DTO/ObjectProperties.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,49 @@
44

55
namespace CodebarAg\MFiles\DTO;
66

7+
use Carbon\CarbonImmutable;
78
use Illuminate\Support\Arr;
89
use Illuminate\Support\Collection;
910

1011
final class ObjectProperties
1112
{
1213
public function __construct(
14+
public readonly int $classId,
15+
public readonly int $objectId,
16+
public readonly int $objectTypeId,
17+
public readonly int $objectVersionId,
18+
public readonly CarbonImmutable $lastModifiedAt,
1319
public readonly Collection $properties,
14-
public readonly ?int $objectId = null,
15-
public readonly ?string $objectType = null,
16-
public readonly ?int $objectTypeId = null,
20+
public readonly Collection $files,
1721
) {}
1822

1923
public static function fromArray(array $data): self
2024
{
25+
$lastModifiedAt = Arr::get($data, 'ObjVer.Modified');
26+
$properties = Arr::get($data, 'Properties', []);
27+
$files = Arr::get($data, 'Files', []);
28+
2129
return new self(
22-
properties: collect(Arr::get($data, 'Properties', []))
23-
->map(fn (array $property) => Property::fromArray($property)),
24-
objectId: Arr::get($data, 'ObjectID'),
25-
objectType: Arr::get($data, 'ObjectType'),
26-
objectTypeId: Arr::get($data, 'ObjectTypeID'),
30+
classId: Arr::get($data, 'Class'),
31+
objectId: Arr::get($data, 'ObjVer.ID'),
32+
objectTypeId: Arr::get($data, 'ObjVer.Type'),
33+
objectVersionId: Arr::get($data, 'ObjVer.Version'),
34+
lastModifiedAt: CarbonImmutable::parse($lastModifiedAt),
35+
properties: collect($properties)->map(fn (array $property) => GetProperty::fromArray($property)),
36+
files: collect($files)->map(fn (array $file) => File::fromArray($file)),
2737
);
2838
}
2939

3040
public function toArray(): array
3141
{
3242
return [
33-
'properties' => $this->properties->map(fn (Property $property) => $property->toArray())->toArray(),
43+
'classId' => $this->classId,
3444
'objectId' => $this->objectId,
35-
'objectType' => $this->objectType,
3645
'objectTypeId' => $this->objectTypeId,
46+
'objectVersionId' => $this->objectVersionId,
47+
'lastModifiedAt' => $this->lastModifiedAt->toIso8601String(),
48+
'properties' => $this->properties->toArray(),
49+
'files' => $this->files->toArray(),
3750
];
3851
}
3952
}

src/DTO/Property.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/DTO/PropertyValue.php renamed to src/DTO/SetProperty.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66

77
use CodebarAg\MFiles\Enums\MFDataTypeEnum;
88

9-
final class PropertyValue
9+
final class SetProperty
1010
{
1111
public function __construct(
1212
public readonly int $propertyDef,
1313
public readonly MFDataTypeEnum $dataType,
1414
public readonly mixed $value,
15+
public readonly mixed $displayValue = null,
1516
) {}
1617

17-
public static function fromArray(int $propertyDef, MFDataTypeEnum $dataType, mixed $value): self
18+
public static function fromArray(int $propertyDef, MFDataTypeEnum $dataType, mixed $value, mixed $displayValue = null): self
1819
{
1920
return new self(
2021
propertyDef: $propertyDef,
2122
dataType: $dataType,
2223
value: $value,
24+
displayValue: $displayValue ?? $value,
2325
);
2426
}
2527

src/Requests/CreateSingleFileDocumentRequest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
namespace CodebarAg\MFiles\Requests;
66

77
use CodebarAg\MFiles\DTO\Document;
8-
use CodebarAg\MFiles\DTO\PropertyValue;
8+
use CodebarAg\MFiles\DTO\SetProperty;
99
use CodebarAg\MFiles\Responses\CreateSingleFileDocumentResponse;
1010
use Saloon\Contracts\Body\HasBody;
1111
use Saloon\Enums\Method;
1212
use Saloon\Http\Request;
13+
use Saloon\Http\Response;
1314
use Saloon\Traits\Body\HasJsonBody;
1415
use Saloon\Traits\Plugins\AcceptsJson;
1516

@@ -35,13 +36,13 @@ protected function defaultBody(): array
3536
{
3637
return [
3738
'PropertyValues' => collect($this->propertyValues)
38-
->map(fn (PropertyValue $propertyValue) => $propertyValue->toArray())
39+
->map(fn (SetProperty $propertyValue) => $propertyValue->toArray())
3940
->toArray(),
4041
'Files' => $this->files,
4142
];
4243
}
4344

44-
public function createDtoFromResponse(\Saloon\Http\Response $response): Document
45+
public function createDtoFromResponse(Response $response): Document
4546
{
4647
return CreateSingleFileDocumentResponse::createDtoFromResponse($response);
4748
}

src/Requests/DownloadFileRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use CodebarAg\MFiles\Responses\DownloadFileResponse;
99
use Saloon\Enums\Method;
1010
use Saloon\Http\Request;
11+
use Saloon\Http\Response;
1112

1213
class DownloadFileRequest extends Request
1314
{
@@ -25,7 +26,7 @@ public function resolveEndpoint(): string
2526
return "/objects/{$this->objectType}/{$this->objectId}/{$this->objectVersion}/files/{$this->fileId}/content";
2627
}
2728

28-
public function createDtoFromResponse(\Saloon\Http\Response $response): DownloadedFile
29+
public function createDtoFromResponse(Response $response): DownloadedFile
2930
{
3031
return DownloadFileResponse::createDtoFromResponse($response);
3132
}

src/Requests/GetObjectInformationRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use CodebarAg\MFiles\Responses\GetObjectInformationResponse;
99
use Saloon\Enums\Method;
1010
use Saloon\Http\Request;
11+
use Saloon\Http\Response;
1112
use Saloon\Traits\Plugins\AcceptsJson;
1213

1314
class GetObjectInformationRequest extends Request
@@ -27,7 +28,7 @@ public function resolveEndpoint(): string
2728
return "/objects/{$this->objectType}/{$this->objectId}/{$this->objectVersion}?include=properties";
2829
}
2930

30-
public function createDtoFromResponse(\Saloon\Http\Response $response): ObjectProperties
31+
public function createDtoFromResponse(Response $response): ObjectProperties
3132
{
3233
return GetObjectInformationResponse::createDtoFromResponse($response);
3334
}

src/Requests/LogInToVaultRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace CodebarAg\MFiles\Requests;
66

7-
use Illuminate\Support\Arr;
7+
use CodebarAg\MFiles\Responses\LogInToVaultResponse;
88
use Saloon\Contracts\Body\HasBody;
99
use Saloon\Enums\Method;
1010
use Saloon\Http\Response;
@@ -50,6 +50,6 @@ protected function defaultBody(): array
5050

5151
public function createDtoFromResponse(Response $response): ?string
5252
{
53-
return Arr::get($response->json(), 'Value');
53+
return LogInToVaultResponse::createDtoFromResponse($response);
5454
}
5555
}

0 commit comments

Comments
 (0)