Skip to content

Commit bb501d7

Browse files
feat: Handle origin to internal link
1 parent 685855b commit bb501d7

3 files changed

Lines changed: 46 additions & 5 deletions

File tree

src/models/Origin.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Origin
1212
{
1313
public readonly string $value;
1414

15-
public readonly User|Collection|null $model;
15+
public readonly User|Link|Collection|null $model;
1616

1717
public readonly string $label;
1818

@@ -29,12 +29,14 @@ public function __construct(string $value)
2929
/**
3030
* Return the model (User or Collection) matching with the value if any.
3131
*/
32-
private function modelFromValue(): User|Collection|null
32+
private function modelFromValue(): User|Link|Collection|null
3333
{
3434
list($origin_type, $origin_id) = utils\OriginHelper::extractFromPath($this->value);
3535

3636
if ($origin_type === 'user' && $origin_id) {
3737
return User::find($origin_id);
38+
} elseif ($origin_type === 'link' && $origin_id) {
39+
return Link::find($origin_id);
3840
} elseif ($origin_type === 'collection' && $origin_id) {
3941
return Collection::find($origin_id);
4042
} else {
@@ -59,6 +61,8 @@ private function labelFromValue(): string
5961

6062
if ($this->model instanceof User) {
6163
return $this->model->username;
64+
} elseif ($this->model instanceof Link) {
65+
return $this->model->title;
6266
} elseif ($this->model instanceof Collection) {
6367
return $this->model->name();
6468
} else {
@@ -77,6 +81,8 @@ private function urlFromValue(): string
7781

7882
if ($this->model instanceof User) {
7983
return \Minz\Url::absoluteFor('profile', ['id' => $this->model->id]);
84+
} elseif ($this->model instanceof Link) {
85+
return \Minz\Url::absoluteFor('link', ['id' => $this->model->id]);
8086
} elseif ($this->model instanceof Collection) {
8187
return \Minz\Url::absoluteFor('collection', ['id' => $this->model->id]);
8288
} else {

src/utils/OriginHelper.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ class OriginHelper
1717
*
1818
* - For the path `/collections/1234567890`, ['collection', '1234567890']
1919
* will be returned (if the collection exists in db)
20-
* - For the path `/p/1234567890`, ['user', '1234567890'] will be
21-
* returned (if the user exists in db)
20+
* - For the path `/links/1234567890`, ['link', '1234567890'] will be
21+
* returned (if the link exists in db)
22+
* - For the path `/p/1234567890`, ['user', '1234567890'] will be returned
23+
* (if the user exists in db)
2224
* - For other paths, ['', null] will be returned
2325
*
2426
* The method also handles URLs starting with the base URL of the application.
2527
*
26-
* @return array{'collection'|'user', string}|array{'', null}
28+
* @return array{'collection'|'link'|'user', string}|array{'', null}
2729
* }
2830
*/
2931
public static function extractFromPath(string $url_or_path): array
@@ -54,6 +56,17 @@ public static function extractFromPath(string $url_or_path): array
5456
return ['collection', $collection_id];
5557
}
5658

59+
$result = preg_match('#^/links/(?P<id>\d+)$#', $path, $matches);
60+
if (isset($matches['id'])) {
61+
$link_id = $matches['id'];
62+
63+
if (!models\Link::exists($link_id)) {
64+
return ['', null];
65+
}
66+
67+
return ['link', $link_id];
68+
}
69+
5770
$result = preg_match('#^/p/(?P<id>\d+)/?#', $path, $matches);
5871
if (isset($matches['id'])) {
5972
$user_id = $matches['id'];

tests/utils/OriginHelperTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\utils;
44

55
use tests\factories\CollectionFactory;
6+
use tests\factories\LinkFactory;
67
use tests\factories\UserFactory;
78

89
class OriginHelperTest extends \PHPUnit\Framework\TestCase
@@ -43,6 +44,17 @@ public function testExtractOriginFromPathWithExistingUser(): void
4344
$this->assertSame($user->id, $origin_id);
4445
}
4546

47+
public function testExtractOriginFromPathWithExistingLink(): void
48+
{
49+
$link = LinkFactory::create();
50+
$path = \Minz\Url::for('link', ['id' => $link->id]);
51+
52+
list($origin_type, $origin_id) = OriginHelper::extractFromPath($path);
53+
54+
$this->assertSame('link', $origin_type);
55+
$this->assertSame($link->id, $origin_id);
56+
}
57+
4658
public function testExtractOriginFromPathWithNonExistingCollection(): void
4759
{
4860
$path = \Minz\Url::for('collection', ['id' => '12345']);
@@ -63,6 +75,16 @@ public function testExtractOriginFromPathWithNonExistingUser(): void
6375
$this->assertNull($origin_id);
6476
}
6577

78+
public function testExtractOriginFromPathWithNonExistingLink(): void
79+
{
80+
$path = \Minz\Url::for('link', ['id' => '12345']);
81+
82+
list($origin_type, $origin_id) = OriginHelper::extractFromPath($path);
83+
84+
$this->assertSame('', $origin_type);
85+
$this->assertNull($origin_id);
86+
}
87+
6688
public function testExtractOriginFromPathWithUnsupportedPath(): void
6789
{
6890
$path = \Minz\Url::for('bookmarks');

0 commit comments

Comments
 (0)