skqr/hateoas

View on GitHub
JsonApi/Serializer/PaginationMetadataSerializer.php

Summary

Maintainability
A
35 mins
Test Coverage
<?php
/**
 * @copyright 2014 Integ S.A.
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
 * @author Javier Lorenzana <javier.lorenzana@gointegro.com>
 */

namespace GoIntegro\Hateoas\JsonApi\Serializer;

// JSON-API
use GoIntegro\Hateoas\JsonApi\Document;

/**
 * @todo Move a un sub-namespace "JsonApi\Extension".
 */
class PaginationMetadataSerializer implements DocumentSerializerInterface
{
    public function serialize(Document $document)
    {
        $json = [];

        if (!empty($document->pagination)) {
            foreach (['page', 'size', 'total'] as $key) {
                if (is_null($document->pagination->$key)) continue;
                $json[$key] = $document->pagination->$key;
            }
        }

        return $json;
    }
}