initial commit
This commit is contained in:
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user