-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathHasParent.php
151 lines (126 loc) · 3.77 KB
/
HasParent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace Parental;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionException;
trait HasParent
{
/**
* @var bool
*/
public $hasParent = true;
/**
* @throws ReflectionException
*/
public static function bootHasParent(): void
{
// This adds support for using Parental with standalone Eloquent, outside a normal Laravel app.
if (static::getEventDispatcher() === null) {
static::setEventDispatcher(new Dispatcher);
}
static::creating(function ($model) {
if ($model->parentHasHasChildrenTrait()) {
$model->forceFill(
[$model->getInheritanceColumn() => $model->classToAlias(get_class($model))]
);
}
});
static::addGlobalScope(function ($query) {
$instance = new static;
if ($instance->parentHasHasChildrenTrait()) {
$query->where($query->getModel()->getTable() . '.' . $instance->getInheritanceColumn(), $instance->classToAlias(get_class($instance)));
}
});
}
public function parentHasHasChildrenTrait(): bool
{
return $this->hasChildren ?? false;
}
/**
* @throws ReflectionException
*/
public function getTable(): string
{
if (! isset($this->table)) {
return str_replace('\\', '', Str::snake(Str::plural(class_basename($this->getParentClass()))));
}
return $this->table;
}
/**
* @throws ReflectionException
*/
public function getForeignKey(): string
{
return Str::snake(class_basename($this->getParentClass())) . '_' . $this->primaryKey;
}
/**
* @param string $related
* @param null|Model $instance
*
* @throws ReflectionException
*/
public function joiningTable($related, $instance = null): string
{
$relatedClassName = method_exists((new $related), 'getClassNameForRelationships')
? (new $related)->getClassNameForRelationships()
: class_basename($related);
$models = [
Str::snake($relatedClassName),
Str::snake($this->getClassNameForRelationships()),
];
sort($models);
return strtolower(implode('_', $models));
}
/**
* @throws ReflectionException
*/
public function getClassNameForRelationships(): string
{
return class_basename($this->getParentClass());
}
/**
* Get the class name for polymorphic relations.
*
* @throws ReflectionException
*/
public function getMorphClass(): string
{
$parentClass = $this->getParentClass();
return (new $parentClass)->getMorphClass();
}
/**
* Get the class name for poly-type collections
*
* @throws ReflectionException
*/
public function getQueueableClassName(): string
{
return $this->getParentClass();
}
/**
* Merge the fillable attributes for the model with those of its Parent Class
*
* @return array<string>
*/
public function getFillable()
{
$parentClass = $this->getParentClass();
if ((new ReflectionClass($parentClass))->isAbstract()) {
return $this->fillable;
}
$parentFillable = (new $parentClass)->getFillable();
return array_unique(array_merge($parentFillable, $this->fillable));
}
/**
* Get the class name for Parent Class.
*
* @throws ReflectionException
*/
protected function getParentClass(): string
{
static $parentClassName;
return $parentClassName ?: $parentClassName = (new ReflectionClass($this))->getParentClass()->getName();
}
}