initial commit
This commit is contained in:
160
src/app/Models/Address.php
Normal file
160
src/app/Models/Address.php
Normal file
@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Traits\FormattedAddressTrait;
|
||||
use App\Models\Traits\HasUidTrait;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Prunable;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
|
||||
class Address extends Model
|
||||
{
|
||||
use FormattedAddressTrait;
|
||||
use HasFactory;
|
||||
use HasUidTrait;
|
||||
use Prunable;
|
||||
|
||||
/** @var string */
|
||||
protected $table = 'addresses';
|
||||
|
||||
/** @var string */
|
||||
protected $keyType = 'string';
|
||||
|
||||
/** @var bool */
|
||||
public $incrementing = false;
|
||||
|
||||
/** @var array */
|
||||
protected $fillable = [
|
||||
'addressable_type',
|
||||
'addressable_id',
|
||||
'team_id',
|
||||
'street',
|
||||
'unit',
|
||||
'city',
|
||||
'state',
|
||||
'postal_code',
|
||||
'country',
|
||||
];
|
||||
|
||||
/** @var array */
|
||||
protected $hidden = [];
|
||||
|
||||
/** @var array */
|
||||
protected $casts = [
|
||||
'is_primary' => 'boolean',
|
||||
];
|
||||
|
||||
/** @var array */
|
||||
protected $dates = [];
|
||||
|
||||
/** @var array */
|
||||
protected $appends = [];
|
||||
|
||||
/** @var array */
|
||||
protected $touches = [];
|
||||
|
||||
/** @var array */
|
||||
protected $dispatchesEvents = [];
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Class Constants
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
|
||||
//
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Custom/Private Methods
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get the prunable model query.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function prunable(): Builder
|
||||
{
|
||||
//return static::where('created_at', '<=', now()->subMonth());
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the model for pruning.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function pruning(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Accessors
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
|
||||
//
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Mutators
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
|
||||
//
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Scopes
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
|
||||
/**
|
||||
* Enforce that a given model belongs to a Team.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param int|string $teamId
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeOnTeam($query, $teamId): Builder
|
||||
{
|
||||
return $query->where('team_id', $teamId);
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Relationships
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
|
||||
/**
|
||||
* Morphable relationship method.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
||||
*/
|
||||
public function addressable(): MorphTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
}
|
35
src/app/Models/Traits/FormattedAddressTrait.php
Normal file
35
src/app/Models/Traits/FormattedAddressTrait.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
trait FormattedAddressTrait
|
||||
{
|
||||
/**
|
||||
* Combine the street and optionally a street suffix
|
||||
* e.g. Apt 1
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAddressStreetAttribute(): string
|
||||
{
|
||||
$street = $this->street;
|
||||
if (! empty($this->unit)) {
|
||||
$street .= " {$this->unit}";
|
||||
}
|
||||
return $street;
|
||||
}
|
||||
|
||||
/**
|
||||
* Combine the city and state together as a single line
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCityStateAttribute(): string
|
||||
{
|
||||
return "{$this->city}, {$this->state}";
|
||||
}
|
||||
}
|
171
src/app/Models/Traits/FormattedDateTrait.php
Normal file
171
src/app/Models/Traits/FormattedDateTrait.php
Normal file
@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
trait FormattedDateTrait
|
||||
{
|
||||
/**
|
||||
* Return the created at datetime as mon D, year.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $dateTimeString
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCreatedAtDateShortAttribute(): string
|
||||
{
|
||||
return $this->created_at->format('M j, Y');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the created at datetime as month day, year.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $dateTimeString
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCreatedAtDateFullAttribute(): string
|
||||
{
|
||||
return $this->created_at->format('F jS, Y');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the created at datetime as month day, year hour:minute meridian.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $dateTimeString
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCreatedAtFullAttribute(): string
|
||||
{
|
||||
return $this->created_at->format('F jS, Y g:i a');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the updated at datetime as mon day, year.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $dateTimeString
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUpdatedAtDateShortAttribute(): string
|
||||
{
|
||||
if (empty($this->updated_at)) {
|
||||
return '--';
|
||||
}
|
||||
return $this->updated_at->format('M j, Y');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the updated at datetime as mon day, year.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $dateTimeString
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUpdatedAtDateFullAttribute(): string
|
||||
{
|
||||
if (empty($this->updated_at)) {
|
||||
return '--';
|
||||
}
|
||||
return $this->updated_at->format('F jS, Y');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the updated at datetime as mon day, year hour:minute meridian.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $dateTimeString
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUpdatedAtFullAttribute(): string
|
||||
{
|
||||
if (empty($this->updated_at)) {
|
||||
return '--';
|
||||
}
|
||||
return $this->updated_at->format('F jS, Y g:i a');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a datetime string as YYYY-MM-DD.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $dateTimeString
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function dateStr(string $dateTimeString): string
|
||||
{
|
||||
return Carbon::parse($dateTimeString)->format('Y-m-d');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a datetime string as mon d, year.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $dateTimeString
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function dateShort(string $dateTimeString): string
|
||||
{
|
||||
return Carbon::parse($dateTimeString)->format('M j, Y');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a datetime string as month day, year.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $dateTimeString
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function dateFull(string $dateTimeString): string
|
||||
{
|
||||
return Carbon::parse($dateTimeString)->format('F jS, Y');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a datetime string as HH:MM (military time)
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $dateTimeString
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function timeShort(string $dateTimeString): string
|
||||
{
|
||||
return Carbon::parse($dateTimeString)->format('H:i');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a datetime string as hour:minute meridian
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $dateTimeString
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function timeFull(string $dateTimeString): string
|
||||
{
|
||||
return Carbon::parse($dateTimeString)->format('g:i a');
|
||||
}
|
||||
}
|
55
src/app/Models/Traits/FormattedPhoneTrait.php
Normal file
55
src/app/Models/Traits/FormattedPhoneTrait.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
trait FormattedPhoneTrait
|
||||
{
|
||||
/**
|
||||
* Format a phone number to be human readable.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPhoneNumberAttribute(): string
|
||||
{
|
||||
$phoneLength = strlen($this->phone);
|
||||
$phoneNumber = preg_replace('//', '', $this->phone);
|
||||
|
||||
if ($phoneLength > 10) {
|
||||
$countryCode = substr($phoneNumber, 0, $phoneLength - 10);
|
||||
$areaCode = substr($phoneNumber, -10, 3);
|
||||
$nextThree = substr($phoneNumber, -7, 3);
|
||||
$lastFour = substr($phoneNumber, -4, 4);
|
||||
|
||||
$phoneNumber = "({$areaCode}) {$nextThree}-{$lastFour}";
|
||||
} elseif ($phoneLength == 10) {
|
||||
$areaCode = substr($phoneNumber, 0, 3);
|
||||
$nextThree = substr($phoneNumber, 3, 3);
|
||||
$lastFour = substr($phoneNumber, 6, 4);
|
||||
|
||||
$phoneNumber = "({$areaCode}) {$nextThree}-{$lastFour}";
|
||||
} elseif ($phoneLength == 7) {
|
||||
$nextThree = substr($phoneNumber, 0, 3);
|
||||
$lastFour = substr($phoneNumber, 3, 4);
|
||||
|
||||
$phoneNumber = "{$nextThree}-{$lastFour}";
|
||||
}
|
||||
|
||||
return $phoneNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all non-numeric characters from the phone number.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setPhoneNumberAttribute($value): void
|
||||
{
|
||||
$this->attributes['phone'] = preg_replace('/[^0-9]/', '', $value);
|
||||
}
|
||||
}
|
44
src/app/Models/Traits/HasUidTrait.php
Normal file
44
src/app/Models/Traits/HasUidTrait.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
trait HasUidTrait
|
||||
{
|
||||
/**
|
||||
* Ensure that when a model is saving, a unique ID
|
||||
* is set for the model.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function bootHasUidTrait(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize logic.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function initializeHasUidTrait(): void
|
||||
{
|
||||
$this->id = $this->generateUid();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a cryptographically safe unique ID.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generateUid(): string
|
||||
{
|
||||
$bytes = openssl_random_pseudo_bytes(env('APP_UID_BYTES', 8));
|
||||
return bin2hex($bytes);
|
||||
}
|
||||
}
|
182
src/app/Models/User.php
Normal file
182
src/app/Models/User.php
Normal file
@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Traits\HasUidTrait;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Prunable;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
use Laravel\Jetstream\HasProfilePhoto;
|
||||
use Laravel\Jetstream\HasTeams;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasApiTokens;
|
||||
use HasFactory;
|
||||
use HasProfilePhoto;
|
||||
use HasTeams;
|
||||
use HasUidTrait;
|
||||
use Notifiable;
|
||||
use Prunable;
|
||||
use TwoFactorAuthenticatable;
|
||||
|
||||
/** @var string */
|
||||
protected $table = 'users';
|
||||
|
||||
/** @var string */
|
||||
protected $keyType = 'string';
|
||||
|
||||
/** @var bool */
|
||||
public $incrementing = false;
|
||||
|
||||
/** @var array */
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'surname',
|
||||
'timezone_name',
|
||||
'current_team_id',
|
||||
'profile_photo_path',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/** @var array */
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
'two_factor_recovery_codes',
|
||||
'two_factor_secret',
|
||||
];
|
||||
|
||||
/** @var array */
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
];
|
||||
|
||||
/** @var array */
|
||||
protected $dates = [];
|
||||
|
||||
/** @var array */
|
||||
protected $appends = [
|
||||
'full_name',
|
||||
'name_full',
|
||||
'profile_photo_url',
|
||||
];
|
||||
|
||||
/** @var array */
|
||||
protected $touches = [];
|
||||
|
||||
/** @var array */
|
||||
protected $dispatchesEvents = [];
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Class Constants
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
|
||||
//
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Custom/Private Methods
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get the prunable model query.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function prunable()//: Builder
|
||||
{
|
||||
//return static::where('created_at', '<=', now()->subMonth());
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the model for pruning.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function pruning(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Accessors
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
|
||||
/**
|
||||
* Return the surname then (first)name separated by a comma.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFullNameAttribute(): string
|
||||
{
|
||||
return "{$this->surname}, {$this->name}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return both the (first)name and surname.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNameFullAttribute(): string
|
||||
{
|
||||
return "{$this->name} {$this->surname}";
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Mutators
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
|
||||
//
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Scopes
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
|
||||
//
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Relationships
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
*/
|
||||
|
||||
/**
|
||||
* Return the associated Address with a User.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
|
||||
*/
|
||||
public function addresses(): MorphOne
|
||||
{
|
||||
return $this->morphOne(Address::class, 'addressable');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user