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 Textshot #169

Merged
merged 3 commits into from
Sep 20, 2024
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
3 changes: 0 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ jobs:
- name: Execute tests
run: cp phpunit.xml.dist phpunit.xml

- name: Migrate Config
run: vendor/bin/pest --migrate-configuration

- name: Execute tests
run: vendor/bin/pest
env:
Expand Down
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ then optimize the processes that power the core of your business.
| Documents/Sections | Get All Sections from a Document | ✅ | |
| Documents/Sections | Get a Specific Section | ✅ | |
| Documents/Sections | Delete Section | ✅ | |
| Documents/Sections/Textshot | Get Textshot for a Specific Section | ✅ | |
| Documents/Download | Download Document | ✅ | |
| Documents/Download | Download Section | ✅ | |
| Documents/Download | Download Thumbnail | ✅ | |
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"license": "MIT",
"authors": [
{
"name": "Sebastian Bürgin",
"name": "Sebastian Bürgin-Fix",
"email": "[email protected]",
"homepage": "https://www.codebar.ch",
"role": "Sofware-Engineer"
Expand Down Expand Up @@ -41,7 +41,8 @@
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
"spatie/laravel-ray": "^1.33"
"phpunit/phpunit": "^10.5",
"spatie/laravel-ray": "^1.35"
},
"autoload": {
"psr-4": {
Expand Down
17 changes: 16 additions & 1 deletion docs/Documents/sections.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Sections

| Request | Supported |
|----------------------------------|-----------|
| Get All Sections from a Document | ✅ |
| Get a Specific Section | ✅ |
| Delete Section | ✅ |

| Get Textshot | ✅ |

### Get All Sections

```php
use CodebarAg\DocuWare\Requests\Documents\Sections\GetAllSectionsFromADocument;

Expand All @@ -17,6 +19,7 @@ $sections = $connector->send(new GetAllSectionsFromADocument(
```

### Get Specific Section

```php
use CodebarAg\DocuWare\Requests\Documents\Sections\GetASpecificSection;

Expand All @@ -27,6 +30,7 @@ $section = $connector->send(new GetASpecificSection(
```

### Delete Section

```php
use CodebarAg\DocuWare\Requests\Documents\Sections\DeleteSection;

Expand All @@ -35,3 +39,14 @@ $deleted = $connector->send(new DeleteSection(
$sectionId
))->dto();
```

### Get Textshot

```php
use CodebarAg\DocuWare\Requests\Documents\Sections\GetTextshot;

$deleted = $connector->send(new GetTextshot(
$fileCabinetId,
$sectionId
))->dto();
```
24 changes: 24 additions & 0 deletions src/DTO/Textshot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace CodebarAg\DocuWare\DTO;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

final class Textshot
{
public static function fromJson(array $data): self
{
$pages = collect(Arr::get($data, 'Pages', []));

return new self(
page_count: $pages->count(),
pages: TextshotPage::fromCollection($pages),
);
}

public function __construct(
public int $page_count,
public Collection $pages,
) {}
}
52 changes: 52 additions & 0 deletions src/DTO/TextshotPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace CodebarAg\DocuWare\DTO;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

final class TextshotPage
{
public static function fromCollection(Collection $collection): Collection
{
return $collection->map(fn (array $data) => self::fromJson($data));
}

public static function fromJson(array $data): self
{
$rawItems = Arr::get($data, 'Items', []);

return new self(
language: Arr::get($data, 'Lang'),
content: self::content($rawItems)
);
}

public function __construct(
public string $language,
public string $content,
) {}

protected static function content(array $rawItems): string
{
return collect($rawItems)
->filter(function ($item) {
return Arr::get($item, '$type') === 'TextZone';
})
->pluck('Ln')
->flatten(2)
->filter(function ($item) {
return is_array($item);
})
->flatten(1)
->map(function ($item) {

$type = Arr::get($item, '$type');

return match ($type) {
'Word' => Arr::get($item, 'Value'),
default => null,
};
})->implode(' ');
}
}
44 changes: 44 additions & 0 deletions src/Requests/Documents/Sections/GetTextshot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace CodebarAg\DocuWare\Requests\Documents\Sections;

use CodebarAg\DocuWare\Responses\Documents\Sections\GetTextshotResponse;
use Illuminate\Support\Facades\Cache;
use Saloon\CachePlugin\Contracts\Cacheable;
use Saloon\CachePlugin\Drivers\LaravelCacheDriver;
use Saloon\CachePlugin\Traits\HasCaching;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

class GetTextshot extends Request implements Cacheable
{
use HasCaching;

protected Method $method = Method::GET;

public function __construct(
protected readonly string $fileCabinetId,
protected readonly string $sectionId,
) {}

public function resolveEndpoint(): string
{
return '/FileCabinets/'.$this->fileCabinetId.'/Sections/'.$this->sectionId.'/Textshot';
}

public function resolveCacheDriver(): LaravelCacheDriver
{
return new LaravelCacheDriver(Cache::store(config('laravel-docuware.configurations.cache.driver')));
}

public function cacheExpiryInSeconds(): int
{
return config('laravel-docuware.configurations.cache.lifetime_in_seconds', 3600);
}

public function createDtoFromResponse(Response $response): mixed
{
return GetTextshotResponse::fromResponse($response);
}
}
20 changes: 20 additions & 0 deletions src/Responses/Documents/Sections/GetTextshotResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace CodebarAg\DocuWare\Responses\Documents\Sections;

use CodebarAg\DocuWare\DTO\Textshot;
use CodebarAg\DocuWare\Events\DocuWareResponseLog;
use CodebarAg\DocuWare\Support\EnsureValidResponse;
use Saloon\Http\Response;

final class GetTextshotResponse
{
public static function fromResponse(Response $response): Textshot
{
event(new DocuWareResponseLog($response));

EnsureValidResponse::from($response);

return Textshot::fromJson($response->throw()->json());
}
}
36 changes: 36 additions & 0 deletions tests/Feature/Requests/Documents/Sections/GetTextshotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use CodebarAg\DocuWare\Events\DocuWareResponseLog;
use CodebarAg\DocuWare\Requests\Documents\Sections\GetAllSectionsFromADocument;
use CodebarAg\DocuWare\Requests\Documents\Sections\GetTextshot;
use CodebarAg\DocuWare\Requests\FileCabinets\Upload\CreateDataRecord;
use Illuminate\Support\Facades\Event;

it('get textshot for a specific section', function () {
Event::fake();

$fileCabinetId = config('laravel-docuware.tests.file_cabinet_id');
$sectionId = '15850-15497';

$document = $this->connector->send(new CreateDataRecord(
$fileCabinetId,
'::fake-file-content::',
'example.txt'
))->dto();

$sections = $this->connector->send(new GetAllSectionsFromADocument(
$fileCabinetId,
$document->id
))->dto();

$textshot = $this->connector->send(new GetTextshot(
$fileCabinetId,
$sections->first()->id,
))->dto();

expect($textshot->page_count)->toBe(1);
expect($textshot->pages->first()->content)->toBe(':: fake - file - content ::');

Event::assertDispatched(DocuWareResponseLog::class);

})->group('requests', 'sections', 'textshot');
1 change: 1 addition & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use CodebarAg\DocuWare\Requests\General\UserManagement\GetUsers\GetUsers;
use CodebarAg\DocuWare\Tests\TestCase;
use Illuminate\Support\Sleep;
use Illuminate\Support\Str;

uses(TestCase::class)
->in(__DIR__);
Expand Down