You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.5 KiB
87 lines
2.5 KiB
<?php
|
|
namespace blobt\airpc;
|
|
|
|
use yii\base\Arrayable;
|
|
use yii\data\DataProviderInterface;
|
|
use yii\data\Pagination;
|
|
|
|
/**
|
|
* 重载部分yii\rest\Serializer方法,以适应RPC数据返回格式化
|
|
* Class Serializer
|
|
* @package blobt\airpc
|
|
*/
|
|
class Serializer extends \yii\rest\Serializer
|
|
{
|
|
/**
|
|
* Serializes a data provider.
|
|
* @param DataProviderInterface $dataProvider
|
|
* @return array the array representation of the data provider.
|
|
*/
|
|
protected function serializeDataProvider($dataProvider)
|
|
{
|
|
if ($this->preserveKeys) {
|
|
$models = $dataProvider->getModels();
|
|
} else {
|
|
$models = array_values($dataProvider->getModels());
|
|
}
|
|
$models = $this->serializeModels($models);
|
|
|
|
$pagination = $dataProvider->getPagination();
|
|
|
|
//因为tcp传输,所以不用获取分页链接
|
|
// if (($pagination = $dataProvider->getPagination()) !== false) {
|
|
// $this->addPaginationHeaders($pagination);
|
|
// }
|
|
|
|
// if ($this->request->getIsHead()) {
|
|
// return null;
|
|
// } else
|
|
if ($this->collectionEnvelope === null) {
|
|
return $models;
|
|
}
|
|
|
|
$result = [
|
|
$this->collectionEnvelope => $models,
|
|
];
|
|
if ($pagination !== false) {
|
|
return array_merge($result, $this->serializePagination($pagination));
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Serializes a pagination into an array.
|
|
* @param Pagination $pagination
|
|
* @return array the array representation of the pagination
|
|
* @see addPaginationHeaders()
|
|
*/
|
|
protected function serializePagination($pagination)
|
|
{
|
|
return [
|
|
// $this->linksEnvelope => Link::serialize($pagination->getLinks(true)),
|
|
$this->metaEnvelope => [
|
|
'totalCount' => $pagination->totalCount,
|
|
'pageCount' => $pagination->getPageCount(),
|
|
'currentPage' => $pagination->getPage() + 1,
|
|
'perPage' => $pagination->getPageSize(),
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Serializes a model object.
|
|
* @param Arrayable $model
|
|
* @return array the array representation of the model
|
|
*/
|
|
protected function serializeModel($model)
|
|
{
|
|
// if ($this->request->getIsHead()) {
|
|
// return null;
|
|
// }
|
|
|
|
// list($fields, $expand) = $this->getRequestedFields();
|
|
$fields = [];$expand=[];
|
|
return $model->toArray($fields, $expand);
|
|
}
|
|
}
|