Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
6.67% |
1 / 15 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| OwnerResolver | |
6.67% |
1 / 15 |
|
25.00% |
1 / 4 |
109.38 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| asset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| nodePath | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| localize | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Richtext; |
| 6 | |
| 7 | use Cosray\Assets\Asset; |
| 8 | use Cosray\Field\Field; |
| 9 | use Cosray\Field\Owner; |
| 10 | |
| 11 | /** |
| 12 | * Production resolver backed by a field owner: assets from the |
| 13 | * catalog repository, node paths from `url_paths`, locale maps |
| 14 | * resolved along the owner's locale fallback chain. |
| 15 | */ |
| 16 | final class OwnerResolver implements Resolver |
| 17 | { |
| 18 | public function __construct( |
| 19 | private readonly Owner $owner, |
| 20 | ) {} |
| 21 | |
| 22 | public function asset(string $uid): ?Asset |
| 23 | { |
| 24 | return $uid === '' ? null : $this->owner->assets()->get($uid); |
| 25 | } |
| 26 | |
| 27 | public function nodePath(string $uid): ?string |
| 28 | { |
| 29 | $map = $this->owner->paths()->map($uid); |
| 30 | |
| 31 | if ($map === []) { |
| 32 | return null; |
| 33 | } |
| 34 | |
| 35 | $path = $this->localize($map); |
| 36 | |
| 37 | return is_string($path) ? $path : null; |
| 38 | } |
| 39 | |
| 40 | public function localize(array $map): mixed |
| 41 | { |
| 42 | $locale = $this->owner->locale(); |
| 43 | |
| 44 | while ($locale) { |
| 45 | if ( |
| 46 | ($map[$locale->id] ?? null) !== null |
| 47 | && $map[$locale->id] !== '' |
| 48 | && $map[$locale->id] !== [] |
| 49 | ) { |
| 50 | return $map[$locale->id]; |
| 51 | } |
| 52 | |
| 53 | $locale = $locale->fallback(); |
| 54 | } |
| 55 | |
| 56 | return $map[Field::NEUTRAL_LOCALE] ?? null; |
| 57 | } |
| 58 | } |