ladybirdweb /
laravel-importer
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LWS\Import\Traits; |
||
| 4 | |||
| 5 | use ErrorException; |
||
| 6 | use ReflectionClass; |
||
| 7 | use ReflectionMethod; |
||
| 8 | use Illuminate\Database\Eloquent\Relations\Relation; |
||
| 9 | |||
| 10 | trait RelationshipsTrait |
||
| 11 | { |
||
| 12 | public function relationships() |
||
| 13 | { |
||
| 14 | $model = new static; |
||
| 15 | |||
| 16 | $relationships = []; |
||
| 17 | |||
| 18 | foreach ((new ReflectionClass($model))->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
||
| 19 | if ($method->class != get_class($model) || |
||
| 20 | ! empty($method->getParameters()) || |
||
| 21 | $method->getName() == __FUNCTION__) { |
||
| 22 | continue; |
||
| 23 | } |
||
| 24 | |||
| 25 | try { |
||
| 26 | $return = $method->invoke($model); |
||
| 27 | |||
| 28 | if ($return instanceof Relation) { |
||
| 29 | $relationships[$method->getName()] = [ |
||
| 30 | 'type' => (new ReflectionClass($return))->getShortName(), |
||
| 31 | 'model' => (new ReflectionClass($return->getRelated()))->getName(), |
||
| 32 | ]; |
||
| 33 | } |
||
| 34 | } catch (ErrorException $e) { |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
| 35 | } |
||
| 36 | } |
||
| 37 | |||
| 38 | return $relationships; |
||
| 39 | } |
||
| 40 | } |
||
| 41 |