Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[12.x] Add AsUri model cast #55909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions 32 src/Illuminate/Database/Eloquent/Casts/AsUri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Illuminate\Database\Eloquent\Casts;

use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Support\Uri;

class AsUri implements Castable
{
/**
* Get the caster class to use when casting from / to this cast target.
*
* @param array $arguments
* @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Support\Uri, string|Uri>
*/
public static function castUsing(array $arguments)
{
return new class implements CastsAttributes
{
public function get($model, $key, $value, $attributes)
{
return isset($value) ? new Uri($value) : null;
}

public function set($model, $key, $value, $attributes)
{
return isset($value) ? (string) $value : null;
}
};
}
}
21 changes: 21 additions & 0 deletions 21 tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
use Illuminate\Database\Eloquent\Casts\AsHtmlString;
use Illuminate\Database\Eloquent\Casts\AsStringable;
use Illuminate\Database\Eloquent\Casts\AsUri;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
Expand All @@ -49,6 +50,7 @@
use Illuminate\Support\HtmlString;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Support\Stringable;
use Illuminate\Support\Uri;
use InvalidArgumentException;
use LogicException;
use Mockery as m;
Expand Down Expand Up @@ -284,6 +286,24 @@ public function testDirtyOnCastedHtmlString()
$this->assertTrue($model->isDirty('asHtmlStringAttribute'));
}

public function testDirtyOnCastedUri()
{
$model = new EloquentModelCastingStub;
$model->setRawAttributes([
'asUriAttribute' => 'https://www.example.com:1234?query=param&another=value',
]);
$model->syncOriginal();

$this->assertInstanceOf(Uri::class, $model->asUriAttribute);
$this->assertFalse($model->isDirty('asUriAttribute'));

$model->asUriAttribute = new Uri('https://www.example.com:1234?query=param&another=value');
$this->assertFalse($model->isDirty('asUriAttribute'));

$model->asUriAttribute = new Uri('https://www.updated.com:1234?query=param&another=value');
$this->assertTrue($model->isDirty('asUriAttribute'));
}

// public function testDirtyOnCastedEncryptedCollection()
// {
// $this->encrypter = m::mock(Encrypter::class);
Expand Down Expand Up @@ -3733,6 +3753,7 @@ protected function casts(): array
'asarrayobjectAttribute' => AsArrayObject::class,
'asStringableAttribute' => AsStringable::class,
'asHtmlStringAttribute' => AsHtmlString::class,
'asUriAttribute' => AsUri::class,
'asCustomCollectionAttribute' => AsCollection::using(CustomCollection::class),
'asEncryptedArrayObjectAttribute' => AsEncryptedArrayObject::class,
'asEncryptedCustomCollectionAttribute' => AsEncryptedCollection::using(CustomCollection::class),
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.