С версии: 2.0
Автор: Qiang Xue (qiang.xue@gmail.com)
Наследование: extends yii\base\Object
Пример #1
0
 /**
  * Serializes a pagination into an array.
  * @param Pagination $pagination
  * @return array the array representation of the pagination
  * @see addPaginationHeaders()
  */
 protected function serializePagination($pagination)
 {
     return ['_links' => Link::serialize($pagination->getLinks(true)), '_meta' => ['totalCount' => $pagination->totalCount, 'pageCount' => $pagination->getPageCount(), 'currentPage' => $pagination->getPage(), 'perPage' => $pagination->getPageSize()]];
 }
Пример #2
0
 /**
  * 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()]];
 }
Пример #3
0
 /**
  * Serializes a pagination into an array.
  * @param \yii\data\Pagination $pagination
  * @return array the array representation of the pagination
  * @see addPaginationHeaders()
  */
 protected function serializePagination($pagination)
 {
     if ($this->serializedPagination !== null) {
         return $this->serializedPagination;
     }
     /** @var \yii\rest\Serializer $serializer */
     $serializer = $this->serializer;
     return $this->serializedPagination = [$serializer->linksEnvelope => \yii\web\Link::serialize($pagination->getLinks(true)), $serializer->metaEnvelope => ['totalCount' => $pagination->totalCount, 'pageCount' => $pagination->getPageCount(), 'currentPage' => $pagination->getPage() + 1, 'perPage' => $pagination->getPageSize()]];
 }
Пример #4
0
 /**
  * Converts the model into an array.
  *
  * This method will first identify which fields to be included in the resulting array by calling [[resolveFields()]].
  * It will then turn the model into an array with these fields. If `$recursive` is true,
  * any embedded objects will also be converted into arrays.
  *
  * If the model implements the [[Linkable]] interface, the resulting array will also have a `_link` element
  * which refers to a list of links as specified by the interface.
  *
  * @param array $fields the fields being requested. If empty, all fields as specified by [[fields()]] will be returned.
  * @param array $expand the additional fields being requested for exporting. Only fields declared in [[extraFields()]]
  * will be considered.
  * @param boolean $recursive whether to recursively return array representation of embedded objects.
  * @return array the array representation of the object
  */
 public function toArray(array $fields = [], array $expand = [], $recursive = true)
 {
     $data = [];
     foreach ($this->resolveFields($fields, $expand) as $field => $definition) {
         $data[$field] = is_string($definition) ? $this->{$definition} : call_user_func($definition, $this, $field);
     }
     if ($this instanceof Linkable) {
         $data['_links'] = Link::serialize($this->getLinks());
     }
     return $recursive ? ArrayHelper::toArray($data) : $data;
 }
Пример #5
0
 public function toArray(array $fields = [], array $expand = [], $recursive = true)
 {
     $data = [];
     if (method_exists($this, 'defaultExpand')) {
         $expand = array_merge($expand, $this->defaultExpand());
     }
     foreach ($this->resolveFields($fields, $expand) as $field => $definition) {
         $data[$field] = is_string($definition) ? $this->{$definition} : call_user_func($definition, $field, $this);
     }
     if ($this instanceof Linkable) {
         $data['_links'] = Link::serialize($this->getLinks());
     }
     $this->trigger(self::EVENT_AFTER_TO_ARRAY, new ToArrayEvent([], $data));
     return $recursive ? ArrayHelper::toArray($data) : $data;
 }