Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.43% |
5 / 7 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UrlPaths | |
71.43% |
5 / 7 |
|
50.00% |
1 / 2 |
4.37 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| map | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Node; |
| 6 | |
| 7 | use Celema\Quma\Database; |
| 8 | |
| 9 | /** |
| 10 | * Resolves node uids to their active URL paths. Used by the richtext |
| 11 | * renderer to turn `link.node` references into hrefs at render time. |
| 12 | */ |
| 13 | final class UrlPaths |
| 14 | { |
| 15 | /** @var array<string, array<string, string>> */ |
| 16 | private array $cache = []; |
| 17 | |
| 18 | public function __construct( |
| 19 | private readonly Database $db, |
| 20 | ) {} |
| 21 | |
| 22 | /** @return array<string, string> Locale id to active path. */ |
| 23 | public function map(string $uid): array |
| 24 | { |
| 25 | if (isset($this->cache[$uid])) { |
| 26 | return $this->cache[$uid]; |
| 27 | } |
| 28 | |
| 29 | $map = []; |
| 30 | |
| 31 | foreach ($this->db->paths->activeByNodeUid(['uid' => $uid])->all() as $row) { |
| 32 | $map[(string) $row['locale']] = (string) $row['path']; |
| 33 | } |
| 34 | |
| 35 | return $this->cache[$uid] = $map; |
| 36 | } |
| 37 | } |