[...], // assuming collectionEnvelope is "items" * '_links' => { // pagination links as returned by Pagination::getLinks() * 'self' => '...', * 'next' => '...', * 'last' => '...', * }, * '_meta' => { // meta information as returned by Pagination::toArray() * 'totalCount' => 100, * 'pageCount' => 5, * 'currentPage' => 1, * 'perPage' => 20, * }, * ] * ``` * * If this property is not set, the resource arrays will be directly returned without using envelope. * The pagination information as shown in `_links` and `_meta` can be accessed from the response HTTP headers. */ public $collectionEnvelope; /** * @var string the name of the envelope (e.g. `_links`) for returning the links objects. * It takes effect only, if `collectionEnvelope` is set. */ public $linksEnvelope = '_links'; /** * @var string the name of the envelope (e.g. `_meta`) for returning the pagination object. * It takes effect only, if `collectionEnvelope` is set. */ public $metaEnvelope = '_meta'; /** * @var bool whether to preserve array keys when serializing collection data. * Set this to `true` to allow serialization of a collection as a JSON object where array keys are * used to index the model objects. The default is to serialize all collections as array, regardless * of how the array is indexed. * @see serializeDataProvider() * @since 2.0.10 */ public $preserveKeys = false; /** * Serializes the given data into a format that can be easily turned into other formats. * @param mixed $data the data to be serialized. * @return mixed the converted data. */ public function serialize($data) { return $data; } /** * Requested fields * * @return array the names of the requested fields. The first element is an array * representing the list of default fields requested, while the second element is * an array of the extra fields requested in addition to the default fields. * @see Model::fields() * @see Model::extraFields() */ 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) : [], ]; } /** * 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(), ], ]; } /** * Adds HTTP headers about the pagination to the response. * @param Pagination $pagination */ 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)); } }