Skip to content

Commit 90e1014

Browse files
committed
Implemented Suggestion Fields and IntellixTrust Value
1 parent b6a1e57 commit 90e1014

File tree

4 files changed

+127
-44
lines changed

4 files changed

+127
-44
lines changed

src/DTO/Document.php

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44

55
use Carbon\Carbon;
66
use CodebarAg\DocuWare\Support\ParseValue;
7+
use Illuminate\Support\Arr;
78
use Illuminate\Support\Collection;
89
use Illuminate\Support\Str;
910

1011
final class Document
1112
{
1213
public static function fromJson(array $data): self
1314
{
14-
$fields = self::convertFields(collect($data['Fields']));
15+
$suggestions = Arr::has($data, 'Suggestions')
16+
? self::convertSuggestions(collect(Arr::get($data, 'Suggestions')))
17+
: null;
18+
19+
$fields = Arr::has($data, 'Fields')
20+
? self::convertFields(collect(Arr::get($data, 'Fields')))
21+
: null;
22+
1523

1624
return new self(
1725
id: $data['Id'],
@@ -21,9 +29,11 @@ public static function fromJson(array $data): self
2129
extension: $fields['DWEXTENSION']->value,
2230
content_type: $data['ContentType'],
2331
file_cabinet_id: $data['FileCabinetId'],
32+
intellixTrust: Arr::get($data, 'IntellixTrust'),
2433
created_at: ParseValue::date($data['CreatedAt']),
2534
updated_at: ParseValue::date($data['LastModified']),
2635
fields: $fields,
36+
suggestions: $suggestions,
2737
);
2838
}
2939

@@ -34,18 +44,28 @@ protected static function convertFields(Collection $fields): Collection
3444
});
3545
}
3646

47+
protected static function convertSuggestions(Collection $suggestions): Collection|null
48+
{
49+
return $suggestions->mapWithKeys(function (array $suggestion) {
50+
return [$suggestion['DBName'] => SuggestionField::fromJson($suggestion)];
51+
});
52+
}
53+
3754
public function __construct(
38-
public int $id,
39-
public int $file_size,
40-
public int $total_pages,
41-
public string $title,
42-
public string|null $extension,
43-
public string $content_type,
44-
public string $file_cabinet_id,
45-
public Carbon $created_at,
46-
public Carbon $updated_at,
47-
public Collection $fields,
48-
) {
55+
public int $id,
56+
public int $file_size,
57+
public int $total_pages,
58+
public string $title,
59+
public string|null $extension,
60+
public string $content_type,
61+
public string $file_cabinet_id,
62+
public string|null $intellixTrust,
63+
public Carbon $created_at,
64+
public Carbon $updated_at,
65+
public Collection|null $fields,
66+
public Collection|null $suggestions,
67+
)
68+
{
4969
}
5070

5171
public function isPdf(): bool
@@ -87,31 +107,36 @@ public function fileName(): string
87107
}
88108

89109
public static function fake(
90-
?int $id = null,
91-
?int $file_size = null,
92-
?int $total_pages = null,
93-
?string $title = null,
94-
?string $extension = null,
95-
?string $content_type = null,
96-
?string $file_cabinet_id = null,
97-
?Carbon $created_at = null,
98-
?Carbon $updated_at = null,
110+
?int $id = null,
111+
?int $file_size = null,
112+
?int $total_pages = null,
113+
?string $title = null,
114+
?string $extension = null,
115+
?string $content_type = null,
116+
?string $file_cabinet_id = null,
117+
?string $intellixTrust = null,
118+
?Carbon $created_at = null,
119+
?Carbon $updated_at = null,
99120
?Collection $fields = null,
100-
): self {
121+
?Collection $suggestions = null,
122+
): self
123+
{
101124
return new static(
102125
id: $id ?? random_int(1, 999999),
103126
file_size: $file_size ?? random_int(1000, 999999),
104127
total_pages: $total_pages ?? random_int(1, 100),
105128
title: $title ?? 'Fake Title',
106129
extension: $extension ?? '.pdf',
107130
content_type: $content_type ?? 'application/pdf',
108-
file_cabinet_id: $file_cabinet_id ?? (string) Str::uuid(),
131+
file_cabinet_id: $file_cabinet_id ?? (string)Str::uuid(),
132+
intellixTrust: $intellixTrust ?? 'Red',
109133
created_at: $created_at ?? now()->subDay(),
110134
updated_at: $updated_at ?? now(),
111135
fields: $fields ?? collect([
112-
DocumentField::fake(),
113-
DocumentField::fake(),
114-
]),
136+
DocumentField::fake(),
137+
DocumentField::fake(),
138+
]),
139+
suggestions: $suggestions ?? null
115140
);
116141
}
117142
}

