Clean up model extender

- Remove unused private attributes
- Complete docblocks
- Add scalar type hints
- Format code
- Reorder methods

Refs #2100.
This commit is contained in:
Franz Liedke 2020-04-24 16:33:08 +02:00
parent 51ea326959
commit 8306cef963
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4

View File

@ -17,9 +17,6 @@ use Illuminate\Support\Arr;
class Model implements ExtenderInterface
{
private $modelClass;
private $dateAttributes = [];
private $defaults = [];
private $relationships = [];
/**
* @param string $modelClass The ::class attribute of the model you are modifying.
@ -34,10 +31,16 @@ class Model implements ExtenderInterface
* Add an attribute to be treated as a date.
*
* @param string $attribute
* @return self
*/
public function dateAttribute(string $attribute)
{
Arr::set(AbstractModel::$dateAttributes, $this->modelClass, array_merge(Arr::get(AbstractModel::$dateAttributes, $this->modelClass, []), [$attribute]));
Arr::set(AbstractModel::$dateAttributes, $this->modelClass,
array_merge(
Arr::get(AbstractModel::$dateAttributes, $this->modelClass, []),
[$attribute]
)
);
return $this;
}
@ -47,6 +50,7 @@ class Model implements ExtenderInterface
*
* @param string $attribute
* @param mixed $value
* @return self
*/
public function default(string $attribute, $value)
{
@ -55,28 +59,6 @@ class Model implements ExtenderInterface
return $this;
}
/**
* 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 $callable
*
* 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.
*/
public function relationship(string $name, callable $callable)
{
Arr::set(AbstractModel::$customRelations, "$this->modelClass.$name", $callable);
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.
@ -88,8 +70,9 @@ class Model implements ExtenderInterface
* @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, $related, $foreignKey = null, $ownerKey = null)
public function belongsTo(string $name, string $related, string $foreignKey = null, string $ownerKey = null)
{
return $this->relationship($name, function (AbstractModel $model) use ($related, $foreignKey, $ownerKey, $name) {
return $model->belongsTo($related, $foreignKey, $ownerKey, $name);
@ -110,15 +93,16 @@ class Model implements ExtenderInterface
* @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,
$related,
$table = null,
$foreignPivotKey = null,
$relatedPivotKey = null,
$parentKey = null,
$relatedKey = null
string $related,
string $table = null,
string $foreignPivotKey = null,
string $relatedPivotKey = null,
string $parentKey = null,
string $relatedKey = null
) {
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);
@ -136,8 +120,9 @@ class Model implements ExtenderInterface
* @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, $related, $foreignKey = null, $localKey = null)
public function hasOne(string $name, string $related, string $foreignKey = null, string $localKey = null)
{
return $this->relationship($name, function (AbstractModel $model) use ($related, $foreignKey, $localKey) {
return $model->hasOne($related, $foreignKey, $localKey);
@ -155,14 +140,39 @@ class Model implements ExtenderInterface
* @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, $related, $foreignKey = null, $localKey = null)
public function hasMany(string $name, string $related, string $foreignKey = null, string $localKey = null)
{
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 $callable
*
* 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, callable $callable)
{
Arr::set(AbstractModel::$customRelations, "$this->modelClass.$name", $callable);
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
// Nothing needed here.