|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
-
-
- namespace yii\rest;
-
- use Yii;
- use yii\base\Arrayable;
- use yii\base\Component;
- use yii\base\Model;
- use yii\data\DataProviderInterface;
- use yii\data\Pagination;
- use yii\helpers\ArrayHelper;
- use yii\web\Link;
- use yii\web\Request;
- use yii\web\Response;
-
-
- class Serializer extends Component
- {
-
-
- public $fieldsParam = 'fields';
-
-
- public $expandParam = 'expand';
-
-
- public $totalCountHeader = 'X-Pagination-Total-Count';
-
-
- public $pageCountHeader = 'X-Pagination-Page-Count';
-
-
- public $currentPageHeader = 'X-Pagination-Current-Page';
-
-
- public $perPageHeader = 'X-Pagination-Per-Page';
-
-
- public $collectionEnvelope;
-
-
- public $linksEnvelope = '_links';
-
-
- public $metaEnvelope = '_meta';
-
-
- public $request;
-
-
- public $response;
-
-
- public $preserveKeys = false;
-
-
-
-
- public function init()
- {
- if ($this->request === null) {
- $this->request = Yii::$app->getRequest();
- }
- if ($this->response === null) {
- $this->response = Yii::$app->getResponse();
- }
- }
-
-
-
- public function serialize($data)
- {
- if ($data instanceof Model && $data->hasErrors()) {
- return $this->serializeModelErrors($data);
- } elseif ($data instanceof Arrayable) {
- return $this->serializeModel($data);
- } elseif ($data instanceof DataProviderInterface) {
- return $this->serializeDataProvider($data);
- } else {
- return $data;
- }
- }
-
-
-
- protected function getRequestedFields()
- {
- $fields = $this->request->get($this->fieldsParam);
- $expand = $this->request->get($this->expandParam);
-
- return [
- is_string($fields) ? preg_split('/\s*,\s*/', $fields, -1, PREG_SPLIT_NO_EMPTY) : [],
- is_string($expand) ? preg_split('/\s*,\s*/', $expand, -1, PREG_SPLIT_NO_EMPTY) : [],
- ];
- }
-
-
-
- protected function serializeDataProvider($dataProvider)
- {
- if ($this->preserveKeys) {
- $models = $dataProvider->getModels();
- } else {
- $models = array_values($dataProvider->getModels());
- }
- $models = $this->serializeModels($models);
-
- if (($pagination = $dataProvider->getPagination()) !== false) {
- $this->addPaginationHeaders($pagination);
- }
-
- if ($this->request->getIsHead()) {
- return null;
- } elseif ($this->collectionEnvelope === null) {
- return $models;
- } else {
- $result = [
- $this->collectionEnvelope => $models,
- ];
- if ($pagination !== false) {
- return array_merge($result, $this->serializePagination($pagination));
- } else {
- return $result;
- }
- }
- }
-
-
-
- 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(),
- ],
- ];
- }
-
-
-
- protected function addPaginationHeaders($pagination)
- {
- $links = [];
- foreach ($pagination->getLinks(true) as $rel => $url) {
- $links[] = "<$url>; rel=$rel";
- }
-
- $this->response->getHeaders()
- ->set($this->totalCountHeader, $pagination->totalCount)
- ->set($this->pageCountHeader, $pagination->getPageCount())
- ->set($this->currentPageHeader, $pagination->getPage() + 1)
- ->set($this->perPageHeader, $pagination->pageSize)
- ->set('Link', implode(', ', $links));
- }
-
-
-
- protected function serializeModel($model)
- {
- if ($this->request->getIsHead()) {
- return null;
- } else {
- list ($fields, $expand) = $this->getRequestedFields();
- return $model->toArray($fields, $expand);
- }
- }
-
-
-
- protected function serializeModelErrors($model)
- {
- $this->response->setStatusCode(422, 'Data Validation Failed.');
- $result = [];
- foreach ($model->getFirstErrors() as $name => $message) {
- $result[] = [
- 'field' => $name,
- 'message' => $message,
- ];
- }
-
- return $result;
- }
-
-
-
- protected function serializeModels(array $models)
- {
- list ($fields, $expand) = $this->getRequestedFields();
- foreach ($models as $i => $model) {
- if ($model instanceof Arrayable) {
- $models[$i] = $model->toArray($fields, $expand);
- } elseif (is_array($model)) {
- $models[$i] = ArrayHelper::toArray($model);
- }
- }
-
- return $models;
- }
- }
|