Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2485091
EmailPropertyTrait.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
EmailPropertyTrait.php
View Options
<?php
namespace
App\Traits
;
trait
EmailPropertyTrait
{
/** @var ?string Domain name for the to-be-created object */
public
$domainName
;
/**
* Boot function from Laravel.
*/
protected
static
function
bootEmailPropertyTrait
()
{
static
::
creating
(
function
(
$model
)
{
if
(
empty
(
$model
->
email
)
&&
defined
(
'static::EMAIL_TEMPLATE'
))
{
$template
=
static
::
EMAIL_TEMPLATE
;
// @phpstan-ignore-line
$defaults
=
[
'type'
=>
'mail'
,
];
foreach
([
'id'
,
'domainName'
,
'type'
]
as
$prop
)
{
if
(
strpos
(
$template
,
"{{$prop}}"
)
===
false
)
{
continue
;
}
$value
=
$model
->{
$prop
}
??
(
$defaults
[
$prop
]
??
''
);
if
(
$value
===
''
||
$value
===
null
)
{
throw
new
\Exception
(
"Missing '{$prop}' property for "
.
static
::
class
);
}
$template
=
str_replace
(
"{{$prop}}"
,
$value
,
$template
);
}
$model
->
email
=
strtolower
(
$template
);
}
});
}
/**
* Returns the object's domain (including soft-deleted).
*
* @return ?\App\Domain The domain to which the object belongs to, NULL if it does not exist
*/
public
function
domain
():
?
\App\Domain
{
if
(
empty
(
$this
->
email
)
&&
isset
(
$this
->
domainName
))
{
$domainName
=
$this
->
domainName
;
}
elseif
(
strpos
(
$this
->
email
,
'@'
))
{
list
(
$local
,
$domainName
)
=
explode
(
'@'
,
$this
->
email
);
}
else
{
return
null
;
}
return
\App\Domain
::
withTrashed
()->
where
(
'namespace'
,
$domainName
)->
first
();
}
/**
* Find whether an email address exists as a model object (including soft-deleted).
*
* @param string $email Email address
* @param bool $return_object Return model instance instead of a boolean
*
* @return static|bool True or Model object if found, False otherwise
*/
public
static
function
emailExists
(
string
$email
,
bool
$return_object
=
false
)
{
if
(
strpos
(
$email
,
'@'
)
===
false
)
{
return
false
;
}
$email
=
\strtolower
(
$email
);
$object
=
static
::
withTrashed
()->
where
(
'email'
,
$email
)->
first
();
if
(
$object
)
{
return
$return_object
?
$object
:
true
;
}
return
false
;
}
/**
* Ensure the email is appropriately cased.
*
* @param string $email Email address
*/
public
function
setEmailAttribute
(
string
$email
):
void
{
$this
->
attributes
[
'email'
]
=
strtolower
(
$email
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Thu, Nov 20, 10:44 PM (23 h, 22 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
387321
Default Alt Text
EmailPropertyTrait.php (2 KB)
Attached To
Mode
R2 kolab
Attached
Detach File
Event Timeline
Log In to Comment