Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. <?php
  2.  
  3. namespace arashrasoulzadeh\auth\src\traits;
  4.  
  5. use arashrasoulzadeh\auth\src\model\UserAuthMetas;
  6.  
  7. trait ModelMetaTrait
  8. {
  9.  
  10. public $autoExclude = false;
  11.  
  12. public $is_auto_excluded = false;
  13.  
  14.  
  15. public $check_for_duplicate = true;
  16.  
  17. public $additionalExcludedMetas = [ "created_at" , "updated_at" ];
  18. /** @var array
  19. * used by caching system
  20. */
  21. private $fieldCaches = [
  22.  
  23. ];
  24.  
  25. /**
  26. * @inheritDoc
  27. */
  28. public function __get ( $key )
  29. {
  30.  
  31.  
  32. $this->refreshAutoExcludes();
  33. $original = parent::__get( $key );
  34. if ( !in_array( $key , $this->excludeInMeta() ) ) {
  35.  
  36. if ( !isset( $this->fieldCaches[ $key ] ) ) {
  37. $value = $this->loadFromDatabase( $key );
  38. $this->fieldCaches[ $key ] = $this->purifyValue( $value );
  39. return $this->fieldCaches[ $key ];
  40. }
  41. else {
  42. return $this->fieldCaches[ $key ];
  43. }
  44.  
  45.  
  46. }
  47. else {
  48. return $original;
  49. }
  50.  
  51. }
  52.  
  53. /**
  54. * @inheritDoc
  55. */
  56. public function __set ( $key , $value )
  57. {
  58. if ( is_array( $value ) ) {
  59. $value = json_encode( $value );
  60. }
  61. $this->refreshAutoExcludes();
  62. $original = parent::__get( $key );
  63. if ( !in_array( $key , $this->excludeInMeta() ) ) {
  64. $last = $this->$key;
  65. if ( $this->check_for_duplicate ) {
  66. if ( $last != $value ) {
  67. $this->saveToDatabase( $key , $value );
  68. $this->fieldCaches[ $key ] = $value;
  69. }
  70. }
  71. else {
  72. $this->saveToDatabase( $key , $value );
  73. $this->fieldCaches[ $key ] = $value;
  74. }
  75. parent::__set( $key , $value );
  76. }
  77. else {
  78. parent::__set( $key , $value );
  79. }
  80. }
  81.  
  82. public function refreshAutoExcludes ()
  83. {
  84. try {
  85. if ( $this->autoExclude && !$this->is_auto_excluded ) {
  86. $this->is_auto_excluded = true;
  87. $sample = $this->all()->first();
  88. if ( !is_null( $sample ) ) {
  89. $attributes = $sample->getAttributes();
  90. $this->additionalExcludedMetas[] = array_keys( $attributes );
  91. }
  92.  
  93. }
  94. } catch ( \Exception $exception ) {
  95.  
  96. }
  97. }
  98.  
  99. /**
  100. * @return array
  101. * this function keys will NOT be loaded from metas
  102. */
  103. public function excludeInMeta ()
  104. {
  105. return array_merge( [] , $this->additionalExcludedMetas );
  106. }
  107.  
  108. /**
  109. * load the key from database
  110. *
  111. * @param $key
  112. *
  113. * @return string|null
  114. */
  115. public function loadFromDatabase ( $key )
  116. {
  117. $item = UserAuthMetas
  118. ::where( "class" , $this->table )
  119. ->where( "key" , $key )
  120. ->orderBy( "id" , "desc" )
  121. ->where( "owner" , $this->id )
  122. ;
  123. if ( $item->count() ) {
  124. $item = $item->first();
  125. $fresh = $item->fresh();
  126. return $fresh->value;
  127. }
  128. else {
  129. return null;
  130. }
  131. }
  132.  
  133. /**
  134. * reformat value
  135. *
  136. * @param $value
  137. *
  138. * @return string|array
  139. */
  140. private function purifyValue ( $value )
  141. {
  142. $result = json_decode( $value , true );
  143. if ( json_last_error() == JSON_ERROR_NONE ) {
  144. return $result;
  145. }
  146. else {
  147. return $value;
  148. }
  149. }
  150.  
  151. /**
  152. * save the key to database
  153. *
  154. * @param $key
  155. * @param $value
  156. */
  157. private function saveToDatabase ( $key , $value )
  158. {
  159. $item = new UserAuthMetas();
  160. $item->class = $this->table;
  161. $item->owner = $this->id;
  162. $item->key = $key;
  163. $item->value = $value;
  164. $item->save();
  165. }
  166.  
  167. /**
  168. *
  169. */
  170. public function getMetas ()
  171. {
  172. $metas = UserAuthMetas
  173. ::where( "class" , $this->table )
  174. ->where( "owner" , $this->id )
  175. ->orderBy( 'created_at' , 'desc' )
  176. ->get()
  177. ->unique( 'key' )
  178. ;
  179.  
  180. return $metas;
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement