1
0
mirror of https://github.com/flarum/core.git synced 2025-10-10 14:34:30 +02:00

Upgrade to JSON-API RC3 + latest version of tobscure/json-api

Note: npm source for ember-json-api changed to a fork, but I still had
to apply a custom change to get polymorphic relationships to work (see
https://github.com/kurko/ember-json-api/pull/71#issuecomment-85257281)
This commit is contained in:
Toby Zerner
2015-03-24 15:04:24 +10:30
parent 536281e273
commit a62e04f956
36 changed files with 342 additions and 503 deletions

View File

@@ -1,11 +1,10 @@
<?php namespace Flarum\Api\Serializers;
use Flarum\Core\Models\Activity;
class ActivitySerializer extends BaseSerializer
{
/**
* The resource type.
*
* @var string
*/
protected $type = 'activity';
@@ -16,50 +15,30 @@ class ActivitySerializer extends BaseSerializer
* @param Activity $activity The Activity model to serialize.
* @return array
*/
protected function attributes(Activity $activity)
protected function attributes($activity)
{
$attributes = [
'id' => ((int) $activity->id) ?: str_random(5),
'type' => $activity->type,
'contentType' => $activity->type,
'content' => json_encode($activity->data),
'time' => $activity->time->toRFC3339String()
];
return $this->attributesEvent($activity, $attributes);
return $this->extendAttributes($activity, $attributes);
}
/**
* Get a resource containing an activity's sender.
*
* @param Activity $activity
* @return Tobscure\JsonApi\Resource
*/
public function linkUser(Activity $activity)
public function user()
{
return (new UserBasicSerializer)->resource($activity->user_id);
return $this->hasOne('Flarum\Api\Serializers\UserBasicSerializer');
}
/**
* Get a resource containing an activity's sender.
*
* @param Activity $activity
* @param array $relations
* @return Tobscure\JsonApi\Resource
*/
public function includeSender(Activity $activity, $relations)
public function sender()
{
return (new UserBasicSerializer($relations))->resource($activity->sender);
return $this->hasOne('Flarum\Api\Serializers\UserBasicSerializer');
}
/**
* Get a resource containing an activity's sender.
*
* @param Activity $activity
* @param array $relations
* @return Tobscure\JsonApi\Resource
*/
public function includePost(Activity $activity, $relations)
public function post()
{
return (new PostSerializer($relations))->resource($activity->post);
return $this->hasOne('Flarum\Api\Serializers\PostSerializer');
}
}