app/Support/Traits
-
-
Save martinandersen3d/95b90bb5d0958e6d2dc749d443dbcc1d to your computer and use it in GitHub Desktop.
This file contains hidden or 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\Support\Traits; | |
trait HasCents{ | |
public static function attributesDollarsToCents($attributes){ | |
$instance = new static; | |
foreach($instance->centFields as $centField){ | |
$dollarField = str_replace('_cents','',$centField); | |
if(isset($attributes[$dollarField])){ | |
$attributes[$centField] = static::dollarsToCents($attributes[$dollarField]); | |
} | |
} | |
return $attributes; | |
} | |
public static function dollarsToCents($dollars){ | |
$dollars = preg_replace("/[^0-9.]/", "", $dollars); | |
$dollars = number_format($dollars,2,'.',''); | |
if($dollars > 0){ | |
$cents = $dollars * 100; | |
$cents = round($cents); | |
return $cents; | |
} | |
return 0; | |
} | |
public static function centsToDollars($cents){ | |
if($cents > 0){ | |
$dollars = $cents/100; | |
return $dollars; | |
} | |
return 0; | |
} | |
} |
This file contains hidden or 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\Support\Traits; | |
trait HasPassword{ | |
public static function attributesGeneratePasswordIfEmpty($attributes = []){ | |
if(empty($attributes['password'])){ | |
$attributes['password'] = str_random(11); | |
} | |
return $attributes; | |
} | |
public static function attributesHashPasswordIfExists($attributes = []){ | |
if(!empty($attributes['password'])){ | |
$attributes['password'] = \Hash::make($attributes['password']); | |
}else{ | |
unset($attributes['password']); | |
} | |
return $attributes; | |
} | |
} |
This file contains hidden or 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\Support\Traits; | |
trait HasPhotos{ | |
public function photos(){ | |
return $this->morphMany(\App\Photo::class, 'photoable'); | |
} | |
public function photo(){ | |
return $this->morphOne(\App\Photo::class, 'photoable'); | |
} | |
public function storePhoto($attributes = []){ | |
$data = [ | |
'photoable_type' => get_class($this), | |
'photoable_id' => $this->id, | |
]; | |
$data = $data+$attributes; | |
$storagePath = $this->photoStoragePath.'/'.$this->id; | |
$photo = \App\Photo::attachAndCreate($data,$storagePath); | |
return $photo; | |
} | |
public function attachPhotoIfExists(array $attributes = []){ | |
if(!empty($attributes['photo_file'])){ | |
$photoData = [ | |
'file' => $attributes['photo_file'], | |
]; | |
if($this->photo){ | |
$this->photo->updateAttachment($attributes['photo_file']); | |
}else{ | |
$this->storePhoto($photoData); | |
} | |
} | |
} | |
public function photoUrl(){ | |
if($this->photo){ | |
return $this->photo->fileUrl(); | |
} | |
return url($this->photoDefaultPath); | |
} | |
} |
This file contains hidden or 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\Support\Traits; | |
trait HasRole{ | |
public function roleIs($roleName){ | |
if($this->role == $roleName){ | |
return true; | |
} | |
return false; | |
} | |
public function roleIsIn($roles){ | |
foreach($roles as $role){ | |
if($this->roleIs($role)){ | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
This file contains hidden or 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\Support\Traits; | |
trait HasSlug{ | |
public static function generateSlug($string){ | |
$slug = strtolower($string); | |
$slug = str_replace(' ','-', $slug); | |
return $slug; | |
} | |
public static function findBySlug($slug){ | |
return static::where('slug', $slug)->first(); | |
} | |
public static function addSlugToAttributesIfEmpty($attributes){ | |
if(empty($attributes['slug'])){ | |
$attributes['slug'] = static::generateSlug($attributes['name']); | |
} | |
return $attributes; | |
} | |
} |
This file contains hidden or 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\Support\Traits; | |
trait HasUUID{ | |
public static function generateUuid(){ | |
return \Uuid::generate()->string; | |
} | |
public static function findByUuid($uuid){ | |
return static::where('uuid', $uuid)->first(); | |
} | |
public static function addUuidToAttributesIfEmpty($attributes){ | |
if(empty($attributes['uuid'])){ | |
$attributes['uuid'] = static::generateUuid(); | |
} | |
return $attributes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment