Skip to content

Commit fecfb38

Browse files
authored
Merge pull request #169 from codebar-ag/feature-textshot
Feature Textshot
2 parents 71cad3d + de193a1 commit fecfb38

File tree

11 files changed

+198
-7
lines changed

11 files changed

+198
-7
lines changed

.github/workflows/run-tests.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ jobs:
4444
- name: Execute tests
4545
run: cp phpunit.xml.dist phpunit.xml
4646

47-
- name: Migrate Config
48-
run: vendor/bin/pest --migrate-configuration
49-
5047
- name: Execute tests
5148
run: vendor/bin/pest
5249
env:

.phpunit.cache/test-results

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ then optimize the processes that power the core of your business.
139139
| Documents/Sections | Get All Sections from a Document || |
140140
| Documents/Sections | Get a Specific Section || |
141141
| Documents/Sections | Delete Section || |
142+
| Documents/Sections/Textshot | Get Textshot for a Specific Section || |
142143
| Documents/Download | Download Document || |
143144
| Documents/Download | Download Section || |
144145
| Documents/Download | Download Thumbnail || |

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"license": "MIT",
1414
"authors": [
1515
{
16-
"name": "Sebastian Bürgin",
16+
"name": "Sebastian Bürgin-Fix",
1717
"email": "[email protected]",
1818
"homepage": "https://www.codebar.ch",
1919
"role": "Sofware-Engineer"
@@ -41,7 +41,8 @@
4141
"phpstan/extension-installer": "^1.3",
4242
"phpstan/phpstan-deprecation-rules": "^1.1",
4343
"phpstan/phpstan-phpunit": "^1.3",
44-
"spatie/laravel-ray": "^1.33"
44+
"phpunit/phpunit": "^10.5",
45+
"spatie/laravel-ray": "^1.35"
4546
},
4647
"autoload": {
4748
"psr-4": {

docs/Documents/sections.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Sections
2+
23
| Request | Supported |
34
|----------------------------------|-----------|
45
| Get All Sections from a Document ||
56
| Get a Specific Section ||
67
| Delete Section ||
7-
8+
| Get Textshot ||
89

910
### Get All Sections
11+
1012
```php
1113
use CodebarAg\DocuWare\Requests\Documents\Sections\GetAllSectionsFromADocument;
1214

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

1921
### Get Specific Section
22+
2023
```php
2124
use CodebarAg\DocuWare\Requests\Documents\Sections\GetASpecificSection;
2225

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

2932
### Delete Section
33+
3034
```php
3135
use CodebarAg\DocuWare\Requests\Documents\Sections\DeleteSection;
3236

@@ -35,3 +39,14 @@ $deleted = $connector->send(new DeleteSection(
3539
$sectionId
3640
))->dto();
3741
```
42+
43+
### Get Textshot
44+
45+
```php
46+
use CodebarAg\DocuWare\Requests\Documents\Sections\GetTextshot;
47+
48+
$deleted = $connector->send(new GetTextshot(
49+
$fileCabinetId,
50+
$sectionId
51+
))->dto();
52+
```

src/DTO/Textshot.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace CodebarAg\DocuWare\DTO;
4+
5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Collection;
7+
8+
final class Textshot
9+
{
10+
public static function fromJson(array $data): self
11+
{
12+
$pages = collect(Arr::get($data, 'Pages', []));
13+
14+
return new self(
15+
page_count: $pages->count(),
16+
pages: TextshotPage::fromCollection($pages),
17+
);
18+
}
19+
20+
public function __construct(
21+
public int $page_count,
22+
public Collection $pages,
23+
) {}
24+
}

src/DTO/TextshotPage.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace CodebarAg\DocuWare\DTO;
4+
5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Collection;
7+
8+
final class TextshotPage
9+
{
10+
public static function fromCollection(Collection $collection): Collection
11+
{
12+
return $collection->map(fn (array $data) => self::fromJson($data));
13+
}
14+
15+
public static function fromJson(array $data): self
16+
{
17+
$rawItems = Arr::get($data, 'Items', []);
18+
19+
return new self(
20+
language: Arr::get($data, 'Lang'),
21+
content: self::content($rawItems)
22+
);
23+
}
24+
25+
public function __construct(
26+
public string $language,
27+
public string $content,
28+
) {}
29+
30+
protected static function content(array $rawItems): string
31+
{
32+
return collect($rawItems)
33+
->filter(function ($item) {
34+
return Arr::get($item, '$type') === 'TextZone';
35+
})
36+
->pluck('Ln')
37+
->flatten(2)
38+
->filter(function ($item) {
39+
return is_array($item);
40+
})
41+
->flatten(1)
42+
->map(function ($item) {
43+
44+
$type = Arr::get($item, '$type');
45+
46+
return match ($type) {
47+
'Word' => Arr::get($item, 'Value'),
48+
default => null,
49+
};
50+
})->implode(' ');
51+
}
52+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace CodebarAg\DocuWare\Requests\Documents\Sections;
4+
5+
use CodebarAg\DocuWare\Responses\Documents\Sections\GetTextshotResponse;
6+
use Illuminate\Support\Facades\Cache;
7+
use Saloon\CachePlugin\Contracts\Cacheable;
8+
use Saloon\CachePlugin\Drivers\LaravelCacheDriver;
9+
use Saloon\CachePlugin\Traits\HasCaching;
10+
use Saloon\Enums\Method;
11+
use Saloon\Http\Request;
12+
use Saloon\Http\Response;
13+
14+
class GetTextshot extends Request implements Cacheable
15+
{
16+
use HasCaching;
17+
18+
protected Method $method = Method::GET;
19+
20+
public function __construct(
21+
protected readonly string $fileCabinetId,
22+
protected readonly string $sectionId,
23+
) {}
24+
25+
public function resolveEndpoint(): string
26+
{
27+
return '/FileCabinets/'.$this->fileCabinetId.'/Sections/'.$this->sectionId.'/Textshot';
28+
}
29+
30+
public function resolveCacheDriver(): LaravelCacheDriver
31+
{
32+
return new LaravelCacheDriver(Cache::store(config('laravel-docuware.configurations.cache.driver')));
33+
}
34+
35+
public function cacheExpiryInSeconds(): int
36+
{
37+
return config('laravel-docuware.configurations.cache.lifetime_in_seconds', 3600);
38+
}
39+
40+
public function createDtoFromResponse(Response $response): mixed
41+
{
42+
return GetTextshotResponse::fromResponse($response);
43+
}
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace CodebarAg\DocuWare\Responses\Documents\Sections;
4+
5+
use CodebarAg\DocuWare\DTO\Textshot;
6+
use CodebarAg\DocuWare\Events\DocuWareResponseLog;
7+
use CodebarAg\DocuWare\Support\EnsureValidResponse;
8+
use Saloon\Http\Response;
9+
10+
final class GetTextshotResponse
11+
{
12+
public static function fromResponse(Response $response): Textshot
13+
{
14+
event(new DocuWareResponseLog($response));
15+
16+
EnsureValidResponse::from($response);
17+
18+
return Textshot::fromJson($response->throw()->json());
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use CodebarAg\DocuWare\Events\DocuWareResponseLog;
4+
use CodebarAg\DocuWare\Requests\Documents\Sections\GetAllSectionsFromADocument;
5+
use CodebarAg\DocuWare\Requests\Documents\Sections\GetTextshot;
6+
use CodebarAg\DocuWare\Requests\FileCabinets\Upload\CreateDataRecord;
7+
use Illuminate\Support\Facades\Event;
8+
9+
it('get textshot for a specific section', function () {
10+
Event::fake();
11+
12+
$fileCabinetId = config('laravel-docuware.tests.file_cabinet_id');
13+
$sectionId = '15850-15497';
14+
15+
$document = $this->connector->send(new CreateDataRecord(
16+
$fileCabinetId,
17+
'::fake-file-content::',
18+
'example.txt'
19+
))->dto();
20+
21+
$sections = $this->connector->send(new GetAllSectionsFromADocument(
22+
$fileCabinetId,
23+
$document->id
24+
))->dto();
25+
26+
$textshot = $this->connector->send(new GetTextshot(
27+
$fileCabinetId,
28+
$sections->first()->id,
29+
))->dto();
30+
31+
expect($textshot->page_count)->toBe(1);
32+
expect($textshot->pages->first()->content)->toBe(':: fake - file - content ::');
33+
34+
Event::assertDispatched(DocuWareResponseLog::class);
35+
36+
})->group('requests', 'sections', 'textshot');

0 commit comments

Comments
 (0)