API: allow date attributes to be added

This commit is contained in:
Toby Zerner 2015-07-01 15:11:57 +09:30
parent 894349e9c5
commit a4dc7ff121
3 changed files with 40 additions and 4 deletions

View File

@ -47,7 +47,6 @@ class Discussion extends Model
*
* @var array
*/
protected $dates = ['start_time', 'last_time'];
/**
* An array of posts that have been added during this request.
@ -62,6 +61,7 @@ class Discussion extends Model
* @var \Flarum\Core\Models\Post[]
*/
public $removedPosts = [];
protected static $dates = ['start_time', 'last_time'];
/**
* The user for which the state relationship should be loaded.

View File

@ -17,19 +17,26 @@ use LogicException;
*
* @todo Refactor out validation, either into a trait or into a dependency.
* The following requirements need to be fulfilled:
* - Ability for extensions to alter ruleset.
* - Ability for extensions to alter ruleset (add, modify, remove).
* - Ability for extensions to add custom rules to the validator instance.
* - Use Flarum's translator with the validator instance.
*/
abstract class Model extends Eloquent
{
/**
* Indicates if the model should be timestamped. Turn them off by default.
* Indicates if the model should be timestamped. Turn off by default.
*
* @var boolean
*/
public $timestamps = false;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
public static $dates = [];
/**
* The validation rules for this model.
*
@ -162,6 +169,26 @@ abstract class Model extends Eloquent
return $rules;
}
/**
* Get the attributes that should be converted to dates.
*
* @return array
*/
public function getDates()
{
return static::$dates;
}
/**
* Add an attribute to be converted to a date.
*
* @param string $attribute
*/
public static function addDate($attribute)
{
static::$dates[] = $attribute;
}
/**
* Get an attribute from the model. If nothing is found, attempt to load
* a custom relation method with this key.

View File

@ -32,6 +32,11 @@ class Model implements ExtenderInterface
return $this;
}
public function date($attribute)
{
$this->dates[] = $attribute;
}
public function hasOne($relation, $related, $foreignKey = null, $localKey = null)
{
$this->relations[$relation] = function ($model) use ($relation, $related, $foreignKey, $localKey) {
@ -73,7 +78,7 @@ class Model implements ExtenderInterface
$model = $this->model;
foreach ($this->relations as $relation => $callback) {
$model::addRelationship($relation, $callback);
$model::setRelationMethod($relation, $callback);
}
foreach ($this->scopeVisible as $callback) {
@ -83,5 +88,9 @@ class Model implements ExtenderInterface
foreach ($this->allow as $info) {
$model::allow($info['action'], $info['callback']);
}
foreach ($this->dates as $attribute) {
$model::addDate($attribute);
}
}
}