Page MenuHomePhorge

EmailPropertyTrait.php
No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None

EmailPropertyTrait.php

<?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

Mime Type
text/x-php
Expires
Thu, Nov 20, 10:44 PM (1 d, 1 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
387321
Default Alt Text
EmailPropertyTrait.php (2 KB)

Event Timeline