-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from codebar-ag/feature-textshot
Feature Textshot
- Loading branch information
Showing
11 changed files
with
198 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(' '); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
tests/Feature/Requests/Documents/Sections/GetTextshotTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters