Skip to content

Instantly share code, notes, and snippets.

@arashrasoulzadeh
Created September 10, 2019 17:39
Show Gist options
  • Save arashrasoulzadeh/ba74e0ebe7ac7df266fd8a2e7f88938e to your computer and use it in GitHub Desktop.
Save arashrasoulzadeh/ba74e0ebe7ac7df266fd8a2e7f88938e to your computer and use it in GitHub Desktop.
<?php
namespace arashrasoulzadeh\auth\src\traits;
use arashrasoulzadeh\auth\src\model\UserAuthMetas;
trait ModelMetaTrait
{
public $autoExclude = false;
public $is_auto_excluded = false;
public $check_for_duplicate = true;
public $additionalExcludedMetas = [ "created_at" , "updated_at" ];
/** @var array
* used by caching system
*/
private $fieldCaches = [
];
/**
* @inheritDoc
*/
public function __get ( $key )
{
$this->refreshAutoExcludes();
$original = parent::__get( $key );
if ( !in_array( $key , $this->excludeInMeta() ) ) {
if ( !isset( $this->fieldCaches[ $key ] ) ) {
$value = $this->loadFromDatabase( $key );
$this->fieldCaches[ $key ] = $this->purifyValue( $value );
return $this->fieldCaches[ $key ];
}
else {
return $this->fieldCaches[ $key ];
}
}
else {
return $original;
}
}
/**
* @inheritDoc
*/
public function __set ( $key , $value )
{
if ( is_array( $value ) ) {
$value = json_encode( $value );
}
$this->refreshAutoExcludes();
$original = parent::__get( $key );
if ( !in_array( $key , $this->excludeInMeta() ) ) {
$last = $this->$key;
if ( $this->check_for_duplicate ) {
if ( $last != $value ) {
$this->saveToDatabase( $key , $value );
$this->fieldCaches[ $key ] = $value;
}
}
else {
$this->saveToDatabase( $key , $value );
$this->fieldCaches[ $key ] = $value;
}
parent::__set( $key , $value );
}
else {
parent::__set( $key , $value );
}
}
public function refreshAutoExcludes ()
{
try {
if ( $this->autoExclude && !$this->is_auto_excluded ) {
$this->is_auto_excluded = true;
$sample = $this->all()->first();
if ( !is_null( $sample ) ) {
$attributes = $sample->getAttributes();
$this->additionalExcludedMetas[] = array_keys( $attributes );
}
}
} catch ( \Exception $exception ) {
}
}
/**
* @return array
* this function keys will NOT be loaded from metas
*/
public function excludeInMeta ()
{
return array_merge( [] , $this->additionalExcludedMetas );
}
/**
* load the key from database
*
* @param $key
*
* @return string|null
*/
public function loadFromDatabase ( $key )
{
$item = UserAuthMetas
::where( "class" , $this->table )
->where( "key" , $key )
->orderBy( "id" , "desc" )
->where( "owner" , $this->id )
;
if ( $item->count() ) {
$item = $item->first();
$fresh = $item->fresh();
return $fresh->value;
}
else {
return null;
}
}
/**
* reformat value
*
* @param $value
*
* @return string|array
*/
private function purifyValue ( $value )
{
$result = json_decode( $value , true );
if ( json_last_error() == JSON_ERROR_NONE ) {
return $result;
}
else {
return $value;
}
}
/**
* save the key to database
*
* @param $key
* @param $value
*/
private function saveToDatabase ( $key , $value )
{
$item = new UserAuthMetas();
$item->class = $this->table;
$item->owner = $this->id;
$item->key = $key;
$item->value = $value;
$item->save();
}
/**
*
*/
public function getMetas ()
{
$metas = UserAuthMetas
::where( "class" , $this->table )
->where( "owner" , $this->id )
->orderBy( 'created_at' , 'desc' )
->get()
->unique( 'key' )
;
return $metas;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment