-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Model.php
188 lines (172 loc) · 7.8 KB
/
Model.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extend;
use Flarum\Database\AbstractModel;
use Flarum\Extension\Extension;
use Flarum\Foundation\ContainerUtil;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Arr;
class Model implements ExtenderInterface
{
private $modelClass;
private $customRelations = [];
/**
* @param string $modelClass: The ::class attribute of the model you are modifying.
* This model should extend from \Flarum\Database\AbstractModel.
*/
public function __construct(string $modelClass)
{
$this->modelClass = $modelClass;
}
/**
* Add an attribute to be treated as a date.
*
* @param string $attribute
* @return self
*/
public function dateAttribute(string $attribute): self
{
Arr::set(
AbstractModel::$dateAttributes,
$this->modelClass,
array_merge(
Arr::get(AbstractModel::$dateAttributes, $this->modelClass, []),
[$attribute]
)
);
return $this;
}
/**
* Add a default value for a given attribute, which can be an explicit value, a closure,
* or an instance of an invokable class. Unlike with some other extenders,
* it CANNOT be the `::class` attribute of an invokable class.
*
* @param string $attribute
* @param mixed $value
* @return self
*/
public function default(string $attribute, $value): self
{
Arr::set(AbstractModel::$defaults, "$this->modelClass.$attribute", $value);
return $this;
}
/**
* Establish a simple belongsTo relationship from this model to another model.
* This represents an inverse one-to-one or inverse one-to-many relationship.
* For more complex relationships, use the ->relationship method.
*
* @param string $name: The name of the relation. This doesn't have to be anything in particular,
* but has to be unique from other relation names for this model, and should
* work as the name of a method.
* @param string $related: The ::class attribute of the model, which should extend \Flarum\Database\AbstractModel.
* @param string $foreignKey: The foreign key attribute of the parent model.
* @param string $ownerKey: The primary key attribute of the parent model.
* @return self
*/
public function belongsTo(string $name, string $related, string $foreignKey = null, string $ownerKey = null): self
{
return $this->relationship($name, function (AbstractModel $model) use ($related, $foreignKey, $ownerKey, $name) {
return $model->belongsTo($related, $foreignKey, $ownerKey, $name);
});
}
/**
* Establish a simple belongsToMany relationship from this model to another model.
* This represents a many-to-many relationship.
* For more complex relationships, use the ->relationship method.
*
* @param string $name: The name of the relation. This doesn't have to be anything in particular,
* but has to be unique from other relation names for this model, and should
* work as the name of a method.
* @param string $related: The ::class attribute of the model, which should extend \Flarum\Database\AbstractModel.
* @param string $table: The intermediate table for this relation
* @param string $foreignPivotKey: The foreign key attribute of the parent model.
* @param string $relatedPivotKey: The associated key attribute of the relation.
* @param string $parentKey: The key name of the parent model.
* @param string $relatedKey: The key name of the related model.
* @return self
*/
public function belongsToMany(
string $name,
string $related,
string $table = null,
string $foreignPivotKey = null,
string $relatedPivotKey = null,
string $parentKey = null,
string $relatedKey = null
): self {
return $this->relationship($name, function (AbstractModel $model) use ($related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $name) {
return $model->belongsToMany($related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $name);
});
}
/**
* Establish a simple hasOne relationship from this model to another model.
* This represents a one-to-one relationship.
* For more complex relationships, use the ->relationship method.
*
* @param string $name: The name of the relation. This doesn't have to be anything in particular,
* but has to be unique from other relation names for this model, and should
* work as the name of a method.
* @param string $related: The ::class attribute of the model, which should extend \Flarum\Database\AbstractModel.
* @param string $foreignKey: The foreign key attribute of the parent model.
* @param string $localKey: The primary key attribute of the parent model.
* @return self
*/
public function hasOne(string $name, string $related, string $foreignKey = null, string $localKey = null): self
{
return $this->relationship($name, function (AbstractModel $model) use ($related, $foreignKey, $localKey) {
return $model->hasOne($related, $foreignKey, $localKey);
});
}
/**
* Establish a simple hasMany relationship from this model to another model.
* This represents a one-to-many relationship.
* For more complex relationships, use the ->relationship method.
*
* @param string $name: The name of the relation. This doesn't have to be anything in particular,
* but has to be unique from other relation names for this model, and should
* work as the name of a method.
* @param string $related: The ::class attribute of the model, which should extend \Flarum\Database\AbstractModel.
* @param string $foreignKey: The foreign key attribute of the parent model.
* @param string $localKey: The primary key attribute of the parent model.
* @return self
*/
public function hasMany(string $name, string $related, string $foreignKey = null, string $localKey = null): self
{
return $this->relationship($name, function (AbstractModel $model) use ($related, $foreignKey, $localKey) {
return $model->hasMany($related, $foreignKey, $localKey);
});
}
/**
* Add a relationship from this model to another model.
*
* @param string $name: The name of the relation. This doesn't have to be anything in particular,
* but has to be unique from other relation names for this model, and should
* work as the name of a method.
* @param callable|string $callback
*
* The callable can be a closure or invokable class, and should accept:
* - $instance: An instance of this model.
*
* The callable should return:
* - $relationship: A Laravel Relationship object. See relevant methods of models
* like \Flarum\User\User for examples of how relationships should be returned.
*
* @return self
*/
public function relationship(string $name, $callback): self
{
$this->customRelations[$name] = $callback;
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
foreach ($this->customRelations as $name => $callback) {
Arr::set(AbstractModel::$customRelations, "$this->modelClass.$name", ContainerUtil::wrapCallback($callback, $container));
}
}
}