|
| 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 | +} |
0 commit comments