From d193e354e3b93a2a3486bf5f103ef65d4bdcd95a Mon Sep 17 00:00:00 2001 From: Rhys Lees <43909932+RhysLees@users.noreply.github.com> Date: Tue, 12 Dec 2023 11:29:38 +0100 Subject: [PATCH] Feature Types and FilterIn (#120) * WIP Co-authored-by: RhysLees --- README.md | 10 +++ src/DocuWareSearchRequestBuilder.php | 26 ++++++- .../Document/DeleteDocumentRequest.php | 2 +- .../Document/GetDocumentCountRequest.php | 2 +- .../Document/GetDocumentDownloadRequest.php | 2 +- .../Document/GetDocumentPreviewRequest.php | 2 +- src/Requests/Document/GetDocumentRequest.php | 3 +- .../Document/GetDocumentsDownloadRequest.php | 2 +- src/Requests/Document/GetDocumentsRequest.php | 3 +- src/Requests/Document/PostDocumentRequest.php | 3 +- .../Document/PostTransferDocumentRequest.php | 2 +- .../Document/PutDocumentFieldsRequest.php | 2 +- src/Requests/Fields/GetFieldsRequest.php | 4 +- .../FileCabinets/GetFileCabinetsRequest.php | 4 +- .../Organization/GetOrganizationRequest.php | 3 +- .../Organization/GetOrganizationsRequest.php | 4 +- src/Requests/Search/GetSearchRequest.php | 3 +- src/Requests/Sections/GetSectionsRequest.php | 4 +- .../Requests/Search/GetSearchRequestTest.php | 72 +++++++++++++++++++ 19 files changed, 134 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 4a081a6c..297a86eb 100644 --- a/README.md +++ b/README.md @@ -325,6 +325,16 @@ $paginatorRequest = DocuWare::searchRequestBuilder() ->get(); $paginator = $connector->send($paginatorRequest)->dto(); + +/** + * Search documents filtered to multiple values. + */ +$paginatorRequest = DocuWare::searchRequestBuilder() + ->fileCabinet($id) + ->filterIn('TYPE', ['Order', 'Invoice']) + ->get(); + +$paginator = $connector->send($paginatorRequest)->dto(); /** * You can specify the dialog which should be used. diff --git a/src/DocuWareSearchRequestBuilder.php b/src/DocuWareSearchRequestBuilder.php index a7c8c09a..a347c1c7 100644 --- a/src/DocuWareSearchRequestBuilder.php +++ b/src/DocuWareSearchRequestBuilder.php @@ -108,13 +108,33 @@ public function orderBy(string $field, ?string $direction = 'asc'): self return $this; } - public function filter(string $name, mixed $value): self + private static function prepareValueForFilter(mixed $value): mixed { if (is_string($value)) { - $value = "\"{$value}\""; + return "\"{$value}\""; } - $this->filters[$name][] = $value; + return $value; + } + + public function filter(string $name, mixed $value): self + { + $this->filters[$name][] = self::prepareValueForFilter($value); + + return $this; + } + + public function filterIn(string $name, mixed $values): self + { + if (is_string($values)) { + return $this->filter($name, $values); + } + + $values = collect($values)->map(function ($value) { + return self::prepareValueForFilter($value); + })->toArray(); + + $this->filters[$name][] = implode(' OR ', $values); return $this; } diff --git a/src/Requests/Document/DeleteDocumentRequest.php b/src/Requests/Document/DeleteDocumentRequest.php index f48ae9bc..cb359062 100644 --- a/src/Requests/Document/DeleteDocumentRequest.php +++ b/src/Requests/Document/DeleteDocumentRequest.php @@ -22,7 +22,7 @@ public function resolveEndpoint(): string return '/FileCabinets/'.$this->fileCabinetId.'/Documents/'.$this->documentId; } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): Response { return DeleteDocumentResponse::fromResponse($response); } diff --git a/src/Requests/Document/GetDocumentCountRequest.php b/src/Requests/Document/GetDocumentCountRequest.php index 8e6598ab..d33b6e0d 100644 --- a/src/Requests/Document/GetDocumentCountRequest.php +++ b/src/Requests/Document/GetDocumentCountRequest.php @@ -45,7 +45,7 @@ public function defaultQuery(): array ]; } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): int { return GetDocumentCountResponse::fromResponse($response); } diff --git a/src/Requests/Document/GetDocumentDownloadRequest.php b/src/Requests/Document/GetDocumentDownloadRequest.php index f69f8c71..09c2a641 100644 --- a/src/Requests/Document/GetDocumentDownloadRequest.php +++ b/src/Requests/Document/GetDocumentDownloadRequest.php @@ -46,7 +46,7 @@ public function defaultQuery(): array ]; } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): string { return GetDocumentDownloadResponse::fromResponse($response); } diff --git a/src/Requests/Document/GetDocumentPreviewRequest.php b/src/Requests/Document/GetDocumentPreviewRequest.php index 88efcb8a..2fc42e27 100644 --- a/src/Requests/Document/GetDocumentPreviewRequest.php +++ b/src/Requests/Document/GetDocumentPreviewRequest.php @@ -38,7 +38,7 @@ public function cacheExpiryInSeconds(): int return config('laravel-docuware.configurations.cache.lifetime_in_seconds', 3600); } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): string { return GetDocumentPreviewResponse::fromResponse($response); } diff --git a/src/Requests/Document/GetDocumentRequest.php b/src/Requests/Document/GetDocumentRequest.php index 4373067d..9b11bc35 100644 --- a/src/Requests/Document/GetDocumentRequest.php +++ b/src/Requests/Document/GetDocumentRequest.php @@ -2,6 +2,7 @@ namespace CodebarAg\DocuWare\Requests\Document; +use CodebarAg\DocuWare\DTO\Document; use CodebarAg\DocuWare\Responses\Document\GetDocumentResponse; use Illuminate\Support\Facades\Cache; use Saloon\CachePlugin\Contracts\Cacheable; @@ -38,7 +39,7 @@ public function cacheExpiryInSeconds(): int return config('laravel-docuware.configurations.cache.lifetime_in_seconds', 3600); } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): Document { return GetDocumentResponse::fromResponse($response); } diff --git a/src/Requests/Document/GetDocumentsDownloadRequest.php b/src/Requests/Document/GetDocumentsDownloadRequest.php index a40c9d79..0653cd30 100644 --- a/src/Requests/Document/GetDocumentsDownloadRequest.php +++ b/src/Requests/Document/GetDocumentsDownloadRequest.php @@ -57,7 +57,7 @@ public function defaultQuery(): array ]; } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): string { return GetDocumentsDownloadResponse::fromResponse($response); } diff --git a/src/Requests/Document/GetDocumentsRequest.php b/src/Requests/Document/GetDocumentsRequest.php index 44d44e4d..2a41c05a 100644 --- a/src/Requests/Document/GetDocumentsRequest.php +++ b/src/Requests/Document/GetDocumentsRequest.php @@ -3,6 +3,7 @@ namespace CodebarAg\DocuWare\Requests\Document; use CodebarAg\DocuWare\Responses\Document\GetDocumentsResponse; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; use Saloon\CachePlugin\Contracts\Cacheable; use Saloon\CachePlugin\Drivers\LaravelCacheDriver; @@ -37,7 +38,7 @@ public function cacheExpiryInSeconds(): int return config('laravel-docuware.configurations.cache.lifetime_in_seconds', 3600); } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): Collection { return GetDocumentsResponse::fromResponse($response); } diff --git a/src/Requests/Document/PostDocumentRequest.php b/src/Requests/Document/PostDocumentRequest.php index c8c49d3f..9da153e7 100644 --- a/src/Requests/Document/PostDocumentRequest.php +++ b/src/Requests/Document/PostDocumentRequest.php @@ -2,6 +2,7 @@ namespace CodebarAg\DocuWare\Requests\Document; +use CodebarAg\DocuWare\DTO\Document; use CodebarAg\DocuWare\DTO\DocumentIndex\PrepareDTO; use CodebarAg\DocuWare\Responses\Document\PostDocumentResponse; use Illuminate\Support\Collection; @@ -45,7 +46,7 @@ protected function defaultBody(): array return $body; } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): Document { return PostDocumentResponse::fromResponse($response); } diff --git a/src/Requests/Document/PostTransferDocumentRequest.php b/src/Requests/Document/PostTransferDocumentRequest.php index 171791a2..c7928587 100644 --- a/src/Requests/Document/PostTransferDocumentRequest.php +++ b/src/Requests/Document/PostTransferDocumentRequest.php @@ -54,7 +54,7 @@ protected function defaultBody(): array ]; } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): bool { return PostTransferDocumentResponse::fromResponse($response); } diff --git a/src/Requests/Document/PutDocumentFieldsRequest.php b/src/Requests/Document/PutDocumentFieldsRequest.php index 2189ad72..9b88165f 100644 --- a/src/Requests/Document/PutDocumentFieldsRequest.php +++ b/src/Requests/Document/PutDocumentFieldsRequest.php @@ -48,7 +48,7 @@ public function defaultBody(): array } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): Collection { return PutDocumentFieldsResponse::fromResponse($response); } diff --git a/src/Requests/Fields/GetFieldsRequest.php b/src/Requests/Fields/GetFieldsRequest.php index 5e2e34fd..3361f532 100644 --- a/src/Requests/Fields/GetFieldsRequest.php +++ b/src/Requests/Fields/GetFieldsRequest.php @@ -3,6 +3,8 @@ namespace CodebarAg\DocuWare\Requests\Fields; use CodebarAg\DocuWare\Responses\Fields\GetFieldsResponse; +use Illuminate\Support\Collection; +use Illuminate\Support\Enumerable; use Illuminate\Support\Facades\Cache; use Saloon\CachePlugin\Contracts\Cacheable; use Saloon\CachePlugin\Drivers\LaravelCacheDriver; @@ -37,7 +39,7 @@ public function cacheExpiryInSeconds(): int return config('laravel-docuware.configurations.cache.lifetime_in_seconds', 3600); } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): Collection|Enumerable { return GetFieldsResponse::fromResponse($response); } diff --git a/src/Requests/FileCabinets/GetFileCabinetsRequest.php b/src/Requests/FileCabinets/GetFileCabinetsRequest.php index 8c164d17..3fbef5af 100644 --- a/src/Requests/FileCabinets/GetFileCabinetsRequest.php +++ b/src/Requests/FileCabinets/GetFileCabinetsRequest.php @@ -3,6 +3,8 @@ namespace CodebarAg\DocuWare\Requests\FileCabinets; use CodebarAg\DocuWare\Responses\FileCabinets\GetFileCabinetsResponse; +use Illuminate\Support\Collection; +use Illuminate\Support\Enumerable; use Illuminate\Support\Facades\Cache; use Saloon\CachePlugin\Contracts\Cacheable; use Saloon\CachePlugin\Drivers\LaravelCacheDriver; @@ -32,7 +34,7 @@ public function cacheExpiryInSeconds(): int return config('laravel-docuware.configurations.cache.lifetime_in_seconds', 3600); } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): Collection|Enumerable { return GetFileCabinetsResponse::fromResponse($response); } diff --git a/src/Requests/Organization/GetOrganizationRequest.php b/src/Requests/Organization/GetOrganizationRequest.php index 475170fb..e85b322d 100644 --- a/src/Requests/Organization/GetOrganizationRequest.php +++ b/src/Requests/Organization/GetOrganizationRequest.php @@ -2,6 +2,7 @@ namespace CodebarAg\DocuWare\Requests\Organization; +use CodebarAg\DocuWare\DTO\Organization; use CodebarAg\DocuWare\Responses\Organization\GetOrganizationResponse; use Illuminate\Support\Facades\Cache; use Saloon\CachePlugin\Contracts\Cacheable; @@ -37,7 +38,7 @@ public function cacheExpiryInSeconds(): int return config('laravel-docuware.configurations.cache.lifetime_in_seconds', 3600); } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): Organization { return GetOrganizationResponse::fromResponse($response); } diff --git a/src/Requests/Organization/GetOrganizationsRequest.php b/src/Requests/Organization/GetOrganizationsRequest.php index 1cb7b6fb..21872337 100644 --- a/src/Requests/Organization/GetOrganizationsRequest.php +++ b/src/Requests/Organization/GetOrganizationsRequest.php @@ -3,6 +3,8 @@ namespace CodebarAg\DocuWare\Requests\Organization; use CodebarAg\DocuWare\Responses\Organization\GetOrganizationsResponse; +use Illuminate\Support\Collection; +use Illuminate\Support\Enumerable; use Illuminate\Support\Facades\Cache; use Saloon\CachePlugin\Contracts\Cacheable; use Saloon\CachePlugin\Drivers\LaravelCacheDriver; @@ -32,7 +34,7 @@ public function cacheExpiryInSeconds(): int return config('laravel-docuware.configurations.cache.lifetime_in_seconds', 3600); } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): Collection|Enumerable { return GetOrganizationsResponse::fromResponse($response); } diff --git a/src/Requests/Search/GetSearchRequest.php b/src/Requests/Search/GetSearchRequest.php index 4e51f32a..a06800e9 100644 --- a/src/Requests/Search/GetSearchRequest.php +++ b/src/Requests/Search/GetSearchRequest.php @@ -2,6 +2,7 @@ namespace CodebarAg\DocuWare\Requests\Search; +use CodebarAg\DocuWare\DTO\DocumentPaginator; use CodebarAg\DocuWare\Responses\Search\GetSearchResponse; use Illuminate\Support\Facades\Cache; use Saloon\CachePlugin\Contracts\Cacheable; @@ -79,7 +80,7 @@ public function defaultBody(): array ]; } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): DocumentPaginator { return GetSearchResponse::fromResponse($response, $this->page, $this->perPage); } diff --git a/src/Requests/Sections/GetSectionsRequest.php b/src/Requests/Sections/GetSectionsRequest.php index eb961eea..7609933e 100644 --- a/src/Requests/Sections/GetSectionsRequest.php +++ b/src/Requests/Sections/GetSectionsRequest.php @@ -3,6 +3,8 @@ namespace CodebarAg\DocuWare\Requests\Sections; use CodebarAg\DocuWare\Responses\Sections\GetSectionsResponse; +use Illuminate\Support\Collection; +use Illuminate\Support\Enumerable; use Illuminate\Support\Facades\Cache; use Saloon\CachePlugin\Contracts\Cacheable; use Saloon\CachePlugin\Drivers\LaravelCacheDriver; @@ -45,7 +47,7 @@ public function cacheExpiryInSeconds(): int return config('laravel-docuware.configurations.cache.lifetime_in_seconds', 3600); } - public function createDtoFromResponse(Response $response): mixed + public function createDtoFromResponse(Response $response): Collection|Enumerable { return GetSectionsResponse::fromResponse($response); } diff --git a/tests/Feature/Requests/Search/GetSearchRequestTest.php b/tests/Feature/Requests/Search/GetSearchRequestTest.php index d3e48f18..db532649 100644 --- a/tests/Feature/Requests/Search/GetSearchRequestTest.php +++ b/tests/Feature/Requests/Search/GetSearchRequestTest.php @@ -4,9 +4,12 @@ use CodebarAg\DocuWare\Connectors\DocuWareStaticConnector; use CodebarAg\DocuWare\DocuWare; use CodebarAg\DocuWare\DTO\Config; +use CodebarAg\DocuWare\DTO\DocumentIndex\IndexTextDTO; use CodebarAg\DocuWare\DTO\DocumentPaginator; use CodebarAg\DocuWare\Events\DocuWareResponseLog; use CodebarAg\DocuWare\Exceptions\UnableToSearch; +use CodebarAg\DocuWare\Requests\Document\DeleteDocumentRequest; +use CodebarAg\DocuWare\Requests\Document\PostDocumentRequest; use CodebarAg\DocuWare\Support\EnsureValidCookie; use Illuminate\Support\Facades\Event; @@ -221,3 +224,72 @@ $this->assertInstanceOf(DocumentPaginator::class, $paginator); Event::assertDispatched(DocuWareResponseLog::class); })->group('search'); + +it('can search documents with multiple values', function () { + Event::fake(); + + $fileCabinetId = config('laravel-docuware.tests.file_cabinet_id'); + $fileContent = '::fake-file-content::'; + $fileName = 'example.txt'; + + $documentOne = $this->connector->send(new PostDocumentRequest( + $fileCabinetId, + $fileContent, + $fileName, + collect([ + IndexTextDTO::make('DOCUMENT_LABEL', '::text::'), + IndexTextDTO::make('DOCUMENT_TYPE', 'Abrechnung'), + ]), + ))->dto(); + + $documentTwo = $this->connector->send(new PostDocumentRequest( + $fileCabinetId, + $fileContent, + $fileName, + collect([ + IndexTextDTO::make('DOCUMENT_LABEL', '::text::'), + IndexTextDTO::make('DOCUMENT_TYPE', 'Rechnung'), + ]), + ))->dto(); + + $documentThree = $this->connector->send(new PostDocumentRequest( + $fileCabinetId, + $fileContent, + $fileName, + collect([ + IndexTextDTO::make('DOCUMENT_LABEL', '::text::'), + IndexTextDTO::make('DOCUMENT_TYPE', 'EtwasAnderes'), + ]), + ))->dto(); + + // Should filter down to documentOne and documentTwo. documentThree should be filtered out. + $paginatorRequestBothDocuments = (new DocuWare()) + ->searchRequestBuilder() + ->fileCabinets([$fileCabinetId]) + ->page(null) + ->perPage(null) + ->fulltext(null) + ->filterIn('DOCUMENT_TYPE', ['Abrechnung', 'Rechnung']) + ->get(); + + $paginator = $this->connector->send($paginatorRequestBothDocuments)->dto(); + + $this->connector->send(new DeleteDocumentRequest( + $fileCabinetId, + $documentOne->id, + ))->dto(); + + $this->connector->send(new DeleteDocumentRequest( + $fileCabinetId, + $documentTwo->id, + ))->dto(); + + $this->connector->send(new DeleteDocumentRequest( + $fileCabinetId, + $documentThree->id, + ))->dto(); + + $this->assertInstanceOf(DocumentPaginator::class, $paginator); + $this->assertCount(2, $paginator->documents); + Event::assertDispatched(DocuWareResponseLog::class); +})->group('search');