Flarum\Api\Serializer\PostBasicSerializer::getDefaultAttributes PHP Метод

getDefaultAttributes() защищенный Метод

protected getDefaultAttributes ( Post $post )
$post Flarum\Core\Post
    protected function getDefaultAttributes($post)
    {
        if (!$post instanceof Post) {
            throw new InvalidArgumentException(get_class($this) . ' can only serialize instances of ' . Post::class);
        }
        $attributes = ['id' => (int) $post->id, 'number' => (int) $post->number, 'time' => $this->formatDate($post->time), 'contentType' => $post->type];
        if ($post instanceof CommentPost) {
            $attributes['contentHtml'] = $post->content_html;
        } else {
            $attributes['content'] = $post->content;
        }
        return $attributes;
    }

Usage Example

Пример #1
0
 /**
  * {@inheritdoc}
  */
 protected function getDefaultAttributes($post)
 {
     $attributes = parent::getDefaultAttributes($post);
     unset($attributes['content']);
     $gate = $this->gate->forUser($this->actor);
     $canEdit = $gate->allows('edit', $post);
     if ($post instanceof CommentPost) {
         $attributes['contentHtml'] = $post->content_html;
         if ($canEdit) {
             $attributes['content'] = $post->content;
         }
         if ($gate->allows('viewIps', $post)) {
             $attributes['ipAddress'] = $post->ip_address;
         }
     } else {
         $attributes['content'] = $post->content;
     }
     if ($post->edit_time) {
         $attributes['editTime'] = $this->formatDate($post->edit_time);
     }
     if ($post->hide_time) {
         $attributes['isHidden'] = true;
         $attributes['hideTime'] = $this->formatDate($post->hide_time);
     }
     $attributes += ['canEdit' => $canEdit, 'canDelete' => $gate->allows('delete', $post)];
     return $attributes;
 }