Alive2212 /
LaravelSmartRestful
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Alive2212\LaravelSmartRestful; |
||
| 4 | |||
| 5 | use App\Resources\Reflection\LaravelReflectionHelper; |
||
| 6 | use Fico7489\Laravel\Pivot\Traits\PivotEventTrait; |
||
| 7 | use Illuminate\Notifications\Notifiable; |
||
| 8 | use Laravel\Passport\HasApiTokens; |
||
| 9 | use Laravel\Scout\Searchable; |
||
| 10 | use Illuminate\Foundation\Auth\User as Authenticatable; |
||
| 11 | use Phoenix\EloquentMeta\MetaTrait; |
||
| 12 | |||
| 13 | |||
| 14 | class BaseAuthModel extends Authenticatable |
||
| 15 | { |
||
| 16 | use Searchable, PivotEventTrait,Notifiable, HasApiTokens, MetaTrait; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 17 | |||
| 18 | /** |
||
| 19 | * default relations of method |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $relation = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * searchable columns in this model |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $searchableColumns = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * searchable columns of one model into this model |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $searchableInnerColumns = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * searchable columns of many model into this model |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $searchableManyInnerColumns = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get the indexable data array for the model. |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | public function toSearchableArray() |
||
| 52 | { |
||
| 53 | if (!sizeof($this->searchableColumns)) { |
||
| 54 | $result = collect($this); |
||
| 55 | } else { |
||
| 56 | $result = collect([ |
||
| 57 | 'id' => $this['id'], |
||
| 58 | ]); |
||
| 59 | foreach ($this->searchableColumns as $searchableColumn) { |
||
| 60 | $result->put($searchableColumn, collect($this)->get($searchableColumn)); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | if (sizeof($this->searchableInnerColumns)) { |
||
| 64 | foreach ($this->searchableInnerColumns as $searchableInnerColumnKey => $searchableInnerColumnValue) { |
||
| 65 | $searchableInnerColumnKey = explode('-', $searchableInnerColumnKey)[0]; |
||
| 66 | $innerModels = explode('.', $searchableInnerColumnKey); |
||
| 67 | $values = $this; |
||
| 68 | foreach ($innerModels as $innerModel) { |
||
| 69 | $values = $values->{$innerModel}; |
||
| 70 | } |
||
| 71 | $values = collect($values); |
||
| 72 | $result->put($searchableInnerColumnKey . '-' . $searchableInnerColumnValue, |
||
| 73 | collect($values)->get($searchableInnerColumnValue)); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | if (sizeof($this->searchableManyInnerColumns)) { |
||
| 77 | foreach ($this->searchableManyInnerColumns as $searchableInnerColumnKey => $searchableInnerColumnValue) { |
||
| 78 | $searchableInnerColumnKey = explode('-', $searchableInnerColumnKey)[0]; |
||
| 79 | $innerModels = explode('.', $searchableInnerColumnKey); |
||
| 80 | $values = $this; |
||
| 81 | foreach ($innerModels as $innerModel) { |
||
| 82 | $values = $values->{$innerModel}; |
||
| 83 | } |
||
| 84 | $values = collect($values); |
||
| 85 | $counter = 0; |
||
| 86 | foreach ($values as $value) { |
||
| 87 | $result->put($searchableInnerColumnKey . '-' . $searchableInnerColumnValue . '-' . $counter, |
||
| 88 | collect($value)->get($searchableInnerColumnValue)); |
||
| 89 | $counter++; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | return $result->toArray(); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * get methods names of child class in array type |
||
| 98 | * |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | public function getMethods() |
||
| 102 | { |
||
| 103 | return (new LaravelReflectionHelper())->getClassMethodsNames($this,\ReflectionMethod::IS_PUBLIC); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | public function getRelatedMethods() |
||
| 110 | { |
||
| 111 | return (new LaravelReflectionHelper())->getClassRelationMethodsNames($this); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return mixed |
||
| 116 | */ |
||
| 117 | public function withRelation() |
||
| 118 | { |
||
| 119 | return $this->with($this->getRelatedMethods()); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | public function loadRelation() |
||
| 126 | { |
||
| 127 | return $this->load($this->getRelatedMethods()); |
||
| 128 | } |
||
| 129 | } |
||
| 130 |