diff --git a/README.md b/README.md index 8a4a67f..4a081a6 100644 --- a/README.md +++ b/README.md @@ -171,7 +171,7 @@ $thumbnail = $connector->send(new GetDocumentDownloadThumbnailRequest($fileCabin /** * Update value of a indexed field. */ -$value = $connector->send(new PutDocumentFieldsRequest($fileCabinetId, $documentId, [$fieldName => $newValue]))->dto()[$fieldName]; +$value = $connector->send(new PutDocumentFieldsRequest($fileCabinetId, $documentId, [$fieldName => $newValue]))->dto(); /** * Update multiple values of indexed fields. @@ -401,35 +401,35 @@ Please see [Tests](tests/Feature/DocuWare.php) for more details. ```php CodebarAg\DocuWare\DTO\DocumentIndex\IndexTextDTO { +name: "FIELD_TEXT" // string - +value: "Value" // string + +value: "Value" // null|string } ``` ```php CodebarAg\DocuWare\DTO\DocumentIndex\IndexNumericDTO { +name: "FIELD_NUMERIC" // string - +value: 1 // int + +value: 1 // null|int } ``` ```php CodebarAg\DocuWare\DTO\DocumentIndex\IndexDecimalDTO { +name: "FIELD_DECIMAL" // string - +value: 1.00 // int|float + +value: 1.00 // null|int|float } ``` ```php use CodebarAg\DocuWare\DTO\DocumentIndex\IndexDateDTO { +name: "FIELD_DATE" // string - +value: now(), // Carbon + +value: now(), // null|Carbon } ``` ```php use CodebarAg\DocuWare\DTO\DocumentIndex\IndexDateTimeDTO { +name: "FIELD_DATETIME" // string - +value: now(), // Carbon + +value: now(), // null|Carbon } ``` @@ -451,7 +451,7 @@ use CodebarAg\DocuWare\DTO\DocumentIndex\IndexTableDTO { 'VALUE' => 1.00, ], ] -]) // Collection|array +]) // null|Collection|array } ``` diff --git a/src/DTO/Dialog.php b/src/DTO/Dialog.php index f6927df..11edc64 100644 --- a/src/DTO/Dialog.php +++ b/src/DTO/Dialog.php @@ -35,11 +35,11 @@ public function isSearch(): bool } public static function fake( - string $id = null, - string $type = null, - string $label = null, - bool $isDefault = null, - string $fileCabinetId = null, + ?string $id = null, + ?string $type = null, + ?string $label = null, + ?bool $isDefault = null, + ?string $fileCabinetId = null, ): self { return new self( id: $id ?? (string) Str::uuid(), diff --git a/src/DTO/Document.php b/src/DTO/Document.php index 0273ea3..9e464e5 100644 --- a/src/DTO/Document.php +++ b/src/DTO/Document.php @@ -105,18 +105,18 @@ public function fileName(): string } public static function fake( - int $id = null, - int $file_size = null, - int $total_pages = null, - string $title = null, - string $extension = null, - string $content_type = null, - string $file_cabinet_id = null, - string $intellixTrust = null, - Carbon $created_at = null, - Carbon $updated_at = null, - Collection $fields = null, - Collection $suggestions = null, + ?int $id = null, + ?int $file_size = null, + ?int $total_pages = null, + ?string $title = null, + ?string $extension = null, + ?string $content_type = null, + ?string $file_cabinet_id = null, + ?string $intellixTrust = null, + ?Carbon $created_at = null, + ?Carbon $updated_at = null, + ?Collection $fields = null, + ?Collection $suggestions = null, ): self { return new self( id: $id ?? random_int(1, 999999), diff --git a/src/DTO/DocumentField.php b/src/DTO/DocumentField.php index 5d9aa27..46aa8b9 100644 --- a/src/DTO/DocumentField.php +++ b/src/DTO/DocumentField.php @@ -33,11 +33,11 @@ public function __construct( public static function fake( ?bool $systemField = false, - string $name = null, - string $label = null, + ?string $name = null, + ?string $label = null, ?bool $isNull = true, - int|float|Carbon|string $value = null, - string $type = null, + int|float|Carbon|string|null $value = null, + ?string $type = null, ): self { $fakeType = Arr::random(['Int', 'Decimal', 'Text', 'DateTime']); diff --git a/src/DTO/DocumentIndex/IndexDateDTO.php b/src/DTO/DocumentIndex/IndexDateDTO.php index 4180dec..c2fad49 100644 --- a/src/DTO/DocumentIndex/IndexDateDTO.php +++ b/src/DTO/DocumentIndex/IndexDateDTO.php @@ -8,12 +8,12 @@ class IndexDateDTO { public function __construct( public string $name, - public Carbon $value, + public null|Carbon|\Carbon\Carbon $value, ) { } - public static function make(string $name, Carbon $value): self + public static function make(string $name, null|Carbon|\Carbon\Carbon $value): self { return new self($name, $value); } diff --git a/src/DTO/DocumentIndex/IndexDateTimeDTO.php b/src/DTO/DocumentIndex/IndexDateTimeDTO.php index d32a9fa..491a1d1 100644 --- a/src/DTO/DocumentIndex/IndexDateTimeDTO.php +++ b/src/DTO/DocumentIndex/IndexDateTimeDTO.php @@ -8,12 +8,12 @@ class IndexDateTimeDTO { public function __construct( public string $name, - public Carbon $value, + public null|Carbon|\Carbon\Carbon $value, ) { } - public static function make(string $name, Carbon $value): self + public static function make(string $name, null|Carbon|\Carbon\Carbon $value): self { return new self($name, $value); } diff --git a/src/DTO/DocumentIndex/IndexDecimalDTO.php b/src/DTO/DocumentIndex/IndexDecimalDTO.php index a4b5c3d..2d89718 100644 --- a/src/DTO/DocumentIndex/IndexDecimalDTO.php +++ b/src/DTO/DocumentIndex/IndexDecimalDTO.php @@ -6,12 +6,12 @@ class IndexDecimalDTO { public function __construct( public string $name, - public int|float $value, + public null|int|float $value, ) { } - public static function make(string $name, int|float $value): self + public static function make(string $name, null|int|float $value): self { return new self($name, $value); } diff --git a/src/DTO/DocumentIndex/IndexNumericDTO.php b/src/DTO/DocumentIndex/IndexNumericDTO.php index 9e9b43a..4fc9764 100644 --- a/src/DTO/DocumentIndex/IndexNumericDTO.php +++ b/src/DTO/DocumentIndex/IndexNumericDTO.php @@ -8,12 +8,12 @@ class IndexNumericDTO public function __construct( public string $name, - public int $value, + public ?int $value, ) { } - public static function make(string $name, int $value): self + public static function make(string $name, ?int $value): self { return new self($name, $value); } diff --git a/src/DTO/DocumentIndex/IndexTableDTO.php b/src/DTO/DocumentIndex/IndexTableDTO.php index 5f80c77..efba784 100644 --- a/src/DTO/DocumentIndex/IndexTableDTO.php +++ b/src/DTO/DocumentIndex/IndexTableDTO.php @@ -9,12 +9,12 @@ class IndexTableDTO { public function __construct( public string $name, - public Collection|array $rows, + public null|Collection|array $rows, ) { } - public static function make(string $name, Collection|array $rows): self + public static function make(string $name, null|Collection|array $rows): self { return new self($name, $rows); } @@ -33,7 +33,7 @@ public function values(): array protected function rowsCollection(): array { - return collect($this->rows)->map(function ($row) { + return collect($this->rows ?? [])->map(function ($row) { $indexes = collect($row)->map(function ($column) { diff --git a/src/DTO/DocumentIndex/IndexTextDTO.php b/src/DTO/DocumentIndex/IndexTextDTO.php index 24d58bf..a495fc4 100644 --- a/src/DTO/DocumentIndex/IndexTextDTO.php +++ b/src/DTO/DocumentIndex/IndexTextDTO.php @@ -6,12 +6,12 @@ class IndexTextDTO { public function __construct( public string $name, - public string $value, + public ?string $value, ) { } - public static function make(string $name, string $value): self + public static function make(string $name, ?string $value): self { return new self($name, $value); } diff --git a/src/DTO/DocumentIndex/PrepareDTO.php b/src/DTO/DocumentIndex/PrepareDTO.php index a025493..0f6716a 100644 --- a/src/DTO/DocumentIndex/PrepareDTO.php +++ b/src/DTO/DocumentIndex/PrepareDTO.php @@ -19,7 +19,7 @@ public static function guess(string $name, mixed $value): mixed }; } - public static function makeContent(Collection $indexes): array + public static function makeFields(Collection $indexes): array { return [ 'Fields' => $indexes @@ -28,4 +28,15 @@ public static function makeContent(Collection $indexes): array ->values(), ]; } + + public static function makeField(Collection $indexes, bool $forceUpdate = false): array + { + return [ + 'Field' => $indexes + ->map(fn (IndexTextDTO|IndexDateDTO|IndexDateTimeDTO|IndexNumericDTO|IndexDecimalDTO|IndexTableDTO $index) => $index->values()) + ->filter() + ->values(), + 'ForceUpdate' => $forceUpdate, + ]; + } } diff --git a/src/DTO/DocumentPaginator.php b/src/DTO/DocumentPaginator.php index 7399abf..e9ea951 100644 --- a/src/DTO/DocumentPaginator.php +++ b/src/DTO/DocumentPaginator.php @@ -86,13 +86,13 @@ public static function fromFailed(Exception $e): self } public static function fake( - int $total = null, - int $per_page = null, - int $current_page = null, - int $last_page = null, - int $from = null, - int $to = null, - Collection $documents = null, + ?int $total = null, + ?int $per_page = null, + ?int $current_page = null, + ?int $last_page = null, + ?int $from = null, + ?int $to = null, + ?Collection $documents = null, ): self { return new self( total: $total ?? random_int(10, 100), diff --git a/src/DTO/Field.php b/src/DTO/Field.php index d2c00e5..0a55c4c 100644 --- a/src/DTO/Field.php +++ b/src/DTO/Field.php @@ -35,10 +35,10 @@ public function isUser(): bool } public static function fake( - string $name = null, - string $label = null, - string $type = null, - string $scope = null, + ?string $name = null, + ?string $label = null, + ?string $type = null, + ?string $scope = null, ): self { return new self( name: $name ?? 'FAKE_FIELD', diff --git a/src/DTO/FileCabinet.php b/src/DTO/FileCabinet.php index 42b492b..2dc68c4 100644 --- a/src/DTO/FileCabinet.php +++ b/src/DTO/FileCabinet.php @@ -28,11 +28,11 @@ public function __construct( } public static function fake( - string $id = null, - string $name = null, - string $color = null, - bool $isBasket = null, - string $assignedCabinet = null, + ?string $id = null, + ?string $name = null, + ?string $color = null, + ?bool $isBasket = null, + ?string $assignedCabinet = null, ): self { return new self( id: $id ?? (string) Str::uuid(), diff --git a/src/DTO/Organization.php b/src/DTO/Organization.php index 6f367a3..7e49651 100644 --- a/src/DTO/Organization.php +++ b/src/DTO/Organization.php @@ -28,9 +28,9 @@ public function __construct( } public static function fake( - string $id = null, - string $name = null, - string $guid = null, + ?string $id = null, + ?string $name = null, + ?string $guid = null, array $additionalInfo = [], array $configurationRights = [], ): self { diff --git a/src/DTO/OrganizationIndex.php b/src/DTO/OrganizationIndex.php index 5ef295b..ef72e96 100644 --- a/src/DTO/OrganizationIndex.php +++ b/src/DTO/OrganizationIndex.php @@ -24,9 +24,9 @@ public function __construct( } public static function fake( - string $id = null, - string $name = null, - string $guid = null, + ?string $id = null, + ?string $name = null, + ?string $guid = null, ): self { return new self( id: $id ?? (string) Str::uuid(), diff --git a/src/DTO/SuggestionField.php b/src/DTO/SuggestionField.php index 68c4bc6..a5d933e 100644 --- a/src/DTO/SuggestionField.php +++ b/src/DTO/SuggestionField.php @@ -26,9 +26,9 @@ public function __construct( public static function fake( array $value = [], - string $name = null, - string $db_name = null, - string $confidence = null, + ?string $name = null, + ?string $db_name = null, + ?string $confidence = null, ): self { return new self( value: $value, diff --git a/src/DTO/TableRow.php b/src/DTO/TableRow.php index 9746aa8..9b12a84 100644 --- a/src/DTO/TableRow.php +++ b/src/DTO/TableRow.php @@ -28,7 +28,7 @@ public function __construct( } public static function fake( - Collection $fields = null, + ?Collection $fields = null, ): self { return new self( fields: $fields ?? collect([ diff --git a/src/Requests/Document/PostDocumentRequest.php b/src/Requests/Document/PostDocumentRequest.php index 495451a..c8c49d3 100644 --- a/src/Requests/Document/PostDocumentRequest.php +++ b/src/Requests/Document/PostDocumentRequest.php @@ -36,7 +36,7 @@ protected function defaultBody(): array $body = []; if ($this->indexes) { - $indexContent = json_encode(PrepareDTO::makeContent($this->indexes)); + $indexContent = json_encode(PrepareDTO::makeFields($this->indexes)); $body[] = new MultipartValue(name: 'document', value: $indexContent, filename: 'index.json'); } diff --git a/src/Requests/Document/PutDocumentFieldsRequest.php b/src/Requests/Document/PutDocumentFieldsRequest.php index 3ff1712..2189ad7 100644 --- a/src/Requests/Document/PutDocumentFieldsRequest.php +++ b/src/Requests/Document/PutDocumentFieldsRequest.php @@ -2,8 +2,10 @@ namespace CodebarAg\DocuWare\Requests\Document; +use CodebarAg\DocuWare\DTO\DocumentIndex\PrepareDTO; use CodebarAg\DocuWare\Exceptions\UnableToUpdateFields; use CodebarAg\DocuWare\Responses\Document\PutDocumentFieldsResponse; +use Illuminate\Support\Collection; use Saloon\Contracts\Body\HasBody; use Saloon\Enums\Method; use Saloon\Http\Request; @@ -22,7 +24,7 @@ class PutDocumentFieldsRequest extends Request implements HasBody public function __construct( protected readonly string $fileCabinetId, protected readonly string $documentId, - protected readonly array $values, + protected readonly ?Collection $indexes = null, protected readonly bool $forceUpdate = false, ) { } @@ -34,27 +36,16 @@ public function resolveEndpoint(): string public function defaultBody(): array { - throw_unless(count($this->values) > 0, UnableToUpdateFields::noValuesProvided()); + $body = []; - $fields = []; - - foreach ($this->values as $key => $value) { - throw_unless($value, UnableToUpdateFields::noValuesProvidedForField($key)); - $fields[] = [ - 'FieldName' => $key, - 'Item' => $value, - ]; + if ($this->indexes) { + $bodyEncode = json_encode(PrepareDTO::makeField($this->indexes)); + $bodyDecode = json_decode($bodyEncode, true); + $body = $bodyDecode; } - $content = [ - 'Field' => $fields, - ]; - - if ($this->forceUpdate) { - $content['ForceUpdate'] = true; - } + return $body; - return $content; } public function createDtoFromResponse(Response $response): mixed diff --git a/src/Support/ParseValue.php b/src/Support/ParseValue.php index 3ff29af..1637b2f 100644 --- a/src/Support/ParseValue.php +++ b/src/Support/ParseValue.php @@ -12,7 +12,7 @@ class ParseValue { public static function field( ?array $field, - int|float|Carbon|string|Collection $default = null, + int|float|Carbon|string|Collection|null $default = null, ): null|int|float|Carbon|string|Collection { if (! $field || $field['IsNull']) { return $default; diff --git a/tests/Feature/Requests/Document/PutDocumentFieldsRequestTest.php b/tests/Feature/Requests/Document/PutDocumentFieldsRequestTest.php index bee2b2e..092c10e 100644 --- a/tests/Feature/Requests/Document/PutDocumentFieldsRequestTest.php +++ b/tests/Feature/Requests/Document/PutDocumentFieldsRequestTest.php @@ -2,6 +2,7 @@ use CodebarAg\DocuWare\Connectors\DocuWareStaticConnector; use CodebarAg\DocuWare\DTO\Config; +use CodebarAg\DocuWare\DTO\DocumentIndex\IndexTextDTO; use CodebarAg\DocuWare\Events\DocuWareResponseLog; use CodebarAg\DocuWare\Requests\Document\DeleteDocumentRequest; use CodebarAg\DocuWare\Requests\Document\PostDocumentRequest; @@ -41,7 +42,9 @@ $response = $this->connector->send(new PutDocumentFieldsRequest( $fileCabinetId, $document->id, - ['UUID' => $newValue] + collect([ + IndexTextDTO::make('UUID', $newValue), + ]) ))->dto(); $this->assertSame('laravel-docuware', $response['UUID']); @@ -57,10 +60,6 @@ Event::fake(); $fileCabinetId = config('laravel-docuware.tests.file_cabinet_id'); - $values = [ - 'UUID' => 'laravel-docuware', - 'DOCUMENT_LABEL' => 'laravel-docuware-2', - ]; $document = $this->connector->send(new PostDocumentRequest( $fileCabinetId, @@ -71,7 +70,10 @@ $response = $this->connector->send(new PutDocumentFieldsRequest( $fileCabinetId, $document->id, - $values, + collect([ + IndexTextDTO::make('UUID', 'laravel-docuware'), + IndexTextDTO::make('DOCUMENT_LABEL', 'laravel-docuware-2'), + ]), true ))->dto(); diff --git a/tests/Unit/DTO/DocumentIndex/PrepareDTOTest.php b/tests/Unit/DTO/DocumentIndex/PrepareDTOTest.php index 7186d81..0d08ccb 100644 --- a/tests/Unit/DTO/DocumentIndex/PrepareDTOTest.php +++ b/tests/Unit/DTO/DocumentIndex/PrepareDTOTest.php @@ -21,7 +21,7 @@ $indexes = collect([PrepareDTO::guess($name, $value)]); - expect(PrepareDTO::makeContent($indexes)) + expect(PrepareDTO::makeFields($indexes)) ->toBeArray(); })->group('dto');