Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Types and FilterIn #120

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 23 additions & 3 deletions src/DocuWareSearchRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Document/DeleteDocumentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Document/GetDocumentCountRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function defaultQuery(): array
];
}

public function createDtoFromResponse(Response $response): mixed
public function createDtoFromResponse(Response $response): int
{
return GetDocumentCountResponse::fromResponse($response);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Document/GetDocumentDownloadRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function defaultQuery(): array
];
}

public function createDtoFromResponse(Response $response): mixed
public function createDtoFromResponse(Response $response): string
{
return GetDocumentDownloadResponse::fromResponse($response);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Document/GetDocumentPreviewRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Requests/Document/GetDocumentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Document/GetDocumentsDownloadRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function defaultQuery(): array
];
}

public function createDtoFromResponse(Response $response): mixed
public function createDtoFromResponse(Response $response): string
{
return GetDocumentsDownloadResponse::fromResponse($response);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Requests/Document/GetDocumentsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Requests/Document/PostDocumentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Document/PostTransferDocumentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function defaultBody(): array
];
}

public function createDtoFromResponse(Response $response): mixed
public function createDtoFromResponse(Response $response): bool
{
return PostTransferDocumentResponse::fromResponse($response);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Document/PutDocumentFieldsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function defaultBody(): array

}

public function createDtoFromResponse(Response $response): mixed
public function createDtoFromResponse(Response $response): Collection
{
return PutDocumentFieldsResponse::fromResponse($response);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Requests/Fields/GetFieldsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Requests/FileCabinets/GetFileCabinetsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Requests/Organization/GetOrganizationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Requests/Organization/GetOrganizationsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Requests/Search/GetSearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Requests/Sections/GetSectionsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
72 changes: 72 additions & 0 deletions tests/Feature/Requests/Search/GetSearchRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');