short-url/app/UrlAddress.php

43 lines
1.1 KiB
PHP
Raw Permalink Normal View History

2020-07-01 08:57:45 +00:00
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
2020-07-02 14:40:49 +00:00
use Illuminate\Support\Carbon;
2020-07-01 08:57:45 +00:00
class UrlAddress extends Model
{
2020-07-02 14:40:49 +00:00
protected $appends = [
'is_expired',
'full_url_destination'
];
public function getFullUrlDestinationAttribute()
{
$httpPath = "http://";
$httpsPath = "https://";
$hasHttpOrHttps = false;
$url = $this['url_destination'];
if ($httpPath == substr($url, 0, strlen($httpPath))) {
$hasHttpOrHttps = true;
} else if ($httpsPath == substr($url, 0, strlen($httpsPath))) {
$hasHttpOrHttps = true;
}
return ($hasHttpOrHttps ? "" : "http://") . $this['url_destination'];
}
public function getIsExpiredAttribute()
{
$urlExpired = false;
if ($this['date_expired'] != null) {
$today = Carbon::now()->setTime(0, 0, 0);
$urlExpiredDate = Carbon::parse($this['date_expired']);
if (!$today->equalTo($urlExpiredDate)) {
$urlExpired = $urlExpiredDate->lessThanOrEqualTo($today);
}
}
return $urlExpired;
}
2020-07-01 08:57:45 +00:00
}