Skip to content

Commit 98c0b02

Browse files
authored
Add AsUri model cast. (#55909)
1 parent d3e8791 commit 98c0b02

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Eloquent\Casts;
4+
5+
use Illuminate\Contracts\Database\Eloquent\Castable;
6+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
7+
use Illuminate\Support\Uri;
8+
9+
class AsUri implements Castable
10+
{
11+
/**
12+
* Get the caster class to use when casting from / to this cast target.
13+
*
14+
* @param array $arguments
15+
* @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Support\Uri, string|Uri>
16+
*/
17+
public static function castUsing(array $arguments)
18+
{
19+
return new class implements CastsAttributes
20+
{
21+
public function get($model, $key, $value, $attributes)
22+
{
23+
return isset($value) ? new Uri($value) : null;
24+
}
25+
26+
public function set($model, $key, $value, $attributes)
27+
{
28+
return isset($value) ? (string) $value : null;
29+
}
30+
};
31+
}
32+
}

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
2929
use Illuminate\Database\Eloquent\Casts\AsHtmlString;
3030
use Illuminate\Database\Eloquent\Casts\AsStringable;
31+
use Illuminate\Database\Eloquent\Casts\AsUri;
3132
use Illuminate\Database\Eloquent\Casts\Attribute;
3233
use Illuminate\Database\Eloquent\Collection;
3334
use Illuminate\Database\Eloquent\Concerns\HasUlids;
@@ -49,6 +50,7 @@
4950
use Illuminate\Support\HtmlString;
5051
use Illuminate\Support\InteractsWithTime;
5152
use Illuminate\Support\Stringable;
53+
use Illuminate\Support\Uri;
5254
use InvalidArgumentException;
5355
use LogicException;
5456
use Mockery as m;
@@ -284,6 +286,24 @@ public function testDirtyOnCastedHtmlString()
284286
$this->assertTrue($model->isDirty('asHtmlStringAttribute'));
285287
}
286288

289+
public function testDirtyOnCastedUri()
290+
{
291+
$model = new EloquentModelCastingStub;
292+
$model->setRawAttributes([
293+
'asUriAttribute' => 'https://www.example.com:1234?query=param&another=value',
294+
]);
295+
$model->syncOriginal();
296+
297+
$this->assertInstanceOf(Uri::class, $model->asUriAttribute);
298+
$this->assertFalse($model->isDirty('asUriAttribute'));
299+
300+
$model->asUriAttribute = new Uri('https://www.example.com:1234?query=param&another=value');
301+
$this->assertFalse($model->isDirty('asUriAttribute'));
302+
303+
$model->asUriAttribute = new Uri('https://www.updated.com:1234?query=param&another=value');
304+
$this->assertTrue($model->isDirty('asUriAttribute'));
305+
}
306+
287307
// public function testDirtyOnCastedEncryptedCollection()
288308
// {
289309
// $this->encrypter = m::mock(Encrypter::class);
@@ -3733,6 +3753,7 @@ protected function casts(): array
37333753
'asarrayobjectAttribute' => AsArrayObject::class,
37343754
'asStringableAttribute' => AsStringable::class,
37353755
'asHtmlStringAttribute' => AsHtmlString::class,
3756+
'asUriAttribute' => AsUri::class,
37363757
'asCustomCollectionAttribute' => AsCollection::using(CustomCollection::class),
37373758
'asEncryptedArrayObjectAttribute' => AsEncryptedArrayObject::class,
37383759
'asEncryptedCustomCollectionAttribute' => AsEncryptedCollection::using(CustomCollection::class),

0 commit comments

Comments
 (0)