src/DTO/SuggestionField.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace CodebarAg\DocuWare\DTO;
4+
5+
use Illuminate\Support\Arr;
6+
7+
final class SuggestionField
8+
{
9+
public static function fromJson(array $data): self
10+
{
11+
return new self(
12+
value: Arr::get($data, 'Value', []),
13+
name: Arr::get($data, 'Name'),
14+
db_name: Arr::get($data, 'DBName'),
15+
confidence: Arr::get($data, 'Confidence'),
16+
);
17+
}
18+
19+
public function __construct(
20+
public array $value,
21+
public string|null $name,
22+
public string|null $db_name,
23+
public string|null $confidence,
24+
)
25+
{
26+
}
27+
28+
public static function fake(
29+
array $value = [],
30+
?string $name = null,
31+
?string $db_name = null,
32+
?string $confidence = null,
33+
): self
34+
{
35+
return new self(
36+
value: $value ?? [],
37+
name: $name ?? 'Fake Name',
38+
db_name: $db_name ?? 'FAKE_NAME',
39+
confidence: $confidence ?? 'Red',
40+
);
41+
}
42+
}

src/DocuWare.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function getFileCabinets(): Collection
9999

100100
$cabinets = $response->throw()->json('FileCabinet');
101101

102-
return collect($cabinets)->map(fn (array $cabinet) => FileCabinet::fromJson($cabinet));
102+
return collect($cabinets)->map(fn(array $cabinet) => FileCabinet::fromJson($cabinet));
103103
}
104104

105105
public function getFields(string $fileCabinetId): Collection
@@ -123,7 +123,7 @@ public function getFields(string $fileCabinetId): Collection
123123

124124
$fields = $response->throw()->json('Fields');
125125

126-
return collect($fields)->map(fn (array $field) => Field::fromJson($field));
126+
return collect($fields)->map(fn(array $field) => Field::fromJson($field));
127127
}
128128

129129
public function getDialogs(string $fileCabinetId): Collection
@@ -147,14 +147,15 @@ public function getDialogs(string $fileCabinetId): Collection
147147

148148
$dialogs = $response->throw()->json('Dialog');
149149

150-
return collect($dialogs)->map(fn (array $dialog) => Dialog::fromJson($dialog));
150+
return collect($dialogs)->map(fn(array $dialog) => Dialog::fromJson($dialog));
151151
}
152152

153153
public function getSelectList(
154154
string $fileCabinetId,
155155
string $dialogId,
156156
string $fieldName,
157-
): array {
157+
): array
158+
{
158159
EnsureValidCookie::check();
159160

160161
$url = sprintf(
@@ -199,13 +200,16 @@ public function getDocument(string $fileCabinetId, int $documentId): Document
199200

200201
$data = $response->throw()->json();
201202

203+
ray($data, 'getDocument');
204+
202205
return Document::fromJson($data);
203206
}
204207

205208
public function getDocumentPreview(
206209
string $fileCabinetId,
207-
int $documentId,
208-
): string {
210+
int $documentId,
211+
): string
212+
{
209213
EnsureValidCookie::check();
210214

211215
$url = sprintf(
@@ -229,8 +233,9 @@ public function getDocumentPreview(
229233

230234
public function downloadDocument(
231235
string $fileCabinetId,
232-
int $documentId,
233-
): string {
236+
int $documentId,
237+
): string
238+
{
234239
EnsureValidCookie::check();
235240

236241
$url = sprintf(
@@ -254,8 +259,9 @@ public function downloadDocument(
254259

255260
public function downloadDocuments(
256261
string $fileCabinetId,
257-
array $documentIds,
258-
): string {
262+
array $documentIds,
263+
): string
264+
{
259265
EnsureValidCookie::check();
260266

261267
throw_if(
@@ -288,10 +294,11 @@ public function downloadDocuments(
288294

289295
public function updateDocumentValue(
290296
string $fileCabinetId,
291-
int $documentId,
297+
int $documentId,
292298
string $fieldName,
293299
string $newValue,
294-
): null|int|float|Carbon|string {
300+
): null|int|float|Carbon|string
301+
{
295302
EnsureValidCookie::check();
296303

297304
$url = sprintf(
@@ -325,11 +332,12 @@ public function updateDocumentValue(
325332
}
326333

327334
public function uploadDocument(
328-
string $fileCabinetId,
329-
string $fileContent,
330-
string $fileName,
335+
string $fileCabinetId,
336+
string $fileContent,
337+
string $fileName,
331338
?Collection $indexes = null,
332-
): Document {
339+
): Document
340+
{
333341
EnsureValidCookie::check();
334342

335343
$url = sprintf(
@@ -362,8 +370,9 @@ public function uploadDocument(
362370

363371
public function deleteDocument(
364372
string $fileCabinetId,
365-
int $documentId,
366-
): void {
373+
int $documentId,
374+
): void
375+
{
367376
EnsureValidCookie::check();
368377

369378
$url = sprintf(

tests/Feature/DTOTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use CodebarAg\DocuWare\DTO\DocumentPaginator;
99
use CodebarAg\DocuWare\DTO\Field;
1010
use CodebarAg\DocuWare\DTO\FileCabinet;
11+
use CodebarAg\DocuWare\DTO\SuggestionField;
1112
use CodebarAg\DocuWare\DTO\TableRow;
1213

1314
uses()->group('dto');
@@ -35,6 +36,12 @@
3536
$this->assertInstanceOf(DocumentField::class, $fake);
3637
});
3738

39+
it('create a fake suggestion field', function () {
40+
$fake = SuggestionField::fake();
41+
42+
$this->assertInstanceOf(SuggestionField::class, $fake);
43+
});
44+
3845
it('create a fake document', function () {
3946
$fake = Document::fake();
4047

0 commit comments

Comments
 (0)