Skip to content

Instantly share code, notes, and snippets.

@IvanAlekseevichPopov
Last active May 17, 2018 09:23
Show Gist options
  • Save IvanAlekseevichPopov/640bd8b03d4b72d75d8cd0c1ec843bde to your computer and use it in GitHub Desktop.
Save IvanAlekseevichPopov/640bd8b03d4b72d75d8cd0c1ec843bde to your computer and use it in GitHub Desktop.
nelmio doc view
<?php
class CustomsStatisticsController {
/**
* @SWG\Response(
* response=200,
* description="Returns customs statistics list joined by tnved",
* @Model(type=\App\Form\Model\View\CustomStatisticsListView::class)
*/
public function customsStatisticsListAction(Request $request, Tnved $tnved)
{
//.......
return new CustomStatisticsListView($paginatedList);
}
}
<?php
declare(strict_types = 1);
namespace App\Form\Model\View;
use App\Form\Model\Response\PaginatedList;
/**
* Class ListView
*/
abstract class AbstractListView
{
/**
* @var array
*/
public $data;
/**
* @var MetaView
*/
public $meta;
/**
* CustomStatisticsView constructor.
*
* @param PaginatedList $list
*/
public function __construct(PaginatedList $list)
{
$this->setData($list);
$this->setMeta($list);
}
abstract protected function setData(PaginatedList $list);
protected function setMeta(PaginatedList $list)
{
$this->meta = new MetaView($list);
}
}
<?php
declare(strict_types = 1);
namespace App\Form\Model\View;
use App\Entity\CustomsStatisticsEntry;
use App\Form\Model\Response\PaginatedList;
/**
* Class CustomStatisticsView
*/
class CustomStatisticsListView extends AbstractListView
{
/**
* @var CustomStatisticsView[]
*/
public $data;
protected function setData(PaginatedList $list)
{
$this->data = array_map(
function (CustomsStatisticsEntry $entry) {
return new CustomStatisticsView($entry);
},
$list->getData()
);
}
}
<?php
declare(strict_types = 1);
namespace App\Form\Model\View;
use App\Entity\CustomsStatisticsEntry;
/**
* Class CustomStatisticsView
*/
class CustomStatisticsView
{
/**
* @var int
*
*/
public $id;
/**
* CustomStatisticsView constructor.
*
* @param CustomsStatisticsEntry $customsStatisticsEntry
*/
public function __construct(CustomsStatisticsEntry $customsStatisticsEntry)
{
$this->id = $customsStatisticsEntry->getId();
}
}
<?php
declare(strict_types = 1);
namespace App\Form\Model\View;
use App\Form\Model\Response\PaginatedList;
/**
* Class MetaView
*/
class MetaView
{
/**
* @var int
*/
public $total;
/**
* @var int
*/
public $totalPages;
/**
* MetaView constructor.
*
* @param PaginatedList $list
*/
public function __construct(PaginatedList $list)
{
$this->totalPages = $list->getTotalPages();
$this->total = $list->getTotal();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment