Created
September 28, 2015 20:04
-
-
Save zach2825/5c2c720f9beff09d9571 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class BaseModel extends Model | |
{ | |
/** | |
* @description | |
* @author info@turtlebytes.com | |
* @param $name | |
* @param $type | |
* @param string $default_value | |
* @return string | |
*/ | |
public function format($name, $type = 'string', $default_value = ' ', $strict = false) | |
{ | |
$formatted = $default_value; | |
//if strict then if model value empty will use the default value. | |
if (isset($this->$name)) | |
{ | |
if ($strict === true && !empty($this->$name)) | |
{ | |
$formatted = $this->$name; | |
} elseif ($strict === false) | |
{ | |
$formatted = $this->$name; | |
} | |
} | |
switch ($type) | |
{ | |
case "ucwords": | |
$formatted = ucwords($formatted); | |
break; | |
case "ucfirst": | |
case "ucFirstLetter": | |
$formatted = ucfirst($formatted); | |
break; | |
case "phone": | |
$cleaned = preg_replace('/[^[:digit:]]/', '', $formatted); | |
$cleaned = ltrim($cleaned, '1'); | |
if (strlen($cleaned) < 9) | |
{ | |
//no need to do anything. non standard phone number | |
} else | |
{ | |
preg_match('/(\d{3})(\d{3})(\d{4})(\d+)?/', $cleaned, $matches); | |
$formatted = "({$matches[1]}) {$matches[2]}-{$matches[3]}"; | |
if (!empty($matches['4'])) | |
{ | |
$formatted .= " ext {$matches['4']}"; | |
} | |
} | |
break; | |
case 'ssn': | |
if (strlen($formatted) > 4) | |
{ | |
$formatted = substr($formatted, -4); | |
} | |
break; | |
case 'date': | |
$formatted = $this->_date($formatted, 'M, d Y'); | |
break; | |
case 'datetime': | |
$formatted = $this->_date($formatted, 'M, d Y G:i:s'); | |
break; | |
case "ago": | |
$your_date = $this->_date($formatted, 'Y-m-d'); | |
$your_date = strtotime($your_date); | |
$now = time(); // or your date as well | |
$datediff = $now - $your_date; | |
$formatted = floor($datediff/(60*60*24)); | |
break; | |
default: | |
if (preg_match('/maxchar:(\d+)/', $type, $max_matches)) | |
{ | |
list($found, $maxChars) = $max_matches; | |
if (strlen($formatted) > $maxChars) | |
{ | |
$formatted = substr($formatted, 1, $maxChars) . " . . . "; | |
} | |
} | |
} | |
return $formatted; | |
} | |
/** | |
* Returns the date of the blog post creation, | |
* on a good and more readable format :) | |
* | |
* @return string | |
*/ | |
public function created_at() | |
{ | |
return $this->_date($this->created_at); | |
} | |
/** | |
* Get the date the post was created. | |
* | |
* @param \Carbon|null $date | |
* @param string $default | |
* @return string | |
*/ | |
public function _date($date = null, $format = null, $default = 'Not a valid date') | |
{ | |
if (is_null($date)) | |
{ | |
$date = $this->created_at; | |
} | |
if (preg_match('/((start|end)_?date)/', $date, $matches)) | |
{ | |
$date = $this->$matches[0]; | |
} | |
$test = date('Y', strtotime($date . "")); | |
if ($test < 1960) | |
{ | |
return $default; | |
} | |
if (is_string($date)) | |
{ | |
$date = Carbon::instance(new \DateTime($date)); | |
} | |
if(!is_null($format)) | |
{ | |
return $date->format($format); | |
} | |
return $date->toFormattedDateString(); | |
} | |
/** | |
* Returns the date of the blog post last update, | |
* on a good and more readable format :) | |
* | |
* @return string | |
*/ | |
public function updated_at() | |
{ | |
return $this->_date($this->updated_at); | |
} | |
/** | |
* @description | |
* @author info@turtlebytes.com | |
* @return bool | |
*/ | |
public function coalesce() | |
{ | |
$args = func_get_args(); | |
for ($i = 0; $i < count($args); $i++) | |
{ | |
if (isset($this->$args[$i])) | |
{ | |
return $this->$args[$i]; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment