1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Components\Attributes; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\Components\Interfaces\SmartAttributeInterface; |
6
|
|
|
use Backpack\CRUD\app\Library\Components\Interfaces\SmartCollectionInterface; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
class Entity extends BaseAttribute implements SmartAttributeInterface |
10
|
|
|
{ |
11
|
|
|
public static function getDefault(SmartCollectionInterface $attributes) |
12
|
|
|
{ |
13
|
|
|
$model = $attributes->hasAttribute('baseModel') ? (new $attributes->getAttributeValue('baseModel')) : app('crud')->model; |
|
|
|
|
14
|
|
|
|
15
|
|
|
$fieldName = $attributes->getAttributeValue('name'); |
16
|
|
|
|
17
|
|
|
//if the name is dot notation we are sure it's a relationship |
18
|
|
|
if (strpos($fieldName, '.') !== false) { |
19
|
|
|
$possibleMethodName = Str::of($fieldName)->before('.'); |
20
|
|
|
|
21
|
|
|
return self::modelMethodIsRelationship($model, $possibleMethodName) ? $fieldName : false; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// if there's a method on the model with this name |
25
|
|
|
if (method_exists($model, $fieldName)) { |
26
|
|
|
// check model method for possibility of being a relationship |
27
|
|
|
return self::modelMethodIsRelationship($model, $fieldName); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// if the name ends with _id and that method exists, |
31
|
|
|
// we can probably use it as an entity |
32
|
|
|
if (Str::endsWith($fieldName, '_id')) { |
33
|
|
|
$possibleMethodName = Str::replaceLast('_id', '', $fieldName); |
34
|
|
|
|
35
|
|
|
if (method_exists($model, $possibleMethodName)) { |
36
|
|
|
// check model method for possibility of being a relationship |
37
|
|
|
return self::modelMethodIsRelationship($model, $possibleMethodName); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function getValidationRules(SmartCollectionInterface $attributes): array |
45
|
|
|
{ |
46
|
|
|
return ['required']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function getAttributeName(): string |
50
|
|
|
{ |
51
|
|
|
return 'entity'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Checks the properties of the provided method to better verify if it could be a relation. |
56
|
|
|
* Case the method is not public, is not a relation. |
57
|
|
|
* Case the return type is Attribute, or extends Attribute is not a relation method. |
58
|
|
|
* If the return type extends the Relation class is for sure a relation |
59
|
|
|
* Otherwise we just assume it's a relation. |
60
|
|
|
* |
61
|
|
|
* DEV NOTE: In future versions we will return `false` when no return type is set and make the return type mandatory for relationships. |
62
|
|
|
* This function should be refactored to only check if $returnType is a subclass of Illuminate\Database\Eloquent\Relations\Relation. |
63
|
|
|
* |
64
|
|
|
* @param $model |
65
|
|
|
* @param $method |
66
|
|
|
* @return bool|string |
67
|
|
|
*/ |
68
|
|
|
private static function modelMethodIsRelationship($model, $method) |
69
|
|
|
{ |
70
|
|
|
$methodReflection = new \ReflectionMethod($model, $method); |
71
|
|
|
|
72
|
|
|
// relationship methods function does not have parameters |
73
|
|
|
if ($methodReflection->getNumberOfParameters() > 0) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// relationships are always public methods. |
78
|
|
|
if (! $methodReflection->isPublic()) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$returnType = $methodReflection->getReturnType(); |
83
|
|
|
|
84
|
|
|
if ($returnType) { |
85
|
|
|
$returnType = $returnType->getName(); |
|
|
|
|
86
|
|
|
|
87
|
|
|
if (is_a($returnType, 'Illuminate\Database\Eloquent\Casts\Attribute', true)) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (is_a($returnType, 'Illuminate\Database\Eloquent\Relations\Relation', true)) { |
92
|
|
|
return $method; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $method; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